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
-
- Rsync Backup Script (SSH Password Authentication)
- 1.1. Script Breakdown
- 1.2. Setting Script Permissions
- 1.3. Running the Script
-
- 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
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
1.3. Running the Script
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.
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
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
2.4. Running the Script
Execute the script:
./rsync_script_key.sh
Automating Backups with cron
Use cron
to schedule automated backups.
-
Edit Crontab:
crontab -e
-
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). -
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!

Linux'scommandlinecanbemoresecurethanWindowsifmanagedcorrectly,butrequiresmoreuserknowledge.1)Linux'sopen-sourcenatureallowsforquicksecurityupdates.2)Misconfigurationcanleadtovulnerabilities.Windows'commandlineismorecontrolledbutlesscustomizable,with

This guide explains how to automatically mount a USB drive on boot in Linux, saving you time and effort. Step 1: Identify Your USB Drive Use the lsblk command to list all block devices. Your USB drive will likely be labeled /dev/sdb1, /dev/sdc1, etc

Cross-platform applications have revolutionized software development, enabling seamless functionality across operating systems like Linux, Windows, and macOS. This eliminates the need to switch apps based on your device, offering consistent experien

Artificial Intelligence (AI) is rapidly transforming numerous sectors, from healthcare and finance to creative fields like art and music. Linux, with its open-source nature, adaptability, and performance capabilities, has emerged as a premier platfo

Looking for a fast, minimal, and efficient Linux distribution without a graphical user interface (GUI)? Lightweight, GUI-less Linux distros are perfect for older hardware or specialized tasks like servers and embedded systems. They consume fewer res

Wine 10.0 stable version release: Running Windows applications on Linux to a higher level Wine, this open source and free application, allows Linux users to run Windows software and games on Unix/Linux operating systems, ushering in the release of the 10.0 stable version! This version has been provided with source code and binary package downloads, and supports various distributions such as Linux, Windows and Mac. This edition embodies a year of hard work and over 8,600 improvements, bringing many exciting improvements. Key highlights include: Enhanced support for Bluetooth devices. Improve support for HID input devices. Optimized performance of 32-bit and 64-bit applications.

This tutorial guides you through installing SQL Server 2022 on RHEL 8.x or 9.x, connecting via the sqlcmd command-line tool, database creation, and basic querying. Prerequisites Before beginning, ensure: A supported RHEL version (RHEL 8 or 9). Sudo

Mozilla Thunderbird 135: Powerful cross-platform mail client Mozilla Thunderbird is a free, open source, cross-platform email, calendar, news, chat and contact management client designed to efficiently handle multiple email accounts and news sources. On February 5, 2025, Mozilla released the Thunderbird 135 version, introducing a number of new features, performance improvements and security fixes. Thunderbird 135 main features: XZ Packaging for Linux Binaries: Smaller files, faster unpacking, and better integration with modern distributions. Cookie storage support: when creating space


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Chinese version
Chinese version, very easy to use

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.
