If you want to periodically back up your data to an external drive then you have to do a few things:-
1. Set up the /etc/fstab file to auto mount drive on boot up
2. Make a shell script to do the actual backup [rsync]
3. Copy this shell script into cron.daily, cron.weekly ….
Thats all. Easy.
Being a Linux noob I found it easier said than done. Based on a few days of trial and error I have written this guide for noobs.
Setting Auto Mount using Menu->Settings->Disks [gnome-disks] did not work for me. By default it uses a weird mount point with UUID names and rsync stalled with permission errors mostly related to FAT file system. So I resorted to manually editing the /etc/fstab file.
gksudo leafpad /etc/fstab
The UUID is obtained by
This is the line to be inserted at the end of fstab.
#My card reader
UUID=3365-3564 /mnt/CARDBK vfat user,umask=000,utf8 0 0
echo "Critical" >> $LOG
echo `date` >> /home/lakshman/cronlog.txt 2>>$LOG
if [ -d $DST ]
then rsync -rlt --delete $SRC $DST 2>>$LOG
else echo "$DST does not exist" >> $LOG
The rsync flags are important. If the -a option is used which is shorthand for -rlptgoD then it will choke on FAT file systems since FAT does not support permissions and such.
Now copy this script into /etc/cron.daily
sudo cp ~/myscript.sh /etc/cron.daily
Cron can be set up by user using crontab -e command. But cron will miss cycles if the machine is Off at the cycle time. So I had to use system cron setup. The script in cron.daily is run by the cron daemon as per the /etc/crontab. This is the default crontab file
# m h dom mon dow user command
17 * * * * root run-parts --report /etc/cron.hourly
25 6 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6 * * 7 root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6 1 * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
If the script is copied into /etc/cron.daily all the scripts in cron.daily will be run at 6.25 AM. But if the machine is not on at 6.25 AM then this cycle will be missed. Thats where anacron helps. anacron uses the /etc/anacrontab file. This is the default /etc/anacrontab file
# These replace cron's entries
1 5 cron.daily run-parts --report /etc/cron.daily
7 10 cron.weekly run-parts --report /etc/cron.weekly
@monthly 15 cron.monthly run-parts --report /etc/cron.monthly
It appears that anacron will take over the cron job and you dont have to do ANYTHING except copying the script into the cron.daily
This works for me. Now I have automated daily backups of my project files and other data to my card reader in addition to my git repositories.