How to create an Automatic Linux Server Backup System to Cloud Storage using rsync, rclone and Cron

Configurare noua (How To)

Situatie

Solutie

Step 1 – Install Required Packages

Update the system and install the tools.

sudo apt update
sudo apt install rsync rclone

For CentOS/RHEL:

sudo yum install rsync rclone

Step 2 – Configure Cloud Storage Access

Run the rclone configuration wizard.

rclone config

Configuration steps:

  1. Choose n for new remote

  2. Enter a name for the remote (example: cloud-backup)

  3. Select the cloud provider (Google Drive, S3, etc.)

  4. Complete authentication

Test the connection:

rclone ls cloud-backup:

If files are listed, the connection works correctly.

Step 3 – Create a Local Backup Directory

Create a directory where backups will be temporarily stored.

sudo mkdir /backup

Create a basic backup structure.

sudo mkdir -p /backup/daily
sudo mkdir -p /backup/weekly
sudo mkdir -p /backup/monthly

Example structure:

/backup
├── daily
├── weekly
└── monthly

Step 4 – Create the Backup Script

Create a script that performs the backup and uploads it to cloud storage.

sudo nano /usr/local/bin/auto-backup.sh

Add the following content:

#!/bin/bash

DATE=$(date +%Y-%m-%d)

SOURCE=“/var/www”
BACKUP_DIR=“/backup/daily/$DATE

mkdir -p $BACKUP_DIR

rsync -av –delete $SOURCE $BACKUP_DIR

rclone sync $BACKUP_DIR cloud-backup:server-backups/$DATE

Explanation:

  • rsync performs an incremental backup

  • rclone uploads the backup to cloud storage

  • backups are organized by date

Save and exit the editor.

Step 5 – Make the Script Executable

Give execution permission to the script.

sudo chmod +x /usr/local/bin/auto-backup.sh

Test the script manually.

/usr/local/bin/auto-backup.sh

Verify that the backup directory is created and files appear in cloud storage.

Step 6 – Automate the Backup with Cron

Open the cron scheduler.

crontab -e

Add the following line:

0 2 * * * /usr/local/bin/auto-backup.sh

This means:

  • the backup runs every day at 02:00 AM

Step 7 – Remove Old Backups Automatically

To avoid filling up disk space, add a cleanup command to the script.

Edit the script again.

sudo nano /usr/local/bin/auto-backup.sh

Add this line at the end:

find /backup/daily/* -mtime +7 -exec rm -rf {} \;

This removes backups older than 7 days.

Step 8 – Verify Backup Integrity

You can periodically verify that backups are correct.

Check cloud synchronization:

rclone check /backup cloud-backup:server-backups

Optional checksum verification:

sha256sum backupfile.tar

Step 9 – Enable Logging

Logging helps diagnose problems if backups fail.

Modify the cron job:

0 2 * * * /usr/local/bin/auto-backup.sh >> /var/log/backup.log 2>&1

Now all output and errors will be saved in:

/var/log/backup.log

View logs with:

tail -f /var/log/backup.log

The system now:

  1. performs automated daily backups

  2. stores them locally and in cloud storage

  3. deletes backups older than 7 days

  4. records activity in log files

This provides a simple but reliable automated backup infrastructure for Linux servers.

Tip solutie

Permanent

Voteaza

(0 din 0 persoane apreciaza acest articol)

Despre Autor

Leave A Comment?