I have been trying to figure how to set up the perfect automatic backup system for my needs with *buntu. I think I finally got it and I'm writing it down while it is still fresh in my mind so I can reference it later in case I forget. Hopefully, this is helpful for other people who have similar needs.
Backing up to a network hard drive (NAS) is more complicated in Linux than it should be. This tutorial describes all the steps necessary to get an automatic backup system running. I have only tested this with Kubuntu / Ubuntu 10.04 Lucid, but it should work for other versions and could also be adapted for other distros.
Don't be intimidated by the length of the tutorial, it is actually very easy to set this up if you follow it step by step. And, it is well worth the trouble if your computer hard drive ever fails.
If anything can be improved, please let me know!
Resources (read these later if you want more in depth info about the software used in this tutorial):
cifs configuration: https://help.ubuntu.com/community/SettingUpSamba
rync commands: http://linux.about.com/library/cmd/blcmdl1_rsync.htm
Automatic Backups to the Network Hard Drive
1. Install necessary software
(libnotify-bin is for alerts when the backup has failed)
sudo apt-get install smbfs libnotify-bin
2. Create mount folder
sudo apt-get install smbfs
then, set ownership:
sudo mkdir /media/backups
"USERNAME" is what you use to login and "GROUP" is the same as your username.
sudo chown USERNAME:GROUP /media/backups
3. Create credentials file to mount drive
gksudo gedit /etc/samba/user
in the empty file, paste the following (and save after adjusting settings):
kdesudo kate /etc/samba/user
replace USERNAME with the username you use to login and replace PASSWORD with the password you use to login.
then, change the permissions on the file to read only for security:
4. Setup network hard drive to always mount
sudo chmod 0400 /etc/samba/user
gksudo gedit /etc/fstab
at the bottom of the file, add the following (and save after adjusting settings):
kdesudo kate /etc/fstab
replace "192.168.XXX.XXX/backups" with the IP address of your network hard drive along with the path to the folder you have setup on the hard drive to store backups and hit enter after you have pasted the line in for an extra carriage return.
//192.168.XXX.XXX/backups /media/backups cifs credentials=/etc/samba/user,noexec,uid=1000,gid=1000 0 0
my fstab file looks like this:
IMPORTANT: Make sure there is an extra carriage return after the last line in the fstab file, otherwise things will be broken next time you reboot and you will have to repair the fstab file with a Live CD.
# /etc/fstab: static file system information.
# Use 'blkid -o value -s UUID' to print the universally unique identifier
# for a device; this may be used with UUID= as a more robust way to name
# devices that works even if disks are added and removed. See fstab(5).
# <file system> <mount point> <type> <options> <dump> <pass>
proc /proc proc nodev,noexec,nosuid 0 0
# / was on /dev/sda1 during installation
UUID=6ce894a4-c599-46a4-810e-8e7a82e9a8c2 / ext4 errors=remount-ro 0 1
# swap was on /dev/sda5 during installation
UUID=585686b0-4158-4615-822d-2fbab12a2126 none swap sw 0 0
//192.168.1.100/backups /media/backups cifs credentials=/etc/samba/user,noexec,uid=1000,gid=1000 0 0
At this point it may be a good idea to reboot and check to make sure the network hard drive is being mounted. After reboot, navigate to /media/backups. You should see any files you already have in your backup folder listed there.
6. Create a folder for current backup
then, set ownership:
sudo mkdir /media/backups/lucid
Since I like to do a fresh install every six months when the new version of *buntu is released, I like to keep the backups in separate folders reflecting the current version I am using. Then, every time I install a new version I create a new folder to have the live update in, and compress the previous folder using bz2 for an archival backup (.i.e. karmic, jaunty etc). This works well for me, and you can set this up in whatever way makes sense for you.
sudo chown USERNAME:GROUP /media/backups/lucid
7. Create file to test if mounted
8. Create the backup script
sudo touch /media/backups/mounted
gksudo gedit ~/backup.sh
paste the following and adjust the warning message as you see fit :
kdesudo kate ~/backup.sh
if [ -f "/media/backups/mounted" ]
rsync -rlptgo --delete --delete-excluded --exclude=**~ --exclude=**/*cache*/ --exclude=**/*Cache*/ --exclude=.local/share/Trash ~/ /media/backups/lucid/
notify-send 'Backup Failed' 'Please connect hard drive and back up your daturz ASAP!'
This script will backup everything in your home folder and exclude stuff you don't need to backup, like your trash and browser cache. For more advanced usage of rsync commands check out the rsync resource at the top of the tutorial.
if [ -f "/media/backups/mounted" ]
rsync -rlptgo --delete --delete-excluded --exclude=**~ --exclude=**/*cache*/ --exclude=**/*Cache*/ --exclude=.local/share/Trash --exclude=**/*trash*/ ~/ /media/backups/lucid/
kdialog --title "Backup Failed" --passivepopup "Please connect hard drive and back up your daturz ASAP!" 3600 &
9. Make the script executable
10. Set the backup to run automatically
sudo chmod +x ~/backup.sh
type or paste in the following, replacing the example (right-click or Ctrl Shift V to paste in terminal):
this will run the backup every day at 1 PM. If you would like the backup to run once a week on every Sunday at 9 AM, add this:
0 13 * * * /home/USERNAME/backup.sh
replace "USERNAME" with your username.
0 09 * * 0 /home/USERNAME/backup.sh
press Ctrl O and then hit Enter to save the changes. then press Ctrl X to exit crontab.
I recommend running the backups daily. After the first backup is completed, only the changes will be copied over. rsync quickly compares all the files in the backup folder on the network hard drive with all the files in your home folder and only transfers what has changed so that the backup is an exact duplicate of your current home folder.
11. Test that it doesn't work
this will unmount the network hard drive. then run:
sudo umount /media/backups
because the network hard drive is not mounted, the backup will fail and you should see the warning pop up.
12. Test to make sure it does work
this will remount the hard drive (remember, the hard drive will automatically mount every time you boot up the computer, as long as the network hard drive is accessible, so you only need to run this if you have manually unmounted the hard drive).
sudo mount /media/backups
the backup will start running in the background.
in your file browser (nautilus for Ubuntu / dolphin for Kubuntu) you can browser to your external hard drive by navigating to smb://192.168.XXX.XXX (of course replace with actual IP address) in the address bar. if you check your backups folder, you will see everything from your home folder is there (or in the process of being copied).
13. Final step
Do the happy dance, because you now have daily automatic backups that will keep your important files synced in case your computer hard drive ever fails.