ZyVOP Logo
Content That Connects
SeriesAI NewsWhy ZyVOPJoin Discord
LoginGet Started
ZyVOP Logo
Content That Connects

The Developer Publishing Hub. Write once, cross-post to Dev.to, Medium, Hashnode, WordPress & Bluesky with automated canonical source tags and zero paywalls.

Content

  • Categories
  • Tags
  • Badges
  • Leaderboard
  • Write Article
  • Newsletter

Company

  • About Us
  • Why ZyVOP
  • Developer API & CLI
  • Write for Us
  • Contact

Connect

  • Privacy Policy
  • Terms of Service
  • Cookie Policy
  • DMCA Policy
  • Code of Conduct

© 2026 ZyVOP. Developer Publishing Hub.

Zero paywalls · Full content ownership
All systems operational
HomeThe Ultimate Guide to Automating Postgres Backups from Docker to Google Drive

The Ultimate Guide to Automating Postgres Backups from Docker to Google Drive

A Step-by-Step Guide to Secure, Reliable, and Auto-Scheduled Disaster Recovery

Bhavya Arora
Bhavya Arora
Senior Developer
May 22, 2026
4 min read
The Ultimate Guide to Automating Postgres Backups from Docker to Google Drive
#Docker#rclone#postgres#Google Drive#Backups
👍1

If you run a production web application, your database is your most valuable asset. While Docker makes deploying a Postgres database incredibly easy, it also creates a false sense of security. If your entire server crashes, your VPS provider goes down, or you accidentally delete a volume, your data is gone forever.

The gold standard for backups is Off-Site Storage.

In this guide, we will walk through exactly how to completely automate your Postgres database backups. We'll extract the data securely from a Docker container, compress it, upload it directly to your Google Drive, and automatically delete old backups so your hard drive never fills up.


Prerequisites

Before we begin, ensure your server has the following:

  • A running Docker container running PostgreSQL

  • rclone installed on your server (sudo apt install rclone)

  • A Google account with sufficient Drive storage space


Step 1: Configure Rclone for Google Drive

Rclone is often referred to as "The Swiss army knife of cloud storage". It's a command-line program that can sync files to almost any cloud provider, including Google Drive.

If your database is hosted on a remote Ubuntu server, you'll need to use rclone's "headless" authorization, since you don't have a web browser on the server.

  1. On your server, initialize the config:

    rclone config
  2. Type n for New remote and name it gdrive.

  3. Select drive (Google Drive) from the list of cloud providers.

  4. Leave client_id and client_secret blank (press Enter).

  5. For scope, choose the option for Full access all files (usually option 1).

  6. Leave the rest of the options default until it asks: Use auto config?. Type n (No).

Rclone will now give you a long command that looks like this:

rclone authorize "drive" "eyJzY29wZ...
  1. On your local computer (where you do have a web browser), install rclone, open your terminal, and paste that exact command.

  2. Your browser will open. Log into your Google Account and click Allow.

  3. Your local terminal will output a secure token. Copy the entire { "access_token": ... } JSON string.

  4. Back on your server, paste the token into the prompt.

Your Google Drive is now securely linked to your server!


Step 2: The Ultimate Backup Script

Now we need a bash script that handles the heavy lifting. This script will read your database credentials from your .env file, export a compressed snapshot of your Postgres database from inside the Docker container, and upload it to Google Drive.

Create a file named backup-db.sh in your project's root folder:

nano backup-db.sh

Paste the following script inside. Be sure to update the PROJECT_DIR and CONTAINER_NAME variables to match your environment:

#!/bin/bash

# Configuration
PROJECT_DIR="/home/bhavya/apps/my-project"
BACKUP_DIR="$PROJECT_DIR/backups"
ENV_FILE="$PROJECT_DIR/.env"

# Rclone configuration
# Format: "RemoteName:FolderNameInGoogleDrive"
RCLONE_REMOTE="gdrive:database-backups"

# Docker container running Postgres
CONTAINER_NAME="my-project-db-1"

# Automatically load the database variables from your .env file
if [ -f "$ENV_FILE" ]; then
    export $(grep -v '^#' "$ENV_FILE" | xargs)
else
    echo "Error: .env file not found at $ENV_FILE"
    exit 1
fi

mkdir -p "$BACKUP_DIR"

