How to Set Up Rclone with Google Drive on a Headless Server (The Right Way)
Backing up your remote server (like a Raspberry Pi) directly to Google Drive is a brilliant way to ensure data safety. The tool for the job is Rclone – the “Swiss Army knife” of cloud storage. However, setting it up on a “headless” server (a machine without a graphical interface or web browser) can be tricky, especially when dealing with Google’s authentication.
In this guide, we will walk you through the bulletproof method: creating your own Google Cloud Platform (GCP) project to avoid rate limits, authorizing on your local machine, and moving the configuration to your server.
Phase 1: Creating Your GCP Project (Avoiding Rate Limits)
Using Rclone’s default Google Drive client ID means sharing bandwidth with thousands of other users, leading to throttling. Here is how to create your own:
- Create a Project: Go to the Google Cloud Console, click the project dropdown, and select New Project. Name it something like “Rclone Backup”.
- Enable the API: Search for Google Drive API in the top search bar and click Enable.
- Configure OAuth Consent Screen:
- Go to APIs & Services > OAuth consent screen.
- Choose External user type.
- Fill in the mandatory fields (App name, email addresses).
- Under “Scopes”, add
.../auth/driveto give Rclone full access to manage files. - Under “Test users”, add your own Google account email.
- Generate Credentials:
- Go to the Credentials tab.
- Click Create Credentials > OAuth client ID.
- Crucial step: Choose Desktop app as the application type. This prevents issues with redirect URLs later.
- Click Create. Copy your Client ID and Client Secret.
Phase 2: Local Authorization
Since your server doesn’t have a web browser, we will generate the access token on your main computer (Windows/Mac/Linux).
- Install Rclone on your local computer.
- Open a terminal and type
rclone config. - Press
nfor a new remote and name it (e.g.,gdrive). - Choose
driveas the storage type. - Paste your Client ID and Client Secret when prompted.
- When asked
Use auto config?, typey(Yes). - Your browser will open. Log in and grant permission.
- Back in the terminal, you will see a block of JSON code containing your token. Copy this entire token block.
Phase 3: Server Configuration
Now, let’s move to your headless server (via SSH).
- Install Rclone on your server (
sudo apt install rclone). - Run
rclone config. - Create a new remote (
n), name it exactly as you did locally (gdrive), choosedrive, and paste your Client ID and Secret. - When asked
Use auto config?, typen(No). - When prompted for the result/token, paste the JSON block you copied from your local machine.
- Save and exit. Test it by typing
rclone lsd gdrive:. You should see your Google Drive folders!
Phase 4: Automating Backups
Now that Rclone is authorized, let’s set up an automated backup script. Create a file named backup.sh:
Bash
#!/bin/bash
BACKUP_DIR="/path/to/local/backup"
DATE=$(date +%Y-%m-%d)
REMOTE="gdrive:ServerBackups"
# Your backup commands here (e.g., mysqldump, tar)
tar -czf "$BACKUP_DIR/data_$DATE.tar.gz" /var/www/html
# Copy to Google Drive (Rclone creates the folder automatically)
rclone copy "$BACKUP_DIR/data_$DATE.tar.gz" "$REMOTE"
# Keep only the last 7 days of backups on Google Drive
rclone delete "$REMOTE" --min-age 7d
Make the script executable (chmod +x backup.sh) and add it to your cron jobs (crontab -e) to run nightly. Your headless server is now securely backed up to the cloud!