Backups are the backbone of an organization. One of the efficient way to back up your files is to use the Rsync utility. Rsync is a powerful tool for efficiently transferring files between computers over a network. In this tutorial, you will learn how to backup files from a remote Linux system (VPS) to a local system using a simple rsync script. You will also learn how to use the script with both SSH password-based authentication and SSH key-based authentication.
Table of Contents
Backup Remote Linux VPS with Rsync
I regularly use this script to back up files from a remote VPS to my local Debian desktop. If you're seeking a straightforward script to back up your remote Linux VPS with Rsync, you can choose from any of the following script variants.
1. Rsync Backup Script for SSH Password-based Authentication
Create a text file, for example "rsync_script_with_ssh_password.sh
", with the following contents.
#!/bin/bash # Define source and destination directories source_directory="/path/to/source" destination_directory="/path/to/destination" # Define the SSH password ssh_password="YourRootPassword" # Loop until rsync completes successfully while true; do # Perform the rsync operation with SSH password authentication and progress display rsync -avz --partial --append --progress -e "sshpass -p '$ssh_password' ssh -p 2200 -o StrictHostKeyChecking=no" root@your_remote_vps_ip:"$source_directory" "$destination_directory" # Check the exit status of rsync if [ $? -eq 0 ]; then echo "rsync completed successfully." break # Exit the loop if rsync is successful else echo "rsync failed or was interrupted. Retrying in 5 seconds..." sleep 5 # Wait for 5 seconds before retrying fi done
1.1. Script Explanation
This script automates the process of using the rsync
command to copy files and directories from a remote server to a local system, while also handling SSH password authentication and retrying the rsync
operation if it fails.
Here's a breakdown of the script:
1. Define Source and Destination Directories:
source_directory="/path/to/source" destination_directory="/path/to/destination"
These variables store the source and destination directories for the rsync
operation. You should replace them with the actual paths you want to use.
2. Define the SSH Password:
ssh_password="YourRootPassword"
This variable holds the SSH password for authentication when connecting to the remote server. Replace "YourRootPassword"
with your actual root password.
3. Loop Until rsync
Completes Successfully:
while true; do
This starts an infinite loop that will keep running until the rsync
operation completes successfully. It allows the script to retry the operation if it fails.
4. Perform the rsync
Operation:
rsync -avz --partial --append --progress -e "sshpass -p '$ssh_password' ssh -p 2200 -o StrictHostKeyChecking=no" root@your_vps_ip:"$source_directory" "$destination_directory"
This line executes the rsync
operation with the following options and configurations:
-avz
: Archive mode with verbose output and compression.--partial
: Allows resuming partial transfers.--append
: Appends data to files instead of starting over.--progress
: Displays progress information.-e "sshpass -p '$ssh_password' ssh -p 2200 -o StrictHostKeyChecking=no"
: Specifies the SSH command and options, including password authentication usingsshpass
. Replace the ssh port number2200
with your own.
5. Check the Exit Status of rsync
:
if [ $? -eq 0 ]; then echo "rsync completed successfully." break # Exit the loop if rsync is successful else echo "rsync failed or was interrupted. Retrying in 5 seconds..." sleep 5 # Wait for 5 seconds before retrying fi
After the rsync
operation, this section checks the exit status of rsync
. If the exit status is 0 (indicating success), it prints a success message and exits the loop. If rsync
fails (non-zero exit status), it prints an error message, waits for 5 seconds, and then retries the rsync
operation.
6. Remove the SSH Key from the Agent (If Using SSH Keys):
ssh-agent -k
If you were using SSH keys for authentication and added the SSH key to the agent at the beginning of each loop iteration, this line removes the SSH key from the agent to ensure it's not left behind.
This script essentially wraps the rsync
operation in a retry loop, handling password-based SSH authentication and displaying progress information. It continues to retry the operation until it succeeds or until you manually terminate the script.
1.2. Make the Script Executable
Run the following command to make the script executable:
$ chmod +x rsync_script_with_ssh_password.sh
1.3. Start the Script
Run the script using command:
$ ./rsync_script_with_ssh_password.sh
This will take some time depending upon the size of the data and the network speed.
Upon successful completion, you will see an output something like below:
receiving incremental file list ./ ostechnix.tar.gz 11,151,804,222 100% 259.80kB/s 3:25:11 (xfr#1, to-chk=0/3) sent 46 bytes received 3,271,217,407 bytes 265,445.49 bytes/sec total size is 11,151,804,817 speedup is 3.41 rsync completed successfully.
Remember that using passwords in scripts can pose a security risk, so it's important to take precautions to protect your scripts and credentials. Additionally, consider using SSH key-based authentication for enhanced security when working with remote servers.
2. Rsync Backup Script for SSH Key-based Authentication
For enhanced security and the convenience of passwordless access, our second script leverages SSH key-based authentication.
2.1. Configure SSH Key-based Authentication
First of all, you need to configure SSH key-based authentication (aka SSH password-less authentication. If you're not sure how to do it, refer the link given below.
2.2. Create Rsync Script
If you want to modify the script for SSH key-based authentication instead of password-based authentication, you can do so by removing the SSH password-related code. Specifically, you can remove the sshpass
part of the rsync
command and the associated ssh_password
variable.
The following is the modified script for SSH key-based authentication.
Create a text file (E.g. rsync_script_with_ssh_key
) with the following contents.
#!/bin/bash # Define source and destination directories source_directory="/path/to/source" destination_directory="/path/to/destination" # Loop until rsync completes successfully while true; do # Perform the rsync operation with SSH key-based authentication and progress display rsync -avz --partial --append --progress -e "ssh -i /path/to/your/private/key -p 2200 -o StrictHostKeyChecking=no" root@your_vps_ip:"$source_directory" "$destination_directory" # Check the exit status of rsync if [ $? -eq 0 ]; then echo "rsync completed successfully." break # Exit the loop if rsync is successful else echo "rsync failed or was interrupted. Retrying in 5 seconds..." sleep 5 # Wait for 5 seconds before retrying fi done
In this modified script:
- The
sshpass
command andssh_password
variable have been removed. - The
rsync
command uses the-i
option to specify the path to your private key for SSH key-based authentication.
Make sure to replace /path/to/your/private/key
with the actual path to your private key. This script will use SSH key-based authentication to connect to your VPS, making it more secure and convenient when compared to password-based authentication.
2.3. Make the Script Executable
Run the following command to make the script executable:
$ chmod +x rsync_script_with_ssh_key.sh
2.4. Start the Script
Run the script using command:
$ ./rsync_script_with_ssh_key.sh
Depending upon the network speed and the size of the data, the file transfer may take some time.
Schedule Backup using cron
You can schedule the script to run at a specific time using the cron utility on Linux. cron
allows you to automate the execution of tasks, including running scripts, at predefined intervals or specific times. Here's how you can schedule your script to run at a specific time:
1. Open the Crontab Configuration:
Open your user's crontab
configuration for editing. You can do this by running the following command:
$ crontab -e
This will open the crontab
configuration file in your default text editor.
2. Add the Scheduled Task:
In the crontab
file, add a line to specify when and how often you want the script to run. The format of a cron
job line is as follows:
* * * * * /path/to/script.sh
Here's what each field means:
- The first field (minutes) can be a number from 0 to 59.
- The second field (hours) can be a number from 0 to 23.
- The third field (days of the month) can be a number from 1 to 31.
- The fourth field (months) can be a number from 1 to 12 or a three-letter abbreviation (e.g.,
Jan
,Feb
, etc.). - The fifth field (days of the week) can be a number from 0 to 7, where both 0 and 7 represent Sunday, or a three-letter abbreviation (e.g.,
Sun
,Mon
, etc.).
For instance, to run your script every day at 2:00 AM, you can add the following line to the crontab
file:
0 2 * * * /path/to/your/script.sh
Make sure to replace /path/to/your/script.sh
with the actual path to your script.
3. Save and Exit:
Save the crontab
file and exit your text editor.
4. Verify the Scheduled Task:
To verify that your cron
job has been added, you can list your user's cron
jobs with the following command:
crontab -l
This will display the list of scheduled tasks for your user.
Your script will now run automatically at the specified time every day. You can customize the schedule by adjusting the values in the cron
job line according to your needs.
Conclusion
Backups are essential for any organization looking to safeguard its data and ensure the continuity of its business. Fortunately, rsync
has got you covered, offering a reliable means to protect your crucial data.
If you haven't backed up your system yet, it is highly recommended to do so now, before any unexpected events occur.