Backing up your VPS regularly is crucial to prevent data loss due to system failures, accidental deletions, or security breaches. This guide will show you how to automatically back up your VPS to a remote server using Rsync.
📌 Prerequisites
-
A Linux VPS (Ubuntu/Debian/CentOS)
-
A remote backup server with SSH access
-
Root or sudo privileges
1️⃣ Install Rsync
Most Linux distributions come with Rsync pre-installed. To ensure it’s available, run:
For Ubuntu/Debian:
sudo apt install rsync -y
For CentOS/RHEL:
sudo yum install rsync -y
2️⃣ Manually Back Up Your VPS Using Rsync
Before automating backups, test Rsync with a manual backup.
Run the following command to back up /var/www/
(your website files) to a remote server:
rsync -avz --delete /var/www/ user@backup-server:/backup/
-
-a
: Archive mode (preserves file permissions and structure) -
-v
: Verbose (shows progress) -
-z
: Compresses files during transfer -
--delete
: Removes files on the backup server that no longer exist on the VPS
✅ If successful, your files will be copied to the remote server.
3️⃣ Set Up SSH Key Authentication (Optional, But Recommended)
To automate backups without entering a password each time, set up SSH key authentication.
Step 1: Generate an SSH Key on Your VPS
ssh-keygen -t rsa -b 4096
Press Enter for all prompts.
Step 2: Copy the Key to the Backup Server
ssh-copy-id user@backup-server
✅ Now, Rsync will work without prompting for a password.
4️⃣ Automate Backups with Cron Jobs
To schedule backups, use a cron job.
Step 1: Open the Crontab
crontab -e
Step 2: Add a Backup Schedule
For daily backups at midnight, add this line:
0 0 * * * rsync -avz --delete /var/www/ user@backup-server:/backup/
✅ This will back up your /var/www/
directory every night at midnight.
5️⃣ Verify and Restore Backups
To check if backups were successful:
ls -lah /backup/
To restore a backup:
rsync -avz user@backup-server:/backup/ /var/www/
✅ Your files will be restored from the last backup.
🎉 Conclusion
You’ve now set up automated VPS backups using Rsync! Your data will be securely stored on a remote server, reducing the risk of loss. 🚀
For additional support, contact Voltichost Support.