How To Wakeup Backup Nas Server and Mirror Files Using Rsync in Linux


I‘ve a critical data stored on my small home server. I backup my Desktop, Laptop and a remote VPS server to my home nas server powered by Debian Linux using rsnapshot backup utility. In the event that my main nas server has a hardware (hard disk failure) problem, having mirrored files provides some sort of peace of mind. How do I mirror /backups/personal (total 100GB and growing everyday) to a secondary nas Linux server located in my network?

Tutorial details
DifficultyIntermediate (rss)
Root privilegesYes
Debain/Ubuntu or Unix-like os
rsync+ssh keys
Estimated completion time20m

You can easily sync your files from main nas server to secondary nas server using the following techniques:

  1. Sends ‘magic packets’ to wake-on-lan (WOL) enabled ethernet adapters and secondary nas server to wake up (switch on server). You need to enable the WOL function in the BIOS and need to run ‘ethtool -s eth0 wol g‘ command on nas02 (see below).
  2. Start mirroring using rsync command.
  3. When job done shutdown the secondary backup nas server (nas02) using shutdown command

In this quick tutorial you will learn how to setup file mirroring using rsync in Debian or Ubuntu Linux.

Our sample setup

 ------------+           +------------ nas01       |           |nas02 main        | <----->   |backup server      |           |server|           | ------------+           +-------------

In this setup,

  • nas01 : This box is powered by Debian Linux. This server is always powered on and it makes hourly, daily, weekly, and monthly backups using rsnapshot command line utility. You must setup rsnapshot on the nas01 server.
  • nas02 : Our secondary backup server. You can run Linux+BTRFS or FreeBSD+ZFS or any other Unix of your choice. I’m simply going to use Debian Linux for demo purpose.


  1. Setup the rsnapshot backup server on nas01 server.
  2. Update /etc/hosts file as follows on both nas01 and nas02 servers:    nas01    nas02
  3. Setup the ssh public key based and password less authentication between nas01 and nas02 servers.
  4. Turn on nas02 server. Go to BIOS. Find power management option. You will have a clear, intuitive option labelled “Wake On LAN”. Turn it on. Next, boot the nas02 server and edit the /etc/network/interfaces file. Update it as follows:
     ## eth0 config with wol setting auto eth0 allow-hotplug eth0 iface eth0 inet static     address     netmask     network     gateway     up /sbin/ethtool -s eth0 wol g

nas01 setup (all commands must run on nas01)

Okay, let us get started with our project.

Step 1: /root/bin/wakeup.nas02

Create a shell script called /root/bin/wakeup.nas02:

 #!/bin/bash # wakeup.nas02 : Wake up nas02 server # Author : under GPL v.2.x+ # ------------------------------------------ # Secondary Server Name/IP address server="nas02"   # Secondary Server Mac  servermac="00:40:63:ff:2d:e6"   # See if the server is alive or sleeping, if sleeping send magic packet to wake the server XD ping -W 1 -c4 -q $  server >&/dev/null [ $  ? -eq 0 ] || /usr/bin/wakeonlan $  servermac >&/dev/null

Step 2: /root/bin/mirror.nas02

Create another shell script called /root/bin/mirror.nas02:

 #!/bin/bash # mirror.nas02 : File mirroring script. Also shutdown the nas02 server when done. # Author : under GPL v.2.x+ # ------------------------------------------ # Secondary server name or IP address t="nas02"   # Backup source on nas01 # Must be same as 'snapshot_root' set in rsnapshot.conf s="/backups/personal"   # Secondary nas02 server backup directory  # I am using BTRFS on nas02 but you can use FreeBSD+ZFS too d="root@$  {t}:/backups/"   # Path _rsync="/usr/bin/rsync" _ssh="/usr/bin/ssh"   # Rsync args  _args='-a -H --progress --delete --numeric-ids --relative --delete-excluded --rsh=/usr/bin/ssh'   # Is the nas02 server alive? If so start mirroring from nas01:$  s ===> nas02:$  d  # And when done shutdown the secondary nas02 server to saver power/electricity bills. ping -W 1 -c4 -q $  t >&/dev/null [ $  ? -eq 0 ] && { $  _rsync $  _args $  {s} $  {d} && $  _ssh root@$  {t} "sync && sync && /sbin/shutdown -h 0"; }

Step 3: Update /etc/rsnapshot.conf file

The default rsnapshot configuration file is located at /etc/rsnapshot.conf. Edit the file and update the following two directives:

   # Specify the path to a script to run right before rsnapshot syncs files # Wake up the nas02 server from sleep i.e. power on the server cmd_preexec     /root/bin/wakeup.nas02   # Specify the path to a script to run right after rsnapshot syncs files # Mirror all nas01 files using this script to nas02 and shutdown nas02 server # when done. cmd_postexec    /root/bin/mirror.nas02

Save and close the file. Run syntax check the config file:
# rsnapshot configtest
Sample outputs:

 Syntax OK

Step 4: Generate an SSH key on nas01 server

Run the following command on nas01 server to generate an SSH key:
# ssh-keygen
Press enter key to accept default and skip password. Next, copy the /root/.ssh/ file to nas02 server, run:
# ssh-copy-id [email protected]
Verify that you can login without passwords:
# ssh [email protected]
# ssh [email protected] date
# ssh [email protected] uptime

Now, we have everything configured and ready to test. See “HowTo: Setup SSH Keys on a Linux / Unix System” for more info.

Step 5: Test it

Type the following command on nas01:
# rsnapshot -vvv hourly

Fig.01: File mirroring in progress

You need to Wait until all files has been synced over by our script. Next, verify that job completed successfully:
# tail -f /var/log/messages
# tail -f /var/log/rsnapshot

Sample outputs:

 Sep 16 23:07:21 nas01 rsnapshot[2531]: /usr/bin/rsnapshot -vvv hourly: completed successfully Sep 17 02:15:27 nas01 rsnapshot[3686]: /usr/bin/rsnapshot -vvv hourly: completed successfully

Step 6: Setup cronjob on nas01

Cron job are used to schedule commands to be executed periodically. You can setup commands or scripts, which will repeatedly run at a set time. I’m going to setup a cronjob to sync my files automatically to nas02. Here is a sample /etc/cron.d/rsnapshot file:

 # This is a sample cron file for rsnapshot. # The values used correspond to the examples in /etc/rsnapshot.conf. # There you can also set the backup points and many other things. # # Feel free to adapt it to your needs.   0 */4       * * *           root    /usr/bin/rsnapshot hourly 30 3        * * *           root    /usr/bin/rsnapshot daily 0  3        * * 1             root    /usr/bin/rsnapshot weekly 30 2        1 * *             root    /usr/bin/rsnapshot monthly


You might also like More from author