Incremental Backups with rsync and Python
This uses rsync to do an incremental backup to another drive. You could easily change this to do remote backups. It uses rsync’s –backup and –backup-dir arguments which will keep incremental backups. The latest backup will have all your files. Older backups will have the old versions of changed files. e.g. if you only changed blah.txt the latest folder will have all your files, while the folder from the day before will only have the old blah.txt.
This is meant to be run once a day, no more no less. Else, it will not work.
You’ll end up with a folder structure like this:
backups/20080428
backups/20080429
backups/20080430
etc
Python script is below:
#!python # Folders to Backup SOURCE_DIRS=( "C:/Backups", "C:/OtherStuff" ) # Backup to: (maybe ssh would work here, I haven't tried it) BACKUP_DIR="D:/" ###################################################### import datetime import platform import os IS_WINDOWS = platform.uname()[0]=='Windows' # Make backup dir, should only happen once if not os.path.isdir( BACKUP_DIR ): os.mkdir( BACKUP_DIR ) today = datetime.datetime.today() yesterday = today - datetime.timedelta(1) today_tstamp = '%d%02d%02d' % (today.year,today.month,today.day) yesterday_tstamp = '%d%02d%02d' % (yesterday.year,yesterday.month,yesterday.day) today_folder = '%s/%s' % (BACKUP_DIR,today_tstamp) yesterday_folder = '%s/%s' % (BACKUP_DIR,yesterday_tstamp) if os.path.isdir( yesterday_folder ): os.system('mv %s %s' % (yesterday_folder,today_folder)) os.mkdir( yesterday_folder ) else: os.mkdir( today_folder ) # Stupid Windows Fix for Cygwin if IS_WINDOWS: today_folder = today_folder.split(':') today_folder = '/cygdrive/%s%s' % (today_folder[0],today_folder[1]) for SOURCE_DIR in SOURCE_DIRS: # Stupid Windows Fix for Cygwin if IS_WINDOWS: SOURCE_DIR = SOURCE_DIR.split(':') SOURCE_DIR = '/cygdrive/%s%s' % (SOURCE_DIR[0],SOURCE_DIR[1]) print "%s --> %s" % (SOURCE_DIR,today_folder) rsync_options="-v -r -a --delete --backup --exclude=.svn --delete-excluded --backup-dir=%s" % yesterday_folder rsync_cmd = 'rsync %s %s %s' % (rsync_options,SOURCE_DIR,today_folder) os.system(rsync_cmd)
No comments
Jump to comment form | comments rss [?] | trackback uri [?]