[Python] shutil을 사용한 주기적 전체백업

2023. 12. 27. 15:32카테고리 없음

 주기적으로 전체 백업을 진행할 일이 발생하여  shutil을 사용하여 백업을 진행해 보려한다.

매일 일단위 3회정도의 백업이 필요해 보여 처리를 진행한다. 

폴더 구조는 그림과 같이 처리하였다. 

실행은 그중에서 FolderCopy.py 파일을 사용한다. 

import shutil
from UTIL.DateTimeUtil import *

class FolderCopyExecuteClass:
    def __init__(self):
        print(" _____ INIT _____")
        self.DateTiemUtil = TimeUtilClass()
        orgPath = "/mnt/www_php"
        destPath = "/mnt/Z_AllBackUP/www_php/"
        self.setCopy(orgPath, destPath)

    def setCopy(self, orgPath, destPath):
        try:
            strDateTime = self.DateTiemUtil.getCurrentDateBunCho112()
            # print(strDateTime)
            destPath = destPath + f"{strDateTime}"
            shutil.copytree(orgPath, destPath)
        except Exception as e:
            print(f" _____ setCopy ___ {e}")

if __name__ == "__main__":
    FolderCopyExecuteClass()

crontab 을 사용하여 해당 내용을 주기적으로 처리하면 문제없이 진행될 듯 싶다. 

반응형