Building a NAS: Stage 2 (Software and Maintenance)

With the system up and running, it made sense for me to make an effort to see it stayed that way. I needed to be able to have the following things happening without any intervention on my behalf:

  • Users’ Windows PCs backing up automatically to their respective folder on the network
  • These backups being automatically rsync’d with an external disk, for a true backup
  • Reports on the status of the machine being made available to me at regular intervals

The first is turning out to be the most difficult. Initially I wanted to write a batch script that handled this for me and then configure it to run once a week using Task Scheduler in Windows. However, there are three different versions of Windows for me to contend with out of my five users, (XP, Vista, 7), so it wasn’t as straightforward as I thought it would be, with Vista and 7 including “Robocopy” out of the box, and XP not. However, we all know the most straightforward solution isn’t always the best one. Even with Robocopy, you cannot do incremental/differential file copies. You can coarsely say “all files changed after some date”, but it still copies everything in full.

Enter FTPSync, a nice little FTP tool which has a built-in differencing algorithm and allows full, incremental and differential backups. Its configuration is somewhat obscure, but nothing too difficult to understand with the help of their online documentation (which as it always happens, was actually down the first time I wanted to access it). After some initial tests on a small dataset, I think it will be most suitable for my purposes, for two reasons: (a) incremental/differential backups, saving time, and (b) it does this over FTP, which is the most CPU-efficient protocol for my underpowered file server.

Setting up the user home directories was pretty easy: just enable the [homes] share in your Samba configuration, and you’re away. Mine are actually redirected via symlinks to another disk, so you also have to add “symlinks = yes” to the [global] section of your config, or your home directories won’t appear as intended. The rsync configuration was also pretty straightforward, but I’ll include my backup script here for your reference:

  1. #!/bin/sh
  2. # 200GB is 209715200 bytes
  3.  
  4. # rsync.pwd file must be owned by root as script is executed from roots crontab
  5.  
  6. RSYNC=/usr/bin/rsync
  7. SSH=/usr/bin/ssh
  8. KEY=/home/owen/ssh-key/owen-rsync-key
  9. RUSER=owen
  10. RHOST=192.168.1.50
  11. RPATH=/home/owen/backup/fileserver-backups/owen/
  12. LPATH=/var/data/owen
  13.  
  14. # $RSYNC -az -e "$SSH -p 768 -i $KEY" $RUSER@$RHOST:$RPATH $LPATH
  15. # rsync -az /var/data/owen/ owen@boxenmkiv::owen –password-file /home/owen/rsync.pwd
  16.  
  17. homeSize=`du -sD $LPATH | cut -f 1`
  18. emailMSG=""
  19. MAILSUBJ="Backup summary for $LPATH on `date`"
  20. RECIPIENT="owen"
  21.  
  22. if [ $homeSize -gt 209715200 ]; then
  23.         emailMSG="User $RUSER rsync backup failed.\nReason: $LPATH size is ~ $(($homeSize/1024/1024)) Gb"
  24. else
  25.         emailMSG="$LPATH is ~ $(($homeSize/1024/1024)) Gb, proceeding with backup at `date`.\n"
  26.         # $RSYNC -az -e "$SSH -p 768 -i $KEY" $RUSER@$RHOST:$RPATH $LPATH
  27.         rsync -az /var/data/owen/ owen@boxenmkiv::owen –password-file /home/owen/rsync.pwd
  28.         emailMSG=$emailMSG"$LPATH backup completed at `date`."
  29. fi
  30. echo $emailMSG | nail -r "my@email.net" -s "Owen’s Backup Result for `date`" my@email.net

There’s actually a lot of junk at the start of the script which I haven’t cleaned out yet (since changing from an rsync over SSH approach), but the important part, the part that does all the work, is the rsync call on line 28. The simple if construction checks the size of the data to be backed up, and then proceeds if below 200Gb, otherwise does nothing. The result of the script is e-mailed to me when it finishes. Also, I have created a little script which runs every now and then to collect the temperature of the hard disks, a df output or two, and some other info, which is also e-mailed to me periodically, just to keep an eye on things.

There is a version of the above script for each of the five users on my system, and they are all executed using roots cron daemon. As noted in the script, the rsync.pwd file must be owned by root for this to work.


About this entry