Bash Script for Remote Backup Using rsync Command
It is a good practice to take regular backups of your important data.
It allows you to restore it whenever you require it.
If the system crashes, you will not be able to restore the backup as needed even if you have a backup.
So, what is the best solution to overcome this problem?
In this situation, remote backup is a better solution.
Rsync is a great Linux command to perform a remote backup.
We have added many useful shell scripts in the past. If you want to check them out, go to the link below.
What is the rsync Command?
Rsync stands for remote synchronization. Rsync is a fast and extraordinarily versatile file copying tool. It is used for fast and incremental file transfer. Rsync is widely used for backups and mirroring purpose.
rsync is typically used to synchronize files and directories between two different systems or local directory.
It uses delta-transfer algorithm, which reduces the amount of data sent over the network by transferring only changed files to destination that has changed since the last backup.
It offers a wide range of options that control every aspect of its behavior and permit very flexible specification of the set of files to be copied.
Step-1: Create a Local Backup of Your Data Using tar Command
First you need to create a local backup of your important data. This was already described in our previous article.
Go to the following link to learn how to backup the home directory of users on Linux using the tar command.
Step-2: Set up Passwordless SSH Authentication
Since you plan to move the backup to a remote (offsite) server, it is mandatory to set up a passwordless ssh authentication to automate this task.
Otherwise, you need to perform this operation manually because the remote server will ask for the password every time you run the script.
Step-3: Create a Bash Script for Remote Backup Using rsync Command
We have included five scripts in this tutorial. Each script is written for different purposes and choose the corresponding script according to your need.
Bash Script-1: Move a Backup to the Remote Server with Standard SSH Port using rsync Command
Use the following bash script to move your backup to the remote server, if you use the standard ssh port on it.
# /opt/scripts/remote-backup-1.sh #!/bin/bash #Remote Server IP rserver=192.168.1.5 #Local backup location lbackuploc=/home/daygeek/Downloads/test/ #Remote backup location rbackuploc=/home/daygeek/site #rsync command with standard port rsync -avz -e ssh $lbackuploc $rserver:$rbackuploc #To delete files older than 10 days find $rbackuploc/* -mtime +10 -exec rm {} \;
Bash Script-2: Move a Backup to the Remote Server with Non-Standard SSH Port using rsync Command
Use the following bash script to move your backup to the remote server, if you use the non-standard ssh port on it.
# /opt/scripts/remote-backup-2.sh #!/bin/bash #Remote Server IP rserver=192.168.1.5 #SSH Non-Stardard port number snsport=2200 #Local backup location lbackuploc=/home/daygeek/Downloads/test/ #Remote backup location rbackuploc=/home/daygeek/site #rsync command with non-standard port rsync -avz -e "ssh -p $snsport" $lbackuploc $rserver:$rbackuploc #To delete files older than 10 days find $rbackuploc/* -mtime +10 -exec rm {} \;
Bash Script-3: Move a Backup to Multiple Remote Server with Standard SSH Port using rsync Command
Use the following bash script to move your backup to multiple remote server, if you use the standard ssh port on it.
# /opt/scripts/remote-backup-3.sh #!/bin/bash #Local backup location lbackuploc=/home/daygeek/Downloads/test/ #Remote backup location rbackuploc=/home/daygeek/site for rserver in 192.168.1.5 192.168.1.8 do rsync -avz -e ssh $lbackuploc $rserver:$rbackuploc find $rbackuploc/* -mtime +10 -exec rm {} \; done
Bash Script-4: Move a Backup to Multiple Remote Server with Non-Standard SSH Port using rsync Command
Use the following bash script to move your backup to multiple remote server, if you use the non-standard ssh port on it.
# /opt/scripts/remote-backup-4.sh #!/bin/bash #SSH Non-Stardard port number snsport=2200 #Local backup location lbackuploc=/home/daygeek/Downloads/test/ #Remote backup location rbackuploc=/home/daygeek/site for rserver in 192.168.1.5 192.168.1.8 do rsync -avz -e "ssh -p $snsport" $lbackuploc $rserver:$rbackuploc find $rbackuploc/* -mtime +10 -exec rm {} \; done
Bash Script-5: Move a Backup to Multiple Remote Server with Standard and Non-Standard SSH Port using rsync Command
Use the following bash script to move your backup to multiple remote server regardless of the ssh port.
# /opt/scripts/remote-backup-5.sh #!/bin/bash #SSH Non-Stardard port number snsport=2200 #Local backup location lbackuploc=/home/daygeek/Downloads/test/ #Remote backup location rbackuploc=/home/daygeek/site for rserver in 192.168.1.5 192.168.1.8 do ssport=$(nmap -Pn $rserver | grep -w ssh | awk '{print $1}' | sed "s/\/tcp//") if [ "$ssport" == "22" ] then echo "$rserver" rsync -avz -e ssh $lbackuploc $rserver:$rbackuploc find $rbackuploc/* -mtime +10 -exec rm {} \; else echo "$rserver" rsync -avz -e "ssh -p $snsport" $lbackuploc $rserver:$rbackuploc find $rbackuploc/* -mtime +10 -exec rm {} \; fi done
Set an executable permission to remote-backup-5.sh
file.
# chmod +x remote-backup-5.sh
Finally run the script to achieve this.
# sh remote-backup-5.sh 192.168.1.5 sending incremental file list ./ wordpress-5.2.zip sent 11,774,912 bytes received 38 bytes 7,849,966.67 bytes/sec total size is 12,114,587 speedup is 1.03 192.168.1.8 sending incremental file list ./ wordpress-5.2.zip sent 11,774,905 bytes received 41 bytes 7,849,964.00 bytes/sec total size is 12,114,587 speedup is 1.03
If you want to ensure backup status, use the following bash script.
# /opt/scripts/list-remote-backup.sh #!/bin/bash #User Name username=daygeek #SSH Non-Stardard port number snsport=2200 #Remote backup location rbackuploc=/home/daygeek/site for rserver in 192.168.1.5 192.168.1.8 do ssport=$(nmap -Pn $rserver | grep -w ssh | awk '{print $1}' | sed "s/\/tcp//") if [ "$ssport" == "22" ] then echo "$rserver" echo "-----------" ssh $username@$rserver "ls -lh $rbackuploc" else echo "$rserver" echo "-----------" ssh $username@$rserver -p "$snsport" "ls -lh $rbackuploc" fi done
Run the script to list the synced backup files.
# sh list-remote-backup.sh 192.168.1.5 ----------- total 12M -rw-r--r-- 1 daygeek daygeek 12M May 20 02:42 wordpress-5.2.zip 192.168.1.8 ----------- total 12M -rw-r--r--. 1 daygeek daygeek 12M May 19 14:12 wordpress-5.2.zip
Step-4: Automate a Backup Using crontab
To fully automate this, finally add the script to the crontab.
Use the cronjob to schedule work at your convenient time. I added a cron job to run the script at 8am every day.
0 8 * * * /opt/scripts/remote-backup-5.sh