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
Difficulty level Intermediate
Root privileges Yes
Requirements rsnahshot,ethtool
Debain/Ubuntu or Unix-like osrsync+ssh keys
Est. reading time 20m
[/donotprint]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
        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:

# wakeup.nas02 : Wake up nas02 server
# Author : under GPL v.2.x+
# ------------------------------------------
# Secondary Server Name/IP address
# Secondary Server Mac 
# 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:

# mirror.nas02 : File mirroring script. Also shutdown the nas02 server when done.
# Author : under GPL v.2.x+
# ------------------------------------------
# Secondary server name or IP address
# Backup source on nas01
# Must be same as 'snapshot_root' set in rsnapshot.conf
# Secondary nas02 server backup directory 
# I am using BTRFS on nas02 but you can use FreeBSD+ZFS too
# Path
# 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 root@nas02
Verify that you can login without passwords:
# ssh root@nas02
# ssh root@nas02 date
# ssh root@nas02 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

See “HowTo: Add Jobs To cron Under Linux or UNIX?” for more info.

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 1 comment so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersdf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
1 comment… add one
  • Tom Sep 17, 2015 @ 9:27

    You may wanna add sleep command before control return back to rsnapshot. This ensures that server get booted fully. For example, pause execution for a minimum 120 seconds one can add:

    [ $? -eq 0 ] || { /usr/bin/wakeonlan $servermac >&/dev/null; sleep 120; }

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum