My team's off-site backups lived in a private GitHub repo, and the repo had grown to about 1 GB. Every night the server makes a new encrypted database dump, and git keeps every old one in its history, so the repo only grows.

So I moved the off-site copy to Google Drive. Now rclone copies the backup folder to a Drive folder every night, and the backup script no longer pushes to GitHub. sudo on the server needs a password, so instead of installing rclone, I run it from its official Docker image.

Result: after the nightly backup, rclone sync makes the Drive folder match the server's backup folder. rclone can see that one folder and nothing else in the Drive. I restored the database from the Drive copy to make sure it works.

Environment:

  • Server: Ubuntu 26.04.1 LTS, Docker 29.1.3 (Ubuntu's package), systemd user units (backup timer at 03:00 with RandomizedDelaySec=15m), no sudo
  • Desktop: Ubuntu with a browser, used once to log in to Google
  • rclone 1.76.0, from the rclone/rclone:1.76.0 image (a pinned tag, so an image update can't change behavior overnight)
  • What gets backed up: a gpg-encrypted Postgres dump (about 34 MB a day), a gpg-encrypted copy of the Docker Compose config (it holds secrets), and a restic repository of uploaded files (about 111 MB; restic is an encrypting backup tool)

Before you start

What you need

  • A backup that already writes encrypted files to one folder on the server (gpg, restic, or similar). This post sends that folder off-site; it doesn't create the backup.
  • Docker on the server and on a desktop with a browser, and SSH access from the desktop to the server.
  • Your server user must be able to run docker without sudo (it's in the docker group); check with docker run --rm hello-world. An admin had already added my user. That group is not a limited role: per Docker's docs, "the docker group grants root-level privileges to the user". So "no sudo" here means no sudo password, not fewer privileges.
  • python3 on the desktop, for reading the client JSON in step 1.
  • A home page URL and a privacy policy URL for Google's consent screen (step 1). The rclone docs say a free GitHub Pages site is fine if you don't have a domain.
  • Two scripts from this post: gdrive-auth.sh for the one-time login and drive-sync.sh for the nightly sync.

In the commands below, replace user@server, /path/to/backups, and <ID> with your own values.

Then make two choices first. Changing either one later means redoing part of the setup.

Give rclone access to its own files only

By default, rclone asks Google for full access to your Drive. The login token is saved on the server, so anyone who copies that file can read your whole Drive.

The drive.file scope (scope = the level of access you request) limits rclone to "only those files and folders it creates", according to the rclone docs. It's the same idea as giving an app a database user with only the grants it needs (PostgreSQL User Creation and Permission Setup).

The downside: rclone can't see anything else. My team wanted the backups inside an existing shared folder, which people had created in the web UI, so rclone couldn't see it. Step 4 works around this.

Use your own OAuth client, not rclone's shared one

An OAuth client is the app identity rclone shows Google when it logs in. Out of the box, every rclone user shares one client. On my first setup, rclone printed this warning:

This remote uses rclone's shared Google Drive client_id, which is being retired and will stop working during 2026 

The rclone docs now say creating your own client "is now required rather than merely recommended". They don't give an exact shutdown date.

Create your client before you create any folder. With drive.file, access is tied to the client that created the files. When I switched clients halfway through, the new client couldn't see the old client's folder, and I ended up with two backup folders (the docs don't say this directly; it's what happened to me).

Set it up

1. Create your own OAuth client

In the Google Cloud console, open Google Auth Platform and go through these pages:

Page What to set Why
Branding App name, support email, developer contact, App domain (home page, privacy policy), Authorized domains Publish stays gray until the domain fields are filled.
Audience External, then Publish app (status: In production) In "Testing", logins expire after a week, which breaks an unattended job.
Clients Create a client of type Desktop app rclone logs in like a desktop app, through a local redirect.
APIs Enable Google Drive API rclone calls this API with your client.

I reused an existing Cloud project, with no Cloud Storage or billing account. The rclone docs note that a personal app with fewer than 100 users can skip Google's verification; you click through an "unverified app" warning at login instead.

Download the client's JSON file, then load the ID and secret into environment variables. This keeps them off your screen and out of your shell history. Once the config exists you don't need the file on this machine, so move it somewhere private, like a password manager:

export GDRIVE_CLIENT_ID=$(python3 -c 'import json;print(json.load(open("client.json"))["installed"]["client_id"])') export GDRIVE_CLIENT_SECRET=$(python3 -c 'import json;print(json.load(open("client.json"))["installed"]["client_secret"])') 

2. Log in on the desktop and copy the config to the server

The server has no browser, so the login happens on the desktop. That's fine: you need a browser only for this one login, and after that rclone refreshes the token on its own.

I put the login and the copy in one small script, run once on the desktop. Here's the core of it (the full script is a download and also checks the connection from the server):

#!/usr/bin/env bash set -euo pipefail REMOTE=${1:?usage: ./gdrive-auth.sh user@server} IMAGE=rclone/rclone:1.76.0 # Never overwrite a working token on the server ssh "$REMOTE" 'test ! -e ~/.config/rclone/rclone.conf' \ || { echo "server already has rclone.conf"; exit 1; } # Log in into a temp folder that is shredded when the script exits work=$(mktemp -d) trap 'find "$work" -type f -exec shred -u {} + 2>/dev/null || true; rm -rf "$work"' EXIT docker run --rm --network host --user "$(id -u):$(id -g)" -v "$work":/config/rclone "$IMAGE" \ config create gdrive drive scope=drive.file \ client_id="$GDRIVE_CLIENT_ID" client_secret="$GDRIVE_CLIENT_SECRET" >/dev/null grep -q '^token = ' "$work/rclone.conf" || { echo "login did not finish"; exit 1; } # Copy the config to the server, readable only by your user ssh "$REMOTE" 'mkdir -p ~/.config/rclone && chmod 700 ~/.config/rclone && umask 077 && cat > ~/.config/rclone/rclone.conf' \ < "$work/rclone.conf" 

Download it and run it in the same shell where you exported the two variables from step 1:

curl -fsSLO https://jamongx.com/downloads/rclone-google-drive-backup-docker/gdrive-auth.sh chmod +x gdrive-auth.sh ./gdrive-auth.sh user@server 

rclone prints a link like http://127.0.0.1:53682/auth?state=…. Open it, log in, and approve. What the less obvious parts of the script do:

  • --network host: Google sends the browser back to 127.0.0.1:53682, where rclone runs a small web server to receive the answer. The container has to share the desktop's network to get it.
  • >/dev/null: standard output carries the finished config, token included. The login link goes to standard error, so you still see it.
  • trap … shred: the desktop copy is destroyed when the script exits, even if a step fails. If the copy to the server failed, you just run the script again.
  • umask 077: the server file is owner-only from the moment it's created.

(Other ways to log in without a browser on the server are at the end.)

3. Set up rclone on the server

On the server, define a small function so the Docker image works like an installed rclone. It mounts the config, plus your backup folder read-only at /src for the checks later. Then test the connection:

rclone() { docker run --rm --user "$(id -u):$(id -g)" \ -v ~/.config/rclone:/config/rclone \ -v /path/to/backups:/src:ro \ rclone/rclone:1.76.0 "$@" } rclone about gdrive: 

4. Create the backup folder and pin rclone to it

rclone mkdir gdrive:server-backup rclone lsjson gdrive: --dirs-only # copy the "ID" of server-backup rclone config update gdrive root_folder_id=<ID> config_refresh_token=false --non-interactive >/dev/null 

root_folder_id tells rclone to treat this folder as the top of the Drive. The output goes to /dev/null because config update can print the token.

Now open Drive in the browser and drag the folder to where your team wants it. rclone finds the folder by its ID, not its path, so my next backup landed in the moved folder.

5. Sync to Drive at the end of each backup

A shell function like the one in step 3 doesn't exist inside your backup script, so the script needs the full docker run. I put it in drive-sync.sh. Download it on the server:

mkdir -p ~/bin curl -fsSL -o ~/bin/drive-sync.sh https://jamongx.com/downloads/rclone-google-drive-backup-docker/drive-sync.sh chmod +x ~/bin/drive-sync.sh 

Then add one line at the end of your backup script, after the local backup is done:

BACKUP_DIR=/path/to/backups ~/bin/drive-sync.sh 

The core of it:

docker run --rm --user "$(id -u):$(id -g)" \ -v "$RCLONE_CONFIG_DIR":/config/rclone \ -v "$BACKUP_DIR":/src:ro \ "$RCLONE_IMAGE" sync /src "$DRIVE_REMOTE" --log-level ERROR 

sync makes Drive an exact copy of the backup folder, deletions included. Old backups are cleaned up in one place, the server, and Drive follows. That's also why the folder should hold only encrypted files.

By default rclone moves deleted files to the Drive trash.

A failed upload doesn't alert anyone right away. The local backup is already safe, and the next night's sync catches up. The script records each successful sync and exits with an error only after 3 days without one (GRACE_DAYS=3), or right away if it has never succeeded.

That error fails your systemd service, so an OnFailure= unit can alert you. Mine sends a Telegram message.

drive-sync.sh is a simplified version of the sync step in my own backup script, which mounts the dump folder and the restic repository as two folders and picks them with --include filters. I haven't run the published single-folder version against Drive; I tested its grace-period logic with a stand-in for docker.

6. Give the first upload more time

My backup unit had TimeoutStartSec=10min, and the first upload hit it and was killed after 317.7 MiB. (A later rclone size showed 445 MiB in the folder, including extra dumps from my test runs.) The rerun finished in 1 minute 37 seconds, because rclone skips files that are already uploaded.

I raised the limit to TimeoutStartSec=30min; if your unit sets a start timeout, leave room for the first upload.

I don't know why that first upload was so slow. It used rclone's shared client; a later full upload with my own client took 1 minute 10 seconds. I didn't test whether the client was the cause.

Check it works: restore from Drive

A finished upload doesn't prove you can get your data back. After years of running backend systems, I don't trust a backup until I've restored it.

First, check the files. --download compares contents, not just sizes:

rclone check /src gdrive: --download 
0 differences found 37 matching files 

My restore-check script downloads the newest dump from Drive, decrypts it, restores it into a temporary Postgres container, and compares every table's row count with production. All 57 tables and 18,131 rows matched, in two runs. The script is tied to our schema, but those four steps work for any database.

Not tested yet: restoring the uploaded files from Drive, and rebuilding everything after losing the server.

Troubleshooting

Symptom Cause and fix
Error: unknown flag: --auth-no-open-browser That flag is only for rclone authorize. Remove it; config create prints the link on its own.
Failed with result 'timeout' on the first run The first full upload outlasted TimeoutStartSec. See step 6.
Publish app is gray: "To publish your app, you must complete your configuration on the Branding page" Fill in App domain and Authorized domains, then save.
Invalid domain: must use either http or https as the scheme You typed a bare domain into a URL field. Add https://.
Missing domain: <domain>, and Save stays gray Add that domain to Authorized domains, without https://.
Two backup folders after switching clients Pin the new folder (step 4) and delete the old one by hand.
You keep 3 days of dumps, but 4 days are left The age check counts minutes, and the timer's random delay moves the start. See below.

I found the last one on the fourth morning, when the Drive folder had more backup files than it should. The cleanup deleted dumps by age in minutes:

# before (the script deletes each file this prints; $newest is always kept) find "$dir" -name '*.gpg' -mmin +$((RETENTION_DAYS * 1440)) ! -path "$newest" # after: subtract a margin bigger than the timer's random delay RETENTION_SLACK_MIN=60 find "$dir" -name '*.gpg' -mmin +$((RETENTION_DAYS * 1440 - RETENTION_SLACK_MIN)) ! -path "$newest" 

With RandomizedDelaySec=15m, the backup doesn't start at the same minute every night. On December 4 it started at 03:03, so the December 1 dump from 03:05 was 71 hours 58 minutes old. That's two minutes short of 72 hours, so it survived.

What you give up

  • rclone can't see anything it didn't create.
  • The token exists only on the server. If you lose the server, you have to log in again from another machine with the same client. I haven't practiced that yet.
  • In my case, the backup folder inherited the shared folder's permissions, so teammates can delete files in it. The files are encrypted and the next sync puts them back, but until then the Drive copy is incomplete.
  • The rclone docs say Drive's rate limits hold rclone to "about 2 files per second", so many small files upload slowly.

Undo

rm ~/.config/rclone/rclone.conf 

Then remove the drive-sync.sh line from the backup script, delete the Drive folder, and remove the app's access under third-party connections in your Google account.

Other ways to log in without a browser

The rclone remote setup docs list three ways to log in for a machine without a browser:

Method How
Copy the config file What this post does (step 2, gdrive-auth.sh).
rclone authorize Answer n to the browser question on the server, run rclone authorize "drive" "$GDRIVE_CLIENT_ID" "$GDRIVE_CLIENT_SECRET" on the desktop, and paste the token back.
SSH tunnel Connect with ssh -L localhost:53682:localhost:53682 user@server, then open the printed link on the desktop. With Docker, run the container with --network host so the tunnel can reach it.

I used the first method. rclone authorize takes your own client ID and secret as extra arguments (command docs); I haven't tried the other two.

To skip the browser entirely, rclone also supports a Google service account, which its docs call "unattended mode". Their example is a Google Workspace admin setup, and I haven't tried it.

If you were hoping for a desktop sync app instead: Google's official Drive app is Windows and macOS only (It's FOSS).

Sources

Originally published at jamongx.com.