[donotprint]
Tutorial details | |
---|---|
Difficulty | Intermediate (rss) |
Root privileges | Yes |
Requirements | rsnahshot,ethtool Debain/Ubuntu or Unix-like osrsync+ssh keys |
Time | 20m |
- 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).
- Start mirroring using rsync command.
- 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 192.168.1.10| |192.168.1.11 ------------+ +-------------
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.
Prerequisites
- Setup the rsnapshot backup server on nas01 server.
- Update /etc/hosts file as follows on both nas01 and nas02 servers:
192.168.1.10 nas01 192.168.1.11 nas02
- Setup the ssh public key based and password less authentication between nas01 and nas02 servers.
- 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 192.168.1.11 netmask 255.255.255.0 network 192.168.1.0 gateway 192.168.1.254 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 : www.cyberciti.biz 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 : www.cyberciti.biz 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/id_rsa.pub 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
# 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.
🐧 1 comment so far... add one ↓
Category | List of Unix and Linux commands |
---|---|
File Management | cat |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Network Utilities | dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • jobs • killall • kill • pidof • pstree • pwdx • time |
Searching | grep • whereis • which |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |
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: