Setting up Synology NAS to act as backup destination

Posted by paul on 2014.05.19

Setting up Synology NAS to act as backup destination for Linux using NFS

2014.05.19 Mon

I finally got around to setting up a proper backup solution for my CentOS computers I have at home by setting up script to rsync files to NFS export on my Synology NAS running DSM 5. I would've preferred using something like Automated backup of Linux to Linux and backup files via rsync/ssh as explained in my earlier blog. But configuring Synology to accept incoming files via either rsync or SSH/RSH is a bit of hack. Not hard but you will need to follow direction here which I didn't want to. Using Linux's Smbclient to access Windows file shares on the Synology was another option. I ultimately decided to use NFS file service on Synology which can be enabled very easily with just a few clicks. The more slightly complicated part was setting up the Linux NFS client.

Assumptions

  1. Synology running NFS Server: server.base.loc, IP address: 192.168.120.100. If you are confident the DNS server on your LAN is configured properly, you can use the FQDN to connect to it from the NFS client. If not, you should use the IP.
  2. NFS Client: test100.base.loc, IP address: 192.168.120.200
  3. Replace nfs-test with your own.
  4. Replace nfs-test-synology with your own.

On Synology NAS with DSM 5

Enable NFS service

  1. Control Panel > File Services > NFS Service
  2. Check 'Enable NFS'.
  3. UNcheck 'Enable NFSv4 support'. With this checked I had a few issues.
  4. Check 'Apply default UNIX permission'.

Create a NFS export

  1. Back in 'Control Panel', go to 'Shared Folder'.
  2. Click on Create
  3. Name: nfs-test (replace with your own)
  4. You may check 'Hide this shared folder ...' & 'Hide folders and and files ...'.
  5. OK
  6. Under Permissions > Local users, you may want to give someone Read/Write access. Synology admin group already has Read/Write access.
  7. Under NFS Permissions, click on Create.
  8. For "Hostname or IP: you can use * to allow access from all.
  9. Check 'Enable asynchronous'.
  10. OK
  11. Note the Mount path is /volume1/nfs-test although nfs-test should be replaced with your own.
  12. OK

That's it with setting up NFS service on Synology NAS.

Configure Linux with NFS client setting

  1. Install nfs rpms
  2. [[email protected] ~]# yum install nfs-utils nfs-utils-lib
    
  3. Create a directory to link with the NFS share. I use a bit more descriptive name (instead of something like /mnt/nfs) to show it's for a NFS share (nfs-test) on a Synology NAS. Replace nfs-test-synology with your own.
  4. mkdir -p /mnt/nfs-test-synology
    
  5. Test mounting with 'mount ...' command.
  6. [[email protected] ~]# mount 192.168.120.100:/volume1/nfs-test /mnt/nfs-test-synology
     
    # OR
     
    [[email protected] ~]# mount server.base.loc:/volume1/nfs-test /mnt/nfs-test-synology
  7. Test NFS is mounted with df -h command. Below shows my NFS mount is 7.8 TB in size and 7.2TB is used. So it's working.
  8. [[email protected] ~]# df -h
    Filesystem            Size  Used Avail Use% Mounted on
    /dev/mapper/VolGroup-lv_root
    ...
    /dev/sda1             485M   57M  403M  13% /boot
    192.168.120.100:/volume1/nfs-test
                          7.8T  7.2T  644G  92% /mnt/nfs-test-synology
    
  9. For now umount the NFS mount.
  10. [[email protected] ~]# umount /mnt/nfs-test-synology
    
  11. Before starting to use the NFS export, you need to determine how you will mount the NFS export from the Linux computer.

Two ways for Linux to access NFS export

Now you need to decide how your Linux will mount the NFS export. There are 2 options. Your decision is largely decided by the availability time of the NFS server.

  1. Mount NFS export with a script only when necessary: If your NFS server will be down occasionally and you want to use the NFS export only as a storage to keep backups from the Linux, you can probably live with mounting the NFS export only when you need to, via a shell script. As my project was initially about backing up data from my Linux computers to my Synology NAS via NFS protocol, I chose this option. Even though this sounded simple enough the script turned out to be a little more complicated.
  2. Using /etc/fstab: If you are sure your NFS server will be available 99.9% of the time (like your WiFi Router or internal DNS server), you can edit /etc/fstab to automatically mount the NFS export. You can use the NFS export as an always-available-storage with your Linux computer. But again, choosing this option requires that your NFS server is always available. My own Synology NAS is down sometimes so using /etc/fstab was not a good option for me.

Mount NFS export with a script only when necessary

Following script will exit if any of these tests done in sequence fails: test by pinging & NFS service is running. (Using these tests will prevent Linux NFS client from hanging in case the NFS server is down, however unlikely.) If both pass, it than checks if the NFS export was already mounted or not. If already mounted, rsync will start. If not mounted, it will mount the NFS export and start copying. Lastly, it will umount the NFS export.

  1. Before using the script, you will need to create a hidden file (.nfs-checker-marker) on the NFS server at /volume1/nfs/.nfs-checker-marker. All this files does is allowing NFS client to check if the NFS mount already exists or not.
  2. The script. Be care as the script deletes certain tar.gz files. With a simple typo you can destroy your Linux computer or files on the Synology NAS. So check the script or test in a lab environment. Sorry if syntaxhighlighting may not be working in this example. Copy and paste the code into Sublime or something like it and save the file as backup-2-nfs.sh and you can review the code more easily.
  3. #!/bin/bash
    ###
    # variables
    ###
     
    NOW=$(date +"%Y-%m-%d_%H")
    NOWSTAT=$NOW
    HOST01=`hostname -s`
     
    PingStatus=none  #Ping test
    NFSServiceStatus=none #NFS service available or not test
    NFSMounted=none     #NFS export already mounted or not test
    NFSMountTry=none    #NFS mount attempt success or not test
     
    ###
    # variables that  M U S T  be VERIFIED
    ###
     
    Alert=/root/rootlog.log
     
    # Source and destination folders
    NfsServer="nas.home.loc"
    NfsShare="/volume1/nfs"
    NfsLocal="nfs-nas"
     
    # Backup source
    SRC01="etc"
     
    # For use when purging old backup data
    ToPurge=(/mnt/$NfsLocal/$HOST01/*)
     
    ###
    # script
    ###
    
    # Test pinging the NFS server. If unsuccessful, exit the script.
    /bin/ping -w2 -c1 -q $NfsServer > /dev/null 2>&1
    if [ "$?" -eq "0" ]; then
        PingStatus=yes
    else
        echo "`date`. Critical: $NfsServer is not responding to ping." >> $Alert
        exit 1
    fi
     
    # Test if NFS server is exporting $NfsShare. If not, exit the script.
    /usr/sbin/showmount -e $NfsServer | /bin/grep "${NfsShare}"  > /dev/null 2>&1
    if [ "$?" -eq "0" ]; then
        NFSstatus=yes
    else
        NFSstatus=no
        echo "`date`. Critical: $NfsServer is not exporting $NfsShare." >> $Alert
        exit 1
    fi
     
    # Test if the NFS export is already mounted or not.
    ls -a /mnt/$NfsLocal | /bin/grep .nfs-checker-marker  > /dev/null 2>&1
    if [ "$?" -eq "0" ]; then
        NFSMounted=yes
    else
        NFSMounted=no
    fi
     
    # If already mounted, just start rsyncing files.
    # If not mounted, first mount and start rsyncing files.
    # Note before rsyncing files over, $HOSTNAME/$NOWSTAT/ is copied over to contain backup data.
    # This allows a basic versioning of backup data.
    if [ $NFSMounted == "yes" ]; then
        # Create temp directories to hold data
        mkdir -p /tmp/$HOST01/$NOWSTAT > /dev/null 2>&1
        # Copy space holding folders and files
        /usr/bin/rsync -av /tmp/$HOST01 /mnt/$NfsLocal/
        /usr/bin/rsync -av /tmp/$HOST01/$NOWSTAT /mnt/$NfsLocal/$HOST01/
        /bin/tar -czf /tmp/$SRC01.tar.gz /$SRC01 2>/dev/null;\
        /usr/bin/rsync -a /tmp/$SRC01.tar.gz /mnt/$NfsLocal/$HOST01/$NOWSTAT/  2>/dev/null;\
        rm /tmp/$SRC01.tar.gz  2>/dev/null
        echo "`date`. Info: Rsync to $NfsServer was completed." >> $Alert
    else
        mount $NfsServer:$NfsShare /mnt/$NfsLocal     
        # Create temp directories to hold data
        mkdir -p /tmp/$HOST01/$NOWSTAT > /dev/null 2>&1
        # Copy space holding folders and files
        /usr/bin/rsync -av /tmp/$HOST01 /mnt/$NfsLocal/
        /usr/bin/rsync -av /tmp/$HOST01/$NOWSTAT /mnt/$NfsLocal/$HOST01/
        /bin/tar -czf /tmp/$SRC01.tar.gz /$SRC01 2>/dev/null;\
        /usr/bin/rsync -a /tmp/$SRC01.tar.gz /mnt/$NfsLocal/$HOST01/$NOWSTAT/  2>/dev/null;\
        rm /tmp/$SRC01.tar.gz  2>/dev/null
        echo "`date`. Info: Rsync to $NfsServer was completed." >> $Alert
    fi
     
    # Purge old backups
    for x in ${ToPurge[@]}
    do
        /bin/ls -dt /mnt/$NfsLocal/$HOST01/* | /usr/bin/tail -n +6 | /usr/bin/xargs /bin/rm -rf  #Keep only last 5 backup sets.
    done
     
    # clean up
    rm -rf /tmp/$HOST01
    umount /mnt/$NfsLocal
    exit 0
    
  4. Test running backup-2-nfs.sh by running it manually.
  5. Once it's tested successfully, edit /etc/crontab as root and schedule backup-2-nfs.sh to run daily.

Using /etc/fstab

  1. I ended up not using /etc/fstab method but if you are interested, here it is.
  2. To have the Linux computer mount the NFS mount automatically upon system reboot, you need to edit file /etc/fstab on the Linux NFS client. However I personally do not recommend this. The Linux client could hang if the NFS server happens to go down. Of course this method could be used if you are reasonably sure the NFS server will always be kept running as a DNS server is.
  3. First unmount /mnt/nfs-test-synology if it's still mounted.
  4. [[email protected] ~]# umount /mnt/nfs-test-synology/
    [[email protected] ~]# df -h
    ...
    
  5. As root, edit /etc/fstab and add following line at the end of /etc/fstab. Replace nfs-test & nfs-test-synology with your own. Save the file.
  6. 192.168.120.100:/volume1/nfs-test /mnt/nfs-test-synology nfs users,atime,auto,rw,dev,exec,nfsvers=3,suid 0 0
    
  7. Run df -h to check no NFS mount is there. Issue 'mount -a' command and run df -h again. You should see the NFS mount is now there, with 7.8T addional storage.
  8. [[email protected] ~]# df -h
    Filesystem            Size  Used Avail Use% Mounted on
    /dev/mapper/VolGroup-lv_root
    ...
    /dev/sda1             485M   57M  403M  13% /boot
    [[email protected] ~]#
    [[email protected] ~]# mount -a
    [[email protected] ~]# df -h
    Filesystem            Size  Used Avail Use% Mounted on
    /dev/mapper/VolGroup-lv_root
    ...
    /dev/sda1             485M   57M  403M  13% /boot
    192.168.120.100:/volume1/nfs-test
                          7.8T  7.2T  644G  92% /mnt/nfs-test-synology
    
  9. Now the Linux NFS client will automatically try to mount the NFS export on the Synology upon the Linux's reboot. Or you could mount the NFS export simply by typing 'mount -a' instead of having to remember a long command. But again, using /etc/fstab to auto mount NFS export could cause hanging on the NFS client if the NFS server happens to be down.
  10. Again, use /etc/fstab if you are certain your NFS server will be up 100% of the time.
  11. My preference is using a script as explained above.