Rsync for NAS

This guide assumes you have read the Rsync for Linux or Rsync for Windows guides and you already have a working Rsync data-store and a NAS that you would like to back-up to that data-store.


Enable Rsync on your NAS

The first step in this process is to check if your NAS supports Rsync, and if it does, to enable it. It is assumed you will not set a password, but limit the hosts that can access the Rsync daemon. Please check the documentation for your NAS if you need help. From your Linux workstation (or from within Cygwin), you can check if the NAS is responding by asking it to list its volumes with the command: rsync 192.168.10.240:: (where 192.168.10.240 is the IP address of the NAS. This can also be a hostname.). You may see something like NASA and NASA-snap returned (this will depend on the configuration of your NAS). You can probe for the directory contents of the NAS volume with the command: rsync 192.168.10.240::NASA (for example).


Method 1: LAN back-up

This method describes how to back-up the NAS volume(s) over the Local Area Network. Remember, as we are connecting to the Rsync daemon on the NAS, the data is passed in the clear with no encryption. We assume a script will be run from the master back-up server to pull data from the NAS to the server.

Start with a header describing the script and the last time it was edited. If you are working in a team, ask team-members to add comments if they change the file. Then you know who to blame when it all goes wrong!

#!/bin/bash
#
# Rsync job control file for NAS.
#
# Last updated: 13th December 2012

We now define paths and variables and set the Rsync options. If you are testing, set OPT=vanP to see the progress of a dry-run.

#---------------------------------------------------------------
# Paths, variables and system checks.

# Path to lock file.
#
LOCK_FILE=/var/lock/subsys/nas_rsync

# Options
#
OPT=va

# Paths to back-up directories
#
SNAPSHOT=/backup/snapshot/NAS/

# Check directories exist, if not create them.

# if [ ! -d $SNAPSHOT ]; then
     mkdir -p $SNAPSHOT
fi

We now define the route to the remote host. This can take the form of a hostname or IP address. The double colons (::) tells Rsync to talk to the daemon running on port 873. NASA is the volume we are connecting to in this example.

# Route
#
ROUTE=192.168.10.240::NASA

Now we start the back-up process.

#---------------------------------------------------------------

# Start the back-up process.
#
if [ -f $LOCK_FILE ]; then
     echo "Rsync job already running!"
     exit 1
fi

echo "Rsync job started: `date`"

# Create lock file.
#
touch $LOCK_FILE

# Back-up the NAS
#
rsync -$OPT --delete $ROUTE/* $SNAPSHOT

In the example above, the script picks up every directory on the NAS and compares it to the files in the snapshot - transferring any that are different. You could specify specific directories as in the Rsync for Linux guide.

Tip: You can reverse this process and back-up to a NAS by changing the rsync line to read something like: rsync -$OPT --delete /somedir $ROUTE and changing ROUTE to 192.168.10.240::NASA/backup/ - for example.

The job is now finished, so we remove the lock file and un-set our variables.

echo "Rsync job finished: `date`"


# Job finished, remove lock file.
#
rm -f $LOCK_FILE

# Clean-up.
#
unset SNAPSHOT
unset ROUTE
unset LOCK_FILE
unset OPT

echo "Done!"

exit 0

Putting it all together:

#!/bin/bash
#
# Rsync job control file for NAS.
#
# Last updated: 13th December 2012

#---------------------------------------------------------------
# Paths, variables and system checks.

# Path to lock file.
#
LOCK_FILE=/var/lock/subsys/nas_rsync

# Options
#
OPT=va

# Paths to back-up directories
#
SNAPSHOT=/backup/snapshot/NAS/

# Check directories exist, if not create them.

# if [ ! -d $SNAPSHOT ]; then
     mkdir -p $SNAPSHOT
fi

# Route
#
ROUTE=192.168.10.240::NASA

#---------------------------------------------------------------

# Start the back-up process.
#
if [ -f $LOCK_FILE ]; then
     echo "Rsync job already running!"
     exit 1
fi

echo "Rsync job started: `date`"

# Create lock file.
#
touch $LOCK_FILE

# Back-up the NAS
#
rsync -$OPT --delete $ROUTE/* $SNAPSHOT

echo "Rsync job finished: `date`"


# Job finished, remove lock file.
#
rm -f $LOCK_FILE

# Clean-up.
#
unset SNAPSHOT
unset ROUTE
unset LOCK_FILE
unset OPT

echo "Done!"

exit 0

Method 2: Remote back-up

It is possible to back-up a NAS over the Internet by tunnelling Rsync over SSH. As in the Rsync for Linux guide, you need a Linux system with a working SSH daemon on the same LAN as the NAS in order to be able to use SSH for tunnelling. A VPN is also another method, but it is not explored in this guide. Remember, the connection from the NAS to the local Linux system is not encrypted and the data stream is passed in the clear.

Start with a header describing the script and the last time it was edited. If you are working in a team, ask team-members to add comments if they change the file. Then you know who to blame when it all goes wrong!

#!/bin/bash
#
# Script to rsync back-up NAS at <Remote host>
#
# Last updated: 13th December 2012

We now define paths and variables and set the Rsync options. If you are testing, set OPT=vanP to see the progress of a dry-run.

#---------------------------------------------------------------
# Paths, variables and system checks.

# Path to lock file.
#
LOCK_FILE=/var/lock/subsys/nas_rsync

# Options
#
OPT=va

# Path to back-up
#
SNAPSHOT=/backup/snapshot/NAS/

# Check directories exist, if not create them.
#
if [ ! -d $SNAPSHOT ]; then
     mkdir -p $SNAPSHOT
fi

Now we start the back-up process by establishing a SSH tunnel to the remote host root@host.domain.dot (this assumes SSH keys are present and working on the remote Linux system) by binding the localhost's port 873 (back-up server) to the remote NAS's port 873 on its local LAN IP address (192.168.50.250 in this example). This has the effect of bringing the NAS's Rsync daemon to our local network. The -N option stops SSH from starting an interactive shell and executing remote commands - ideal for port-forwarding. The ampersand puts the SSH connection into its own process and we catch the program ID with PID=$! for later termination of the port-forward.

###################################################################################

# Start the back-up process.
#
if [ -f $LOCK_FILE ]; then
     echo "Rsync job already running!"
     exit 1
fi

echo -e "Rsync job started: `date`\n"

# Create lock file.
#
touch $LOCK_FILE

###################################################################################
# NAS backup
###################################################################################

# Establish a secure tunnel to port 873 on NAS
#
ssh -L localhost:873:192.168.50.250:873 root@host.domain.dot -N &
PID=$!

# Wait for SSH tunnel
#
sleep 10

# Rsync data to snapshot directory
#
rsync -$OPT --delete localhost::NASA/* $SNAPSHOT

###################################################################################

The job is now finished, so we terminate the SSH connection with a SIGTERM, remove the lock file and un-set our variables.

echo -e "\nRsync job finished: `date`"

# Terminate SSH connection
#
kill -15 $PID

# Job finished, remove lock file.
#
rm -f $LOCK_FILE

# Clean-up.
#
unset SNAPSHOT
unset LOCK_FILE
unset OPT
unset PID

echo "Done!"

exit 0

Putting it all together:

#!/bin/bash
#
# Script to rsync back-up NAS at <Remote host>
#
# Last updated: 13th December 2012

#---------------------------------------------------------------
# Paths, variables and system checks.

# Path to lock file.
#
LOCK_FILE=/var/lock/subsys/nas_rsync

# Options
#
OPT=va

# Path to back-up
#
SNAPSHOT=/backup/snapshot/NAS/

# Check directories exist, if not create them.
#
if [ ! -d $SNAPSHOT ]; then
     mkdir -p $SNAPSHOT
fi

###################################################################################

# Start the back-up process.
#
if [ -f $LOCK_FILE ]; then
     echo "Rsync job already running!"
     exit 1
fi

echo -e "Rsync job started: `date`\n"

# Create lock file.
#
touch $LOCK_FILE

###################################################################################
# NAS backup
###################################################################################

# Establish a secure tunnel to port 873 on NAS
#
ssh -L localhost:873:192.168.50.250:873 root@host.domain.dot -N &
PID=$!

# Wait for SSH tunnel
#
sleep 10

# Rsync data to snapshot directory
#
rsync -$OPT --delete localhost::NASA/* $SNAPSHOT

###################################################################################

echo -e "\nRsync job finished: `date`"

# Terminate SSH connection
#
kill -15 $PID

# Job finished, remove lock file.
#
rm -f $LOCK_FILE

# Clean-up.
#
unset SNAPSHOT
unset LOCK_FILE
unset OPT
unset PID

echo "Done!"

exit 0

Cron

As described in the Rsync for Linux guide, you can set the above script(s) to run at a scheduled time by creating a suitable entry for cron.

External Links

Rsync website

Rsync FAQ

Rsync README

Rsync man page

Rsyncd.conf man page

Rsync over SSH

Rsync and Stunnel


Page updated: 10th May 2017