Raspberry Pi automatic backup
As I now have a nice shiny new Raspberry Pi NAS, my old LaCie NetworkSpace was now redundant.But not for long...It's always a very good idea to make regular backups of your irreplaceable files, because hard drives do fail, and do fail spectacularly, then all your files are gone forever, unless you want to pay some serious £££'s.So, on the Pi you could manually copy all your files across the network onto another hard drive connected to your computer, (if you remember) or you could automate the process by using cron and rsync...Cron is exactly like the scheduled tasks feature on Windows Systems, but on headless systems it's accessed through the terminal, and rsync can keep two directories in sync, even on remote hardware for off site storage.Ok, let's begin...First thing to do, is get the old NAS mounted, we do this the same way as we did when mounting the external USB drive, by using fstab.But first, we need to create the mount point and make it world writable...
BTW, my old NAS is still connected to the router via an Ethernet cable.Next we need to edit the fstab file and add the command to mount the drive at our newly created location...
Use the cursor keys to scroll down, enter this line at the bottom, please note, as it's a network drive, the line is different to the USB drive line, and write everything on one line only...
Replace the IP address, mount location, user and password with whatever your values are.I'm using the Myshare folder on my NAS as it's password protected, there is an openshare folder as well, but it's not protected. You can omit the username and password values if you don't need to login, but beware that a NAS connected directly to the router will be available on the network and anyone can access the contents on an unprotected folder.However you could use another USB drive instead if you prefer, just repeat what you did to mount the first USB drive.Now mount the drive, as it's in fstab, you can just do...
Now it's time to play with rsyncRsync keeps directories in sync, this includes new folders and files, existing files that have been edited, and optionally removing folders and files that have been deleted, the two directories will be exactly the same.
I prefer to create a bash script and have cron run that, instead of running the command directly from cron, using a script means you can make manual backups at any time just by running the script.Run these three commands...
This will create a blank file, make it executable and open it for editing. Type this into the window exactly, but edit the paths to match yours...
If you wanted to backup more locations, just add another line.
Press CTRL+O, ENTER and CTRL+X to save and exit.Note: -a means all files, --delete means to delete files from the backup if the original has been deleted, you can omit this if you want to keep all your files, but your backups will get quite large if you create and delete a lot of files regularly.Test the script by running it like this...
If it works, you'll see activity on both drives, and if there's a lot to backup, this could take a while, you can interrupt the backup process by pressing CTRL+C.Now It's time to add the entry into cronThis will automatically run the backup script at the specified time...
Scroll down to the bottom where this line is. # m h dom mon dow command m means minute, h means hour, dom means day of month, mon means month, dow means day of week and command is the command or script you want to run...Add this line to the bottom...
In my line the script /root/backup.sh will be run at 03:00 every day, 0 for minute, 3 for hour, and * means every, so a star in the remaining values will tell cron to run the script every day at 3am. To run the script every March 16th at 14:36 you would use these values 36 14 16 3 * /root/backup.sh 2>&1 > /dev/nullBTW 2>&1 > /dev/null means to redirect all output to null, you could use 2>&1 > /home/pi/cron.log to keep a log file for a while to watch for errorsOnce complete, press CTRL+O, ENTER and CTRL+X, crontab will check your entry on exit and complain if you made a mistake.To access the backed up files, you could add an entry into smb.conf and have a password protected shared folder..?Otherwise all that's left to do now, is wait and see...