Archive for March, 2011

Python Incremental Backup Scripts

March 20, 2011

While interning at Honeywell, I found this small problem that I had to solve. We arent allowed to move flash drives around since they are quite a security risk to computers running Windows XP, and if you want to backup your files between a home computer and a work computer, there is really no better way than a flash drive. Given that we couldnt use them though, I had to come up with a clean solution to the probem. I tried taking differential backups with 7zip which does a pretty great job at this, bu the problem is that its “differential” is based on timestamps. The home computer runs Linux and the Office one Windows. For some weird reason, the timestamps on windows are of different precision than those on linux, so basically, the same file modified at the same time looks different to 7zip on a windows box and a linux box.

I had to come up with some kind of solution for this, so I wrote the following python scripts. One is for backing up a custom folder (say Project) and the other is for restoring the backup on another machine. These scripts take an md5 sum of the contents of the files, so timestamps dont matter.

backup.py

restore.py

The usage is simple. Just copy these scripts to the folder which contains the folder you want to backup. For example, If you want to backup D:\Games , just copy the scripts to D:\ . Now edit the scripts and change the line :-

dir_backup=”Directory” to

dir_backup=”Your Folder Name”

In the above example, that would be :-

dir_backup=”Games”

Now youre good to go. Just double click the scripts (in windows) or run the scripts from the terminal (in linux) to run the backup. When you first backup, some files will be created. Just take the scripts to some other computer and copy delta.7z and remove.txt to the same directory, and run the restore script on it. This would generate the folder you backed up in the same directory as the scripts.

From here on, you just need to run backup.py on one machine and copy the 2 mentioned files to the other and run restore.py. The scripts will make sure that everything is up to date between the folders.

As an example, suppose you have a home computer and an office computer and youre trying to backup the folder “Project”(assume we begin at home),

Run backup.py at home

Mail yourself the delta.7z and the remove.txt

Copy the scripts and the 2 files to some directory on the office comp.

Run restore.py on the office comp.

Make changes to the Project folder at office

Run backup.py at office

Mail delta.7z and remove.txt to home comp.

Copy (and replace) these files into the directory where the scripts are located.

Run restore.py

And so on…

Do comment if you have trouble understanding this!

Advertisements