HomeLinuxStep-by-Step Guide to Setup a Linux VPS Backup Server using Rsync

Step-by-Step Guide to Setup a Linux VPS Backup Server using Rsync

Creating regular backups for your Linux-based VPS is crucial for safeguarding your data and configurations. This tutorial walks you through the process of configuring a simple backup system with the useful and effective file synchronization program rsync. There are other file synchronization programs also for this function such as fwbackups, Duplicity and Bacula but for this tutorial we use rsync for its simplicity of use.

In this tutorial, we will be using another Linux-based VPS for backup storage. Alternatively, you can consider using a different service such as cloud services like Amazon S3, Google Cloud Storage, or external servers depending on your choice.

Prerequisite

Install rsync on both devices:
You can install it on a Debian/Ubuntu system using the following command:

sudo apt update
sudo apt install rsync

For RedHat/Centos system:

sudo yum install rsync

After installation, ensure that the rsync service is enabled and running:

sudo systemctl enable rsync
sudo systemctl start rsync
sudo systemctl status rsync

(rsync is usually pre-installed on most Linux distributions, but it’s always good to make sure you have the latest version)

Install Source Server SSH-Key on the Backup Server:
You can generate a key on your source server using the following command:

ssh-keygen -t rsa

(Press Enter to accept the default file location and passphrase)

After generating the key, copy the public key to the backup server. For that execute the following command replacing the username and IP address on your source server:

ssh-copy-id username@backup_server_ip

(Follow the prompts to complete the copy)

From the source server, try to SSH into the backup server to ensure the key is working!

Reference: https://www.veeble.org/kb/ssh-login-without-password/

Create and Configure the Rsync Script

A sample script, backup_script.sh, is provided for synchronization. Customize paths, usernames, and IP addresses as needed. The script logs actions, checks Rsync’s exit status, and deletes old backups based on a specified retention period.

Example Script:

This script facilitates the synchronization of the “/path/to/source/” directory on your VPS with the “/path/to/backup/” directory on the backup server. Ensure to replace placeholders with precise values for the source and backup paths, username, and IP address of the backup server.

The script meticulously logs every action it takes, providing a straightforward means for debugging and monitoring the backup process. It verifies the exit status of rsync, logging any failures if the status is not equal to 0.

To enhance tracking, the script incorporates the date and hostname in its logs, which proves especially useful when managing multiple servers.

Additionally, the script utilizes the find command to locate and remove files older than a specified retention period (in this instance, 30 days). The “-mtime +$RETENTION_DAYS” option in find identifies files modified more than $RETENTION_DAYS days ago, while the “-type f -delete” options instruct find to delete these identified files.

Make the Script Executable:

Set the script as executable with the command:

chmod +x ~/backup_script.sh

Reference: https://www.veeble.org/kb/linux-file-permissions/

Automating Task Execution with Cron

To create a cron job, execute the following command to open the cron job editor:

crontab -e

Add or modify the line for your backup schedule:

0 1 * * * /home/yourusername/backup_script.sh

Refer https://www.veeble.org/kb/cron-job-how-to-schedule-recurring-user-jobs/ for modifying cron according to your choice.

To confirm the addition of your cron job, you can view a list of your user’s scheduled tasks using the following command:

crontab -l

Your script is set to execute automatically at the designated time specified in the cron job.

Notes

  • Regularly check rsync_backup.log to make sure there are no errors.
  • Verify that the backup server is receiving the files as expected.
  • The script executes the find command on the backup server via SSH. Ensure that the SSH user (backup_user) has the necessary permissions to delete these backup files.
  • This approach assumes that your backups are stored in a way that each backup is a separate file and that these files have modification dates that reflect their backup dates. If your backup structure is different (e.g., using directories for each backup), you’ll need to modify the deletion command accordingly.

With this guide, you’re well-equipped to set up a secure and efficient backup system using Rsync for your Linux VPS, ensuring your data is reliably backed up.

Scroll to Top