Server Management

Automatic Backups with rclone

How to configure automatic backups to cloud storage (Backblaze B2, S3, Google Drive) using rclone

rclone is a tool to synchronize files to over 70 cloud providers. Ideal for offsite automatic backups of your VPS.


Installation

# Recommended method (always downloads latest version)
curl https://rclone.org/install.sh | sudo bash

# Verify
rclone version

Configure a cloud provider

rclone config

Follow the interactive wizard. Examples for the most common providers:

  1. n → new remote
  2. Name: b2backup
  3. Storage: 5 (Backblaze B2)
  4. Account ID: enter your Application Key ID
  5. Application Key: enter your Application Key
  6. Done

Amazon S3 / Wasabi / MinIO

  1. n → new remote
  2. Name: s3backup
  3. Storage: 5 (Amazon S3 and compatible)
  4. Provider: AWS / Wasabi / Other
  5. Access Key ID and Secret Access Key
  6. Region: enter the region (e.g. eu-central-1)

Google Drive

  1. n → new remote
  2. Name: gdrive
  3. Storage: 17 (Google Drive)
  4. Follow OAuth authentication in browser

Basic commands

# List files on a remote
rclone ls b2backup:bucket-name/

# Copy local files → cloud
rclone copy /var/www/html b2backup:my-backup/www

# Sync (synchronizes, deletes files no longer present locally)
rclone sync /var/www/html b2backup:my-backup/www

# Copy with progress
rclone copy /var/www b2backup:backup/www --progress

# Copy only new/modified files (faster)
rclone copy /var/www b2backup:backup/www --update

Complete backup script

sudo nano /usr/local/bin/backup-vps.sh
#!/bin/bash
# Complete VPS backup with rclone
# Variables
REMOTE="b2backup:my-backup"
DATE=$(date +%Y-%m-%d)
BACKUP_DIR="/tmp/backup-$DATE"
LOG="/var/log/backup.log"

echo "=== Backup $DATE ===" >> $LOG
mkdir -p $BACKUP_DIR

# 1. MySQL backup
echo "Backup MySQL..." >> $LOG
mysqldump --all-databases -u root -p"$MYSQL_ROOT_PASSWORD" \
  --single-transaction --quick 2>/dev/null | gzip > $BACKUP_DIR/mysql-$DATE.sql.gz

# 2. Web file backup
echo "Backup /var/www..." >> $LOG
tar -czf $BACKUP_DIR/www-$DATE.tar.gz /var/www/html 2>/dev/null

# 3. Configuration backup
echo "Backup configs..." >> $LOG
tar -czf $BACKUP_DIR/configs-$DATE.tar.gz \
  /etc/nginx /etc/apache2 /etc/letsencrypt 2>/dev/null

# 4. Upload to cloud
echo "Upload to $REMOTE..." >> $LOG
rclone copy $BACKUP_DIR $REMOTE/daily/$DATE --progress 2>> $LOG

# 5. Local cleanup
rm -rf $BACKUP_DIR
echo "Backup completed: $DATE" >> $LOG

# 6. Delete backups older than 30 days from cloud
rclone delete --min-age 30d $REMOTE/daily/ 2>> $LOG
sudo chmod +x /usr/local/bin/backup-vps.sh

Automation with cron

sudo crontab -e
# Backup every night at 3:00
0 3 * * * MYSQL_ROOT_PASSWORD=yourpassword /usr/local/bin/backup-vps.sh

# Weekly backup on Sunday at 2:00
0 2 * * 0 /usr/local/bin/backup-weekly.sh

Backup with encryption

To encrypt files before upload (recommended):

rclone config
# Create a new remote of type "crypt"
# Name: b2encrypted
# Remote: b2backup:my-encrypted-bucket
# Enter a strong passphrase
# Now use the encrypted remote
rclone copy /var/www b2encrypted:www

Files are encrypted before upload: even if the bucket is compromised, data is unreadable.


Incremental backup with --backup-dir

# Keep a current version + backup of what changed
rclone sync /var/www b2backup:backup/current \
  --backup-dir b2backup:backup/$(date +%Y-%m-%d) \
  --progress

Verify backups

# Check that files were uploaded
rclone ls b2backup:my-backup/ | head -20

# Compare local vs cloud (find differences)
rclone check /var/www b2backup:backup/www

# Download and verify a single file
rclone copy b2backup:my-backup/mysql-2025-01-01.sql.gz /tmp/
gunzip /tmp/mysql-2025-01-01.sql.gz
mysql -u root -p < /tmp/mysql-2025-01-01.sql

Restore from backup

# Download all files from a specific date
rclone copy b2backup:my-backup/daily/2025-01-15 /tmp/restore/

# Restore database
gunzip /tmp/restore/mysql-2025-01-15.sql.gz
mysql -u root -p < /tmp/restore/mysql-2025-01-15.sql

# Restore web files
tar -xzf /tmp/restore/www-2025-01-15.tar.gz -C /

On this page