ZyVOP Logo
Content That Connects
SeriesCategoriesTags
ZyVOP Logo
Content That Connects

Empowering developers and creators with cutting-edge insights, comprehensive tutorials, and innovative solutions for the digital future.

Content

  • Tags
  • Write Article

Company

  • About Us
  • Contact

Connect

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

© 2026 ZyVOP. Crafted with care for the developer community.

Made with ❤️ by the ZyVOP team
All systems operational
HomeThe Ultimate Guide to Automating Postgres Backups from Docker to Google Drive
👍1

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

#Docker#postgres#Backups#rclone#Google Drive
Bhavya Arora
Bhavya Arora

Senior Developer

May 22, 2026
5 min read
47 views
The Ultimate Guide to Automating Postgres Backups from Docker to Google Drive

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!

Bhavya Arora

Bhavya Arora

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

Comments (0)

Login to post a comment.

Stay Updated

Get the latest articles delivered to your inbox.

We respect your privacy. Unsubscribe anytime.

Related Posts

Docker for Developers: Stop "It Works on My Machine" Forever

Docker eliminates the “works on my machine” problem by packaging your app, dependencies, and runtime into portable containers. This guide covers production-grade Dockerfiles, layer caching, multi-stage builds, Docker Compose, volumes, networking, and practical workflows for real applications.

Read article

The Developer's Guide to Environment Variables and Secrets Management

Environment variables are easy in local development and much harder in production. This guide covers secure configuration management across .env files, CI/CD pipelines, containers, staging, and production — including validation, documentation, secret rotation, and production-grade secrets management.

Read article

The Production Docker Compose Stack: Node.js, Postgres, Redis, and Nginx in One File

Read article

Database Replication: Why One Database Server Stops Being Enough

Read article

SQL vs NoSQL: Why Modern Systems Use Both

Read article

Popular Tags

#.env.example Node.js#0x profiling#12-factor#AI agents#AI code security#AI coding tools 2026#AI-assisted development#AI-generated vulnerabilities#ALTER TABLE no lock#API Design