edit · print · PDF

Please note that all the SIEpedia's articles address specific issues or questions raised by IAC users, so they do not attempt to be rigorous or exhaustive, and may or may not be useful or applicable in different or more general contexts.

Automated Disk Backup

Let's assume we want to have an updated, on a daily basis, copy of the content of some drive or directory "A", to some other drive or directory "B". This can be done automatically and easily by making use of the commands "rsync" and "crontab" (linux), as explained in this recipe step by step:

Create syncronization script

  • The drive/directory whose content we want to keep a copy of is: /scratch1 ("original" directory)
  • The drive/directory where we want to save the backup copy is /media/diskusb/scratch1 ("mirror" directory)
  • With our favourite text editor, we create a file named "sync.script", with the following line as text:
    rsync -avz --force --exclude=lost+found --delete-after /scratch1/ /media/diskusb/scratch1/
  • We save the file "sync.script" in "/home/myusername/somewhere", i.e., some subdirectory of our "home" directory.
  • We then give execution permission to the script:
    cd /home/myusername/somewhere
    chmod +x sync.script

Schedule daily execution

Now the script is ready to be executed and perform the syncronization of the "mirror" directory to match the "original" directory. But we still have to tell the system to execute the script every day, for the copy to be kept updated. For example, let's assume we want the "sync" to be done every day at 01:01 (1 am). As this procedure is noiseless, neighbours won't complain. The "rsync" command will, as we are calling it, at 01:01, "update" the copy of the "original" directory kept in the "mirror" directory, as expected. It will only copy again those files in the "original" directory that have changed since the last update (the day before, in this case). Obviously, new files in the "original" directory will also be copied to the "mirror". Also, if some file in the "original" has been deleted since the last update, it will be deleted from the "mirror" as well (to remove this feature, omit the "--delete-after" from the rsync options).

  • So, for the script to be executed at hh:mm (24 hour time format), every day, do the following from the console:
    crontab -e (with this we can edit the file where the system reads the tasks to be performed periodically)
  • press the "Insert" key in your keyboard (the default text editor used to modify the "crontab" file is "Vim", and this is the way to start an edition of a file with this program)
  • now the message "-- Insert --" will show up in the lower left part of the console, and we write the following line
    01 01 * * * /home/myusername/somewhere/sync.script
  • press the "Esc" key. (the "-- Insert --" message dissapears)
  • write ":wq" (no quotation marks), and press "Enter".
  • To check that the crontab file has been correctly modified, do the following from console:
    crontab -l
    which should show the line you inserted above. (If not, something has gone wrong... do man crontab for usage info). But it should work on the first shot.

And that's it. Next time it's 01:01 the update of the backup copy will begin. The first time the whole content of the "original" directory will be copied to the "mirror", if it hasn't been done before, and this may take quite a while depending on the volume of data to be copied. Check first that there's enough room in the "mirror" to store a copy of the "original" (use du -h to see the content of the "original" directory in Kb/Mb/Gb, depending on size). Also, check that you, as user, have write permissions on the "mirror" directory. Every day, after the syncronization is done, the system will e-mail you at "myusername@iac.es", notifying what was done during the process, with the following subject: Cron <myusername@iac.es> /home/myusername/somewhere/sync.script

You can filter these e-mails to a separate e-mail folder, which can be "cleaned" every month, for example. I would suggest to check these e-mails, at least from time to times, to ensure that the copy is being correctly updated. It is possible to disable this e-mail capability from the crontab... but check it out yourself in the "crontab" help, as I don't know how to do it.

Advanced usage

You might want to keep also a copy of the erased and/or modified files in your directory, but keeping them in another directory. This could be done with a similar script, using the --backup option, whose execution could also be scheduled in the crontab file. An example:
set fecha=`date +'h20%y'`
rsync -au --delete --backup --backup-dir=/media/diskusb/${fecha}/ /scratch1/ /media/diskusb/scratch1/

edit · print · PDF
Page last modified on June 03, 2008, at 11:53 AM