Scenario
You have two NAS devices, for example two network hard drives (in my case Iomega StorCenter ix2). You want your users (Windows/Mac OS X/Linux) to be able to backup their data to one of them, and have a mirror of the backups on the second one (off-site backup). The setup does not need to be very secure, as this works on an internal network, so a password stored in clear text in a script is not a big deal.
Daily Synchronization by a Linux computer
Setup
Each user has an account and associated directory on Disk1. On Disk2, there is only one “Backups” directory. Windows share/CIFS/SMB is activated in both disks.
Here, both disks have DNS names “Disk1” and “Disk2”. You can use the IP address of the disks instead.
A backupUser user is declared on both disks, with reading rights only on all Disk1 directories, and all rights on Disk2 Backups directory.
On the Linux computer, the directories /mnt/serv1 and /mnt/serv2 must be created.
Backup script
First, let’s make a small perl script backupNAS.pl to perform the backup :
#!/usr/bin/perl -w # # You need : - a backupUser with - all reading rights on all Disk1 directories # - writing rights in Disk2/Backups # - the Disk2/Backups directory must contain a file named AutoBackup # # Mounting Disk2 in /mnt/serv2 `umount /mnt/serv2; mount -t cifs -o user=backupUser,password="MyBackupPassword",iocharset=utf8,file_mode=0777,dir_mode=0777 //Disk2/Backups /mnt/serv2/`; $err1 = $?; # Check : There must be an "AutoBackup" file in the directory if the disk in mounted correctly if ((not -e '/mnt/serv2/AutoBackup') or $err1 != 0){ die "ERROR : cannot mount Disk2 !\n"; } # Get the list of user directories in Disk1 @shareList1 = `smbclient -N -L //Disk1`; # Filtering the list to get only the shared directories. Lines should look like # " username Disk " @reps1 = (); print "Disk 1\n"; foreach $share1(@shareList1){ chomp $share1; if ($share1 =~ /^\s+(\S+)\s+Disk\s+/){ ($name,) = $share1 =~ /^\s+(\S+)\s+Disk\s+/; push (@reps1, $name); } } # Each directory will be mounted then unmounted foreach $rep1(@reps1){ `umount /mnt/serv1`; `mount -t cifs -o user=backupUser,password="MyBackupPassword",ro,iocharset=utf8,file_mode=0444,dir_mode=0555,noperm //Disk1/$rep1 /mnt/serv1/`; if ($? != 0){ print "ERROR : Directory //Disk1/$rep1 mount failed !\n"; next; } print "SYNCHRO rsync /mnt/serv1/ /mnt/serv2/$rep1\n"; print `rsync -rltpv --stats /mnt/serv1/ /mnt/serv2/$rep1`; # --del if deleted files must be removed on Disk2 too } `umount /mnt/serv1`; print "SERVER 1 : backup to DISK 2 is done !\n";
Cron script
Now, let’s create a small script that will call this one daily and log the results. Edit /etc/cron.daily/backupNAScall :
#!/bin/bash
echo >> /var/log/backupServers.log
echo >> /var/log/backupServers.log
echo "-------------------------------------------------" >> /var/log/backupServers.log
echo "Beginning BACKUP -> Launching backupNAS.pl" >> /var/log/backupServers.log
date -R >> /var/log/backupServers.log
perl /pathToScript/backupNAS.pl >> /var/log/backupServers.log
echo "BACKUP FINISHED" >> /var/log/backupServers.log
date -R >> /var/log/backupServers.log
echo "**************************************************" >> /var/log/backupServers.log
Now each day, the backup will start and detailled logs (including the date and time) will be stored in /var/log/backupServers.log !
From the NAS (in progress…)
Next step would be to remove the need for the linux computer entirely and execute this on the small linux system of the NAS disk. The following is specific to the NAS device (Iomega StorCenter ix2).
A ssh server can be activated on the NAS by going to the page https://myDiskAddress/support.html, then activating the access option.
When the disk has rebooted, you can connect to it with ssh root@myDiskAddress. The password is “soho” followed by the admin account password that was set up to access the web interface. With this, you have access to a small system (without perl or bash, just sh or ash – the previous code won’t work on this device “as is”. rsync is there, though).
In /etc/crontabs/root there is a warning :
# DO NOT SCHEDULE TASKS HERE (use sohoProcs.xml to schedule tasks)
The file /usr/local/cfg/sohoProcs.xml seems to be the right place for scheduled tasks. In it, we find commands like this :
<Program Name=”reportquota” Path=”/bin/sh”>
<Args>/usr/bin/reportquota</Args>
<SysOption Days=”1″ Scheduled=”1″ StartTime=”01:00″/>
</Program>
I did not try to add a custom command yet, but this should be the way to go…
This wiki might be helpful : http://ix2-200.wikidot.com/