Rsync: Your Reliable Remote Linux VPS Backup Solution
Data backups are critical for organizational resilience. This tutorial demonstrates efficient file backup using the powerful rsync
utility, specifically focusing on backing up a remote Linux VPS to a local system via a simple script. We'll cover both SSH password and SSH key-based authentication methods.
Table of Contents
cron
This tutorial uses a script to back up a remote VPS to a local Debian system. Choose the script variant that best suits your needs.
Create a file named rsync_script_password.sh
with the following content:
#!/bin/bash # Source and destination paths source_directory="/path/to/source" destination_directory="/path/to/destination" # SSH password (**INSECURE - use SSH keys instead**) ssh_password="YourRootPassword" # Retry loop for rsync while true; do 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" if [ $? -eq 0 ]; then echo "Backup complete." break else echo "Backup failed. Retrying in 5 seconds..." sleep 5 fi done
This script automates remote backups using rsync
, handling SSH password authentication and retries.
/path/to/source
and /path/to/destination
with your actual paths.YourRootPassword
with your root password. This is a security risk; use SSH keys (see section 2).
-avz
(archive, verbose, compression), --partial
(resume interrupted transfers), --append
(append to existing files), --progress
(show progress).while
loop ensures the script retries the backup if it fails.Make the script executable:
chmod x rsync_script_password.sh
Execute the script:
./rsync_script_password.sh
Successful completion will display a "Backup complete." message.
Security Note: Using passwords directly in scripts is highly discouraged. SSH keys provide a much more secure alternative.
For enhanced security, use SSH key-based authentication.
[Link to SSH key setup instructions] (Replace this with a link to a relevant tutorial)
Create rsync_script_key.sh
with this content:
#!/bin/bash # Source and destination paths source_directory="/path/to/source" destination_directory="/path/to/destination" # Path to your private key private_key="/path/to/your/private/key" # Retry loop for rsync while true; do rsync -avz --partial --append --progress -e "ssh -i '$private_key' -p 2200 -o StrictHostKeyChecking=no" root@your_remote_vps_ip:"$source_directory" "$destination_directory" if [ $? -eq 0 ]; then echo "Backup complete." break else echo "Backup failed. Retrying in 5 seconds..." sleep 5 fi done
Replace /path/to/your/private/key
with the path to your private key file.
Make the script executable:
chmod x rsync_script_key.sh
Execute the script:
./rsync_script_key.sh
cron
Use cron
to schedule automated backups.
crontab -e
0 2 * * * /path/to/your/script.sh
(Replace /path/to/your/script.sh
with the path to your chosen script).crontab -l
.Regular backups are essential for data protection. rsync
provides a robust and efficient solution, and using SSH keys significantly enhances security. Remember to schedule your backups using cron
for automated protection.
The above is the detailed content of How To Backup Files From Remote Linux VPS Using Rsync Script. For more information, please follow other related articles on the PHP Chinese website!