Daily backup script in Python

How to create daily backup in python ? Using shutil and date packages we can create a simple daily backup script.

Below script accept source and destination folder as arguments and create date based zip folder

If user want to automate it, please hard code the folder and call python from a shell script or control-m job

Below backup script works in Windows/Linux/Mac operating systems

Python Version – 3 or above

Note:- Please make sure you have sufficient privileges to create folder at destination

import shutil
from datetime import date

class Backup:
    def __init__(self):
        self.fromfolder = input("Please enter Source folder path:")
        self.tofolder   = input("Please enter Backup folder path:")
        self.backupday  = date.today()
        self.newfolder  = self.backupday.strftime("%B%d")
        self.tofolder   = self.tofolder + self.newfolder
        #print('to folder-',self.tofolder)
        self.backup()

    def backup(self):
        shutil.copytree(self.fromfolder,self.tofolder,copy_function = shutil.copy,dirs_exist_ok=True) #copying
        shutil.make_archive(self.tofolder,'zip',self.tofolder) #zipping
        print("Backup created as zip file at-",self.tofolder)
        shutil.rmtree(self.tofolder) #cleaning

if __name__ == "__main__":
    b = Backup()

Note that above program reads all files from a particular folder and copy to another backup folder after zipping it. You can customize it by choosing only particular file types. This script can be automated by calling from a shell script or control-m job. it will create new folder every day.

complete code can be found at github repository

https://github.com/manojankk/simple-backupscript-python/