# Generate a timestamped filename
TIMESTAMP=$(date +"%Y-%m-%d_%H-%M-%S")
BACKUP_FILE="$BACKUP_DIR/db_backup_$TIMESTAMP.dump"

echo "Starting backup at $TIMESTAMP"

# 1. Export the database from the Docker container
# We use the custom Postgres format (-F c) for maximum compression and reliability
docker exec "$CONTAINER_NAME" pg_dump -U "$POSTGRES_USER" -F c -d "$POSTGRES_DB" > "$BACKUP_FILE"

if [ $? -eq 0 ]; then
    echo "Backup created successfully: $BACKUP_FILE"
    
    # 2. Upload the encrypted file to Google Drive using rclone
    echo "Uploading to Google Drive..."
    rclone copy "$BACKUP_FILE" "$RCLONE_REMOTE"
    
    if [ $? -eq 0 ]; then
        echo "Upload successful!"
    else
        echo "Error during rclone upload!"
        exit 1
    fi
else
    echo "Error creating backup!"
    exit 1
fi

# 3. Clean up: Delete local backups older than 7 days so your server disk never fills up
echo "Cleaning up old local backups..."
find "$BACKUP_DIR" -type f -name "db_backup_*.dump" -mtime +7 -exec rm {} \;

echo "Backup process completed."
echo "-----------------------------------"

Save the file and make it executable:

chmod +x backup-db.sh

You can test it immediately by running ./backup-db.sh. If it works, check your Google Drive—you should see a new folder containing your .dump file!


Step 3: Put it on Autopilot with Cron

We don't want to run this script manually every day. Linux has a built-in time-based job scheduler called cron that is perfect for this.

Open your crontab editor:

crontab -e

Scroll to the very bottom and add the following line:

# Run the database backup daily at 2:00 AM
0 2 * * * /home/bhavya/apps/my-project/backup-db.sh >> /home/bhavya/apps/my-project/backups/backup.log 2>&1

Breaking down the cron schedule:

  • 0 2 * * * means the script will run exactly at 2:00 AM every single day.

  • >> backup.log 2>&1 ensures that all terminal outputs (both successes and errors) are saved to a log file. If you ever need to debug why a backup failed, simply check this file!

Conclusion

And that's it! You have now implemented an enterprise-grade disaster recovery pipeline for your startup or side project.

Every night at 2:00 AM:

  1. Your Docker database is frozen and exported.

  2. The file is seamlessly transferred to Google Drive.

  3. Any backup older than 7 days is automatically deleted from your server to save disk space.

Sleep soundly knowing your users' data is safe!

Comments (0)

Login to post a comment.

Bhavya Arora
Bhavya Arora

Passionate developer sharing knowledge about modern web technologies and best practices.

Subscribe to Bhavya Arora's Newsletter

More from Bhavya Arora

View profile

The 15 Git Commands That'll Save Your Sanity (and Your Code)

Most Git tutorials read like documentation. This one reads like advice from a developer who's already made every mistake in it — the 15 commands you'll actually use, why they trip people up, and which ones Git has quietly modernized.

14 minAug 31

How Netflix's Architecture Works in 2026: A Developer's Guide

Netflix's architecture has evolved from a monolithic DVD business into a globally distributed platform built around EKS, Envoy, Open Connect, GraphQL, scalable data systems, resilience engineering, modern video encoding, and AI infrastructure.

11 minAug 30

Qwen3.8-Max: Alibaba Just Open-Sourced a 2.4 Trillion Parameter Monster — And It Changes Everything

Alibaba just dropped the weights for Qwen3.8-Max — a 2.4 trillion parameter Mixture-of-Experts model that autonomously coded for 16 days straight, 4x'd capital in a simulated e-commerce test, and rivals GPT-5.6 Sol on graduate-level science. Here's why this isn't just another model release — it's a seismic shift.

10 minAug 16

AirLLM: Running Giant AI Models on Everyday Hardware

AirLLM lets you run 70B+ parameter models on a consumer GPU with as little as 4GB of VRAM — no quantization, no accuracy loss, no data center. A layer-by-layer streaming trick makes it possible. Here's how it works and who should use it.

7 minAug 5

The Wix Collapse: What a $20 Billion Fall Tells Us About the AI Era

Wix cut 20% of its workforce, slashed its 2026 outlook, and watched its stock fall 85% from peak. The revenue is still growing. So why does the market think the company is dying?

8 minAug 2