[Python] Updater/archiver/synchronizer of directories
I have directory with data (music, photos, ebooks etc.) on my two computers. I wanted to have exactly same files in my data directory on both machines, but sometimes I changed content of the directory on one of computers. I’ve decided to write program, which would help me to update data directory on another machine.
Firstly, I’ve installed Samba, a good tool for managing it – gsambad and smbfs (mount and umount commands for the smbfs). You can use NFS instead of samba if you want. Then, I’ve made my “changed” data directory accessible for another computer.
Lets assume, that /data is “unchanged” data directory on second computer, and /datarem is mounted “changed” data directory from first computer. You can mount this directory using for example:
sudo mount -t smbfs -o iocharset=utf8,username="TYPE_SMB_USER",password="TYPE_PASS" //192.168.2.15/data /datarem
Then you can use my script mov.py with following usage:
./mov.py changed_directory directory_to_update
./mov.py /datarem /data
The script will scan /datarem recursively for files and directories. If corresponding file or directory doesn’t exist in /data, it will be created. If size of file in /datarem is different to a size of corresponding file in /data, the file in /data will be replaced. If sizes are equal, file in /data is keep unchanged (it doesn’t matter for me, but for some usages it may be a problem).
Here is code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
#! /usr/bin/env python # Created by Wojtek Jamrozy (www.wojtekrj.net) import os, sys, shutil def update(start, end): if os.path.isdir(start): for f in os.listdir(start): s_ = os.path.join(start, f) e_ = os.path.join(end, f) if os.path.isdir(s_) and not os.path.exists(e_): os.mkdir(e_) print "Directory " +e_+" has been created\n" update(s_,e_) else: if os.path.exists(end): if os.path.getsize(start) != os.path.getsize(end): shutil.copyfile(start,end) print "File " +end+" has been updated\n" else: shutil.copyfile(start,end) print "File " +end+" has been created\n" st = sys.argv en = sys.argv if not os.path.exists(st): print "Source directory does not exist!" sys.exit() if os.path.isdir(st) and not os.path.exists(en): os.mkdir(en) update(st,en)
You can download it here:
Most functions used in this script are easy to understand. You can find further information here:
3 thoughts on “[Python] Updater/archiver/synchronizer of directories”
Great! Thank you very much!
I always wanted to write in my blog something like that. Can I take part of your post to my site?
Of course, I will add backlink?
Regards, Timur I.
If anyone knows or provide..
I need UK VPN account.. (to bypass unblock etc..)
I already have USA vpn account..
I dont want to provide vpn service..
I want to buy and enjoy one..
Hello. Your site displays incorrectly in Firefox, but content excellent! Thanks for your wise words.