Home > System Tutorial > LINUX > How To Backup Files From Remote Linux VPS Using Rsync Script

How To Backup Files From Remote Linux VPS Using Rsync Script

William Shakespeare
Release: 2025-03-21 09:29:13
Original
808 people have browsed it

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

    1. Rsync Backup Script (SSH Password Authentication)
    • 1.1. Script Breakdown
    • 1.2. Setting Script Permissions
    • 1.3. Running the Script
    1. Rsync Backup Script (SSH Key-based Authentication)
    • 2.1. Configuring SSH Key-based Authentication
    • 2.2. Creating the Rsync Script
    • 2.3. Setting Script Permissions
    • 2.4. Running the Script
  • Automating Backups with cron
  • Summary

Backing Up Your Remote Linux VPS with Rsync

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.

1. Rsync Backup Script (SSH Password Authentication)

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
Copy after login

1.1. Script Breakdown

This script automates remote backups using rsync, handling SSH password authentication and retries.

  • Source/Destination Paths: Replace /path/to/source and /path/to/destination with your actual paths.
  • SSH Password: Replace YourRootPassword with your root password. This is a security risk; use SSH keys (see section 2).
  • rsync Options: -avz (archive, verbose, compression), --partial (resume interrupted transfers), --append (append to existing files), --progress (show progress).
  • Retry Loop: The while loop ensures the script retries the backup if it fails.

1.2. Setting Script Permissions

Make the script executable:

chmod  x rsync_script_password.sh
Copy after login

1.3. Running the Script

Execute the script:

./rsync_script_password.sh
Copy after login

Successful completion will display a "Backup complete." message.

How To Backup Files From Remote Linux VPS Using Rsync Script

Security Note: Using passwords directly in scripts is highly discouraged. SSH keys provide a much more secure alternative.

2. Rsync Backup Script (SSH Key-based Authentication)

For enhanced security, use SSH key-based authentication.

2.1. Configuring SSH Key-based Authentication

[Link to SSH key setup instructions] (Replace this with a link to a relevant tutorial)

2.2. Creating the Rsync Script

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
Copy after login

Replace /path/to/your/private/key with the path to your private key file.

2.3. Setting Script Permissions

Make the script executable:

chmod  x rsync_script_key.sh
Copy after login

2.4. Running the Script

Execute the script:

./rsync_script_key.sh
Copy after login

Automating Backups with cron

Use cron to schedule automated backups.

  1. Edit Crontab: crontab -e
  2. Add Schedule: Add a line like this (runs daily at 2 AM): 0 2 * * * /path/to/your/script.sh (Replace /path/to/your/script.sh with the path to your chosen script).
  3. Save and Verify: Save the crontab file and verify with crontab -l.

Summary

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template