How to Use Rclone on Ubuntu to Automate Data Backups to Google Drive, S3, and OneDrive

Ubuntu tutorial - IT technology blog
Ubuntu tutorial - IT technology blog

A 2 AM VPS Crash and an Unforgettable Lesson

Over a year ago, my 45GB PostgreSQL VPS abruptly died. The culprit was a physical disk array failure on the hosting provider’s end. The entire e-commerce database and over 120GB of uploaded images vanished into thin air within minutes.

Before that incident, I was overly complacent. I relied solely on a cron job compressing files directly into /home/backup on that very same VPS, treating the provider’s weekly auto-snapshots as a safety net. When the physical server cluster suffered a fried controller, both the primary data and the backups evaporated without a trace.

Never keep your backups in the same basket as your production server. Ever since that painful lesson, across all 20 Ubuntu VPS instances I currently manage, the very first thing I do is set up an off-site backup pipeline.

Why Common Sync Tools Often Fall Short

Before settling on an ideal solution, I tried various approaches, all hitting frustrating roadblocks:

  • Google Drive and OneDrive lack official Linux CLIs: Authenticating accounts on a headless VPS over SSH is an absolute nightmare.
  • Third-party tools are memory-hungry: Python or Node.js scripts frequently devour 150MB to 300MB of RAM during execution. On budget 1GB RAM VPS instances, the OS can easily trigger the OOM Killer and terminate database processes.
  • Writing custom scripts against REST APIs is tedious: Using curl requires hand-rolling OAuth2 token refresh logic, handling chunked uploads for files over 5GB, and managing retries during intermittent network drops.

The Alternatives I Weighed

I previously considered three common paths:

  • Rsync over SSH to a dedicated backup VPS: Fast and reliable. However, you have to spend an extra $5 to $10 per month maintaining a secondary server just for storage. Upgrading disk capacity on a VPS is also significantly more expensive than object storage.
  • AWS CLI / s3cmd: Runs flawlessly with AWS S3, Cloudflare R2, or Wasabi. The major downside: you cannot leverage existing free storage from Google Drive Workspace or a 1TB OneDrive Family subscription.
  • BorgBackup: Top-tier compression and deduplication. On the flip side, configuring remote backends to personal cloud drives is overly complex for those without advanced sysadmin expertise.

Rclone: The Lightweight Lifesaver for Sysadmins

Rclone turned out to be the ultimate answer. Written in Go, it ships as a single standalone binary. In production, it consumes only about 20MB to 35MB of RAM. Rclone supports over 40 cloud providers and automatically handles everything from token refreshes to chunked multipart uploads.

Step 1: Installing Rclone Properly on Ubuntu

Avoid using apt install rclone. The default Ubuntu repositories often host versions that are 1–2 years outdated, which frequently break when Google or Microsoft update their APIs. Instead, pull the official installation script:

sudo -v && curl https://rclone.org/install.sh | sudo bash
# Check the version
rclone version

Step 2: Configuring Remotes for Google Drive, AWS S3, and OneDrive

Open the interactive setup wizard:

rclone config

1. Configuring Google Drive

Press n to create a new remote, name it gdrive, and select the number corresponding to Google Drive. When the terminal asks Use web browser to automatically authenticate, select n because the VPS runs headless without a browser.

Rclone will output a command. Open a terminal on your personal machine and run it:

# Run on your local machine with a web browser
rclone authorize "drive" "client_id" "client_secret"

A web browser will automatically open for authorization. Once granted, the local terminal will output a JSON string containing the access token. Copy this entire string, paste it back into your VPS terminal, and you are all set.

2. Configuring AWS S3 (or Cloudflare R2 / MinIO)

S3 is simpler because it uses direct access keys. Create a remote named s3_backup, select Amazon S3 Compliant Storage Provider, and fill in the parameters:

