Sometimes using a full-fledged backup software or the built-in Windows Backup service is an overkill for your requirements. If your need is simply to backup some files and folders periodically, then a bit of Python programming combined with the Windows Task Scheduler takes care of things.
There is a single python file which does the actual backup. There are two kinds of backup:
- Copying files to another disk or folder
- Compressing files and folders and saving the zip file
If you choose the zip option, make sure that your data does not create a zip file more than 2 Gb as zip files cannot work if they are more than approx. 2Gb in size.
The Windows Task Scheduler is used to run the backup script as and when required. Rather than try to run the python script directly from the Task Scheduler, it is better to create a batch file which calls the python script and then run the batch in Task Scheduler.
It is good to have multiple sets of backup. To do so, you can create two identical scripts where the locations of the archived files are different. An easy way is to run a first set on Sun/Tue/Thu/Sat and the second set on Mon/Wed/Fri .
## # Script to copy files and compress them and put them in a separate location # Amit Sengupta, Sep 2015, HYD # Written in Python 2.7 ### import os import zipfile import shutil #copy files and folder and compress into a zip file def doprocess(source_folder, target_zip): zipf = zipfile.ZipFile(target_zip, "w") for subdir, dirs, files in os.walk(source_folder): for file in files: print os.path.join(subdir, file) zipf.write(os.path.join(subdir, file)) print "Created ", target_zip #copy files to a target folder def docopy(source_folder, target_folder): for subdir, dirs, files in os.walk(source_folder): for file in files: print os.path.join(subdir, file) shutil.copy2(os.path.join(subdir, file), target_folder) if __name__ =='__main__': print 'Starting execution' #compress to zip source_folder = 'D:\\projects\\dummydata' target_zip = 'e:\\backups\\dummydata.zip' doprocess(source_folder, target_zip) #copy to backup folder source_folder = 'D:\\data\\mp3files' target_folder = 'e:\\backups\\mp3backup' docopy(source_folder, target_folder) print 'Ending execution'
The batch file can contain a single line which runs python from its installed folder and the full path of the python script. Eg.Mybackup.bat
To run a task on certain days of the week, choose Weekly trigger and select the desired weekdays as shown below: