Some of the things that a programmer needs to know is how to duplicate/back up his/her code so that even if he loses his /her copy, he/she can still recover his/her work. And of course, we want to do it with some security. Now I know you’re thinking SVN for the first one: SVN works like a charm, especially when it comes to coding and code updates. And for the second (security) one, there’s ssh.
Well, then, what about we combine back up and security in one go? This is where rsync comes in. With rsync, I “sync” (sorry, couldn’t help it with the joke) my work in my PC to another PC in the next room, or the server room. I can even sync with a PC halfway across the world. But where’s the security you ask? Well, that’s were ssh comes in.
We can use rsync and ssh together. rsync allows one directory’s data to be mirrored as is (even the permissions) to another directory, whether it be remote or local. And as you may know already, ssh takes care of the security, encryption, etc.
So how do we do it? Well, one implementation is the following:
rsync -e ssh -av <source> <destination>
It’s as simple as that. What the previous command basically says is that we use ssh as a remote shell (-e), print some information regarding the transfer (-v), and synchronize all files (-a).
As an example, say I have a PC at some room at some floor of the building with address 10.36.2.235 , and I have a directory there:
Where I want to dump my current codes at the present working directory:
Now, let’s try ‘syncing’ my current working directory to my hard drive at 10.36.2.235
rsync -e ssh -v * firstname.lastname@example.org:~/kubuntu/test_dir
Of course, since it is ssh, I am prompted to enter my password. After entering it, here’s what happens:
email@example.com's password: skipping directory docs skipping directory ma3x skipping directory qt codes skipping directory tests BeautifulSoup.py BeautifulSoup.pyc BeautifulSoup.tar.gz Download.htm LICENSE LICENSE-2 PBQSingle.py README README-TESTS RSS.py feedparsertest.py read_it.txt rm-flv.py sampledownload.txt sampledownload.txt~ sampledownload2.txt sampledownload2.txt~ sampledownload3.txt sampledownload3.txt~ server.py server.py~ setup.py test-beautifulsoup.py test-beautifulsoup.py~ test-prog exec3.py~ test-time.py test-time.py~ tg_tutorial.tar.gz top_rated.rss vid_log.txt sent 13317215 bytes received 1406 bytes 409803.72 bytes/sec total size is 13311523 speedup is 1.00
Success! So when I edit something in my current directory, even the smallest one, say I update a file’s time stamp:
$ ls -lh vid_log.txt -rw-r--r-- 1 f f 40 2007-08-07 02:50 vid_log.txt $ touch vid_log.txt $ ls -lh vid_log.txt -rw-r--r-- 1 f f 40 2007-08-08 00:27 vid_log.txt
There we go. So let’s see if our little rsync command will help us out with this little change. I again issue:
rsync -e ssh -av * firstname.lastname@example.org:~/kubuntu/test_dir
and the output is
email@example.com's password: building file list ... done vid_log.txt sent 121037 bytes received 48 bytes 14245.29 bytes/sec total size is 18345757 speedup is 151.51
Yet another success (^)___(^) Since all we modified was the file vid_log.txt, and when we issued the same rsync command as before, naturally it only needed to update the vid_log.txt file on the other directory! Nice.
Aahh… the wonders of ssh…more to come soon! (^)____(^)