provider> AWS
env_auth> false
access_key_id> AKIAIOSFODNN7EXAMPLE
secret_access_key> wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
region> ap-southeast-1
location_constraint> ap-southeast-1
acl> private

3. Configuring OneDrive

The steps are similar to Google Drive. Name the remote onedrive, then run rclone authorize "onedrive" on your personal computer to obtain and paste the token.

Step 3: Verifying the Connection

Do not rush into writing automated scripts just yet. Test the connection first to ensure everything works properly:

# List directories on Google Drive
rclone lsd gdrive:

# List S3 buckets
rclone lsd s3_backup:

# Test copying a small file
rclone copy /etc/hosts gdrive:Server_Backups/Test/

Crucial Warning: Rclone features two commands that are easy to confuse: copy and sync. The copy command only uploads new or modified files, leaving existing files on the cloud untouched. Conversely, sync performs a one-way mirror sync. That means if you accidentally delete a file on your VPS, Rclone will purge that file from the cloud on its next run. For data backups, always use copy.

Step 4: Writing an Automated Script to Compress and Upload Data

Create the /opt/scripts/backup.sh file:

#!/usr/bin/env bash
set -euo pipefail

# Configuration variables
DATE=$(date +%Y-%m-%d_%H%M%S)
BACKUP_DIR="/var/backups/daily"
WEB_DIR="/var/www"
DB_NAME="my_production_db"
DB_USER="db_backup_user"
DB_PASS="SuperHardPassword123!"
LOG_FILE="/var/log/rclone_backup.log"

mkdir -p "$BACKUP_DIR"

# 1. Safely dump database with gzip
echo "[$(date)] Dumping database..." >> "$LOG_FILE"
mysqldump -u"$DB_USER" -p"$DB_PASS" --single-transaction --quick "$DB_NAME" | gzip > "$BACKUP_DIR/db_${DATE}.sql.gz"

# 2. Compress source code
echo "[$(date)] Compressing source code..." >> "$LOG_FILE"
tar -czf "$BACKUP_DIR/web_${DATE}.tar.gz" -C "$WEB_DIR" .

# 3. Upload files to Google Drive
echo "[$(date)] Starting upload to Google Drive..." >> "$LOG_FILE"
rclone copy "$BACKUP_DIR" gdrive:Server_Backups/Daily/ \
    --bwlimit 10M \
    --transfers 2 \
    --log-file "$LOG_FILE" \
    --log-level NOTICE

# Push secondary backup to S3 for redundancy
rclone copy "$BACKUP_DIR" s3_backup:my-backup-bucket/Daily/ \
    --bwlimit 10M \
    --transfers 2

# 4. Remove local backups older than 3 days to free up disk space
find "$BACKUP_DIR" -type f -name "*.gz" -mtime +3 -delete

echo "[$(date)] Backup completed successfully!" >> "$LOG_FILE"

Make the file executable:

sudo chmod +x /opt/scripts/backup.sh

Step 5: Setting Up Automated Execution with Crontab

Open root’s crontab:

sudo crontab -e

Add the following line to run the backup daily at 2:30 AM:

30 2 * * * /opt/scripts/backup.sh > /dev/null 2>&1

Battle-Tested Production Tips

  • Don’t forget --bwlimit 10M: Compressed archives of 20GB–30GB will saturate VPS network bandwidth if left uncapped. Your website may slow down or become unreachable during backups. A limit of 10MB/s to 15MB/s is the sweet spot.
  • Avoid Google Drive’s HTTP 403 traps: Google enforces a strict daily upload limit of 750GB per account and throttles high-frequency API calls. Restricting flags to --transfers 2 --checkers 4 prevents temporary 24-hour API lockouts.
  • Wrap data in encryption with crypt: If you are hesitant about storing sensitive data on public clouds, wrap your base remote in an Rclone crypt overlay. Rclone performs AES-256 encryption on-the-fly in VPS RAM before transfer, leaving the cloud provider with nothing but encrypted ciphertext.
Share: