Tuesday 21 February 2012

Daily Backup Script for Linux Users

Those who know how rsync works, need not read the rest of this post!!


Those who are working in two different computers on a daily basis would find this script very useful for synchronizing the data across them. This can essentially be used as a simple backup solution instead of Dropbox or Ubuntu One if your machines are running Linux.


In all the following scenarios it is  assumed that

  • The files in the folder ~/Work or /home/username/Work is being synchronized to a USB drive or a remote location and vice versa.
  • The folder ~/bin exist in both the machines. If the folder ~/bin does not exist use the command mkdir ~/bin to create it. 
  • The path ~/bin is in the PATH environment variable. To check the path issue echo $PATH in the terminal. If it is not, adding the following in the either .profile or .bashrc would set it, next time you login.
          # set PATH so it includes user's private bin if it exists
if [ -d "$HOME/bin" ] ; then
    PATH="$HOME/bin:$PATH"
fi
  • The script files are to be located in ~/bin.
Scenario 1: Synchronizing files to/from a USB drive 
The USB drive when connected usually gets mounted as /media/disk (or for example /media/TRANSCEND if the name/label of the drive is TRANSCEND). In the following the USB stick/Hard disk is labelled as F45TY and has a folder by name myscripts in it.


Create a file, say sync2pen in ~/bin with the following contents


#!/bin/sh
rsync -avz ~/Work /media/F45TY/
rsync -avz ~/Official/*.pdf /media/F45TY/Docs/
rsync -avz ~/bin/sync2* /media/F45TY/myscripts/


and set correct permissions by issuing chmod a+x sync2pen

The dissection of the script
The Shebang
#!/bin/sh
invokes the Bourne shell or a compatible shell. This is the standard starting line of a shell script.
rsync -avz ~/Work /media/F45TY/
Synchronizes all files in the folder/directory ~/Work to the folder Work in the USB drive.
rsync -avz ~/Official/*.pdf /media/F45TY/Docs/
Synchronizes all PDF files in the folder ~/Official to the folder Docs in the USB drive.
rsync -avz ~/bin/sync2* /media/F45TY/myscripts
Synchronizes all files starting with sync2 in the folder ~/bin to the folder myscripts in the USB drive. This line ensures that you have with you the latest script in all the machines/USB drives.

Now to sync the folders issue the command sync2pen in the terminal. It will list all files that were synced/copied to the destination.

To do the reverse, i. e., to sync files in the USB drive to the hard disk, (say, on the second machine) change the source and destination in the rsync commands

Create a file, say sync2disk in ~/bin with the following contents

#!/bin/sh
rsync -avz /media/F45TY/Work ~/
rsync -avz /media/F45TY/Docs/*.pdf ~/Official/
rsync -avz /media/F45TY/myscripts/sync2* ~/bin/

set correct permissions by issuing chmod a+x sync2disk and issue the command sync2disk in the terminal.


Alternately if you do not wish to create these sync scripts in ~/bin, you can keep it in any location and source the script when needed.

Note: If the USB drive is in FAT32/NTFS format (usually it is!) you might notice that every time the script is run, all the files are copied to destination irrespective of changes being made on them. This is due to the incompatibility of permissions in ext3/ext4 file-systems with FAT/NTFS file-systems. So it is better to use a USB drive with a Linux partition and syncing to it. GParted will help you to create/modify Linux partitions.

Scenario 2: Synchronizing files to/from a remote machine 
If you want to sync data between two machines connected by a network, the following will help you. In this example, the files in the local machine is synchronized with a remote machine with IP address 10.56.2.34 and the user vivek has access to it.

Create a file, say sync2remote in ~/bin with the following contents


#!/bin/sh
rsync -avz ~/Work vivek@10.56.2.34:/home/vivek/
rsync -avz ~/Official/*.pdf vivek@10.56.2.34:/home/vivek/Docs/
rsync -avz ~/bin/sync2* vivek@10.56.2.34:/home/vivek/myscripts/



and set correct permissions by issuing chmod a+x sync2remote

Now to sync the folders issue the command sync2remote in the terminal. On invocation it will ask for user's password in the remote machine 10.56.2.34.

To  synchronize files from the remote location, create a file, say sync4remote in ~/bin with the following contents

#!/bin/sh
rsync -avz vivek@10.56.2.34:/home/vivek/Work ~/
rsync -avz vivek@10.56.2.34:/home/vivek/Docs/*.pdf ~/Official/
rsync -avz vivek@10.56.2.34:/home/vivek/myscripts/sync2* ~/bin/

set correct permissions by issuing chmod a+x sync4remote and issue the command sync4remote in the terminal.

Note: If hostname of remote machine is known, one can use hostname instead of IP addresses in the scripts sync2remote and sync4remote. Also username vivek can be avoided if both machines have same username in them.

Corrections/suggestions are welcome.