Syncing Backups to the Cloud with rclone
Your automated backup strategy is excellent, but what if the server itself fails completely? A local backup won't help if the hard drive dies. This is where the 3-2-1 Backup Rule comes in: 3 copies of your data, on 2 different media, with 1 copy off-site.
rclone is the "Swiss army knife for cloud storage." It's a command-line program that lets you sync files and directories to dozens of cloud providers. This guide will show you how to use it to create your off-site backup.
1. Prerequisites
- You have an automated local backup system in place.
- Reference: Automated WordPress Backups on Ubuntu
- You have an account with a cloud storage provider (Google Drive, Dropbox, Backblaze B2, etc.).
2. Install rclone
The recommended way to install rclone is via their official script, which ensures you get the latest version.
sudo -v ; curl https://rclone.org/install.sh | sudo bash
Verify the installation: rclone --version
3. Configure Your Cloud "Remote"
rclone needs to be authorized to access your cloud account. This is done via an interactive setup. We'll use Google Drive as an example.
- Start the configuration tool:
rclone config - Follow the prompts:
n(New remote)- name>:
gdrive(You can name it anything) - Storage>: Find "Google Drive" in the list and enter its number.
- client_id>: Press Enter (leave blank).
- client_secret>: Press Enter (leave blank).
- scope>:
1(Full access to all files). - root_folder_id>: Press Enter (leave blank).
- service_account_file>: Press Enter (leave blank).
- Edit advanced config?:
n(No). - Use auto config?:
y(Yes). This will open a browser window for you to log in to your Google account and grant permission. - Configure this as a team drive?:
n(No, unless you are using a Google Workspace Team Drive). - y/e/d>:
y(Yes, this is OK). - q>:
q(Quit config).
Your gdrive remote is now configured and ready.
4. Integrate with the Backup Script
The final step is to add the rclone command to the end of your existing backup script, so the off-site sync happens automatically after the local backup is created.
-
Edit the script:
sudo nano /usr/local/bin/backup_wp.sh -
Add the sync command: Go to the very end of the script and add a new section for the cloud sync. The
synccommand makes the destination look exactly like the source, deleting old files from the cloud if they are deleted locally.#!/bin/bash # --- (Previous configuration remains the same) --- DB_NAME="wordpress" DB_USER="wp_user" DB_PASS="your_strong_password" WP_PATH="/var/www/html" BACKUP_DIR="/var/backups/wordpress" DATE=$(date +"%Y-%m-%d_%H%M%S") # --- (Previous script logic remains the same) --- mkdir -p $BACKUP_DIR mysqldump -u $DB_USER -p$DB_PASS $DB_NAME > $BACKUP_DIR/db_backup_$DATE.sql tar -czf $BACKUP_DIR/files_backup_$DATE.tar.gz $WP_PATH find $BACKUP_DIR -type f -mtime +7 -name '*.sql' -delete find $BACKUP_DIR -type f -mtime +7 -name '*.tar.gz' -delete # --- NEW: Off-site Sync --- echo " [4/4] Syncing backups to the cloud..." rclone sync $BACKUP_DIR gdrive:WordPressBackups echo "Backup and cloud sync complete!"
Now, every time your cron job runs, it will create the local backup, clean up old files, and then sync the entire backup directory to a folder named WordPressBackups in your Google Drive.