Backup your FreeNAS/NAS4Free shares automatically
If you use FreeNAS or NAS4Free as a file server it is highly recommended to backup your data directories regularly. But often people do not make backups for the sake of convenience. Although it is pretty easy to setup an automated multi generation backup with FreeNAS. To avoid interfering your daily work it is reasonable to perform the backup at night.
FreeNAS uses RSYNC for data saving and syncing. Therewith you can save multiple instances of a data pool without multiply data volume (snapshot). There are only changed files saved. But in the file system it looks like that the complete data pool is multiple saved. That is done by the application of file system links.
For inexperienced users it is easy to restore files because they see an exact copy of the original file system in the backup. They just have to pick up the file/files in question and copy it back. Finally RSYNC just mirrors the data in a very smart and efficient way.
You just need a second harddisk and the script rsnc_snap_d2d.sh from the Knowledgebase of FreeNAS. Change line 24:
if you need more than 9 generations of a backup. i=9 means that nine backups will be stored till the first will be overwritten (first in, first out). If the backup is performed daily you can restore files up to an age of 9 days.
Setup the backup
This how-to is based on FreeNAS 0.69.2 (Muad’Dib).
- Connect your backup hard drive to the server and restart FreeNAS if indicated.
- Add the harddisk in FreeNAS’ Backend (Disks|Management”)
- Format the drive (Disk|Format), I recommend UFS-format
- Set a mount point (Disks|Mount Point|Management) to your drive
- Create a new share (Services|CIFS/SMB|Shares) so that users have access to the backuped files.
But you have to set the access to “read-only” (see screenshot above)! That is very important to avoid confusion. Because it is likely that users, if they find the file they are looking for, edit it and save it at the place where they opened it. And this place is the backup share. When they open the file again on the regular share (because they forgot that they altered the file somewhere on the backup share) they are confused that the changes did not happened. Bit they happened somewhere on the backup. If you have a lot of backup instances you will spend a lot of time to find your file with the changes you made.
You can leave the other settings as they are.
- Upload/copy rsnc_snap_d2d.sh to a share.
Important for experts: If you use SSH to upload the script, please be advised that if you store the script somewhere else in the file system and you installed FreeNAS/NAS4Free embedded, it is lost after a server reboot. Because an embedded installed FreeNAS/NAS4Free stores the core file system on a RAM disc. Just the shares (/mnt) are on the hard drives permanently. This limitation does not apply to full installations!
- To get the backup task done regularly you have to create a cron job (System|Advanced|Cron) with root privileges. Klick on “+” and fill out the field command, like:
sh /path/to/share/rsync_snap_d2d.sh /path/to/share/ /path/to/backupshare/
If you store the script in the root directory of the share called myshare and if the share you want to backup is also myshare and if the target share for the backup is called mybackup the command is:
sh /mnt/myshare/rsync_snap_d2d.sh /mnt/myshare/ /mnt/mybackup/
The first fraction sh of the command executes a shell. The second described the path and the name of the backup script. The third ist the path to the share you want to backup. The fourth part is the path to the backup share. You do not need to save a whole share you can just backup a folder if you like. Just specify the folder in fraction 3 of the command.
Anyway, it is sometimes reasonable to store the backup in a folder and not in the root directory of the backup share. You just have to append the name of the folder after /mnt/mybackup/. But avoid special characters – even umlauts – or blanks in the name of the share. If you can not avoid blanks, put the path in quotation marks, e.g.: “/mnt/my backup/” but not the whole command.Because FreeNAS/NAS4Free are UNIX-System you have to keep in mind that file names and consequently paths are case-sensitive.
In the screenshot below the backup will be performed every day at 5am. If you have more than one share which need backup create more cron jobs like the one above.
The backup above is stored in the backup share /mnt/mybackup in the directories myshare_bak1 to myshare_bak9 if the value “i” in the script is 9. myshare_bak1 contains the newest backups, myshare_bak9 the oldest. The time stamp of the directories shows which backup from which time they contain. To restore, just copy the files or folders in question back with your file manager.
Case of Average
If your harddisk is broken and you need to copy your data back on a new disk my posting Restore RSYNC backups of FreeNAS/NAS4Free will give you step-by-step instructions.
When you need your data quickly before you exchange your harddrive and copy back you backup, refer Mount FreeNAS UFS-Harddrives for details.
- Easy Automated Snapshot-Style Backups with FreeNAS and Rsync | FreeNAS Knowledgebase
- Easy Automated Snapshot-Style Backups with Linux and Rsync | Mike Rubel