Marcio Cunha

How to Automate Encrypted Database Backups to S3 Using Wal-G

Learn how to build a secure, efficient database backup pipeline for PostgreSQL using Wal-G, advanced compression, and encryption directly to AWS S3.

Marcio Cunha12 min
Also available in:EspañolPortuguês
Summary
  • Choosing Wal-G drastically reduces execution time and disk consumption compared to legacy tools like barman.
  • End-to-end encryption ensures sensitive data remains secure even in storage buckets with permissive access configurations.
  • Continuous incremental storage saves significant cloud bandwidth costs and long-term storage space.
  • Automated periodic restore tests are mandatory to validate data integrity prior to real incidents.
  • Proper management of environment variables prevents sensitive credential leaks in automation scripts.

The Operational Challenge of Protecting Production Data

Managing relational databases in production environments requires a solid disaster recovery strategy. In practice, this means a system without reliable backup routines is simply a failure waiting to happen, potentially costing a company its reputation and financial viability.

Traditional backup tools often suffer from severe performance bottlenecks, generating massive files that lock processing and consume unnecessary disk space. To overcome this, engineers seek solutions that perform intelligent compression and direct upload to cloud storage services like Amazon S3.

The Role of Wal-G in the Backup Architecture

Wal-G emerges as the natural successor to the classic Wal-E, rewritten in Go to deliver extremely high I/O performance and native parallelism. In practice, it acts as a Swiss Army knife capable of reading continuous database transaction streams and shipping them compressed to the cloud almost in real time.

One of the greatest advantages of this tool is its ability to perform incremental backups, meaning it only sends changes made since the last full copy. This drastically reduces bandwidth usage and speeds up the process, enabling frequent routines without impacting the main application's performance.

Configuring the Environment and Access Credentials

Before running the tool, you must prepare the ground by configuring appropriate permissions in the cloud and on the database server. In practice, we create a dedicated bucket in Amazon S3 and an IAM user with restricted policies allowing only the read and write operations strictly necessary.

We also configure essential environment variables so the process knows where to connect and which keys to use. Below is a practical configuration example in a shell configuration file or system service manager:

export WALG_S3_PREFIX='s3://my-production-backup-bucket/postgresql'export AWS_REGION='us-east-1'export WALG_COMPRESSION_METHOD='lz4'export AWS_ACCESS_KEY_ID='YOUR_ACCESS_KEY'export AWS_SECRET_ACCESS_KEY='YOUR_SECRET_KEY'

With these variables defined, the Wal-G binary knows precisely where to direct the data blocks generated by database operations.

Implementing End-to-End Encryption

Storing files in the cloud without additional protection is an unacceptable security risk for sensitive customer data or financial records. Therefore, encryption is a mandatory step that must be integrated directly into the backup export workflow before data leaves the local machine.

Wal-G has native support for using GPG symmetric keys to encrypt data before sending it to S3, ensuring that even if someone gains unauthorized access to the bucket, the data remains unreadable garbage. Below is the configuration snippet activating this extra security shield:

export WALG_PGP_KEY_PATH='/etc/wal-g/private_key.gpg'export KEY_ID='ABC123XYZ'

This approach guarantees complete confidentiality, meeting strict regulatory compliance standards such as GDPR, CCPA, and PCI-DSS.

Automating Execution with PostgreSQL and Cron

With the storage and encryption infrastructure ready, the next step is automating the process so it runs without human intervention. We configure the PostgreSQL parameter file, known as postgresql.conf, to point the continuous transaction file command to Wal-G.

In the configuration file, we add the following fundamental directive for continuous archiving operation:

archive_mode = onarchive_command = 'wal-g wal-push %p'archive_timeout = 60

For the full backup (basebackup), we schedule a routine task using the operating system's task scheduler, Cron, executing the command in the early morning hours when user traffic is lowest.

Validating Restoration and Testing Recovery

A backup that has never been tested for restoration is nothing more than a false sense of security. In practice, experienced engineers know that discovering a flaw in the recovery process should never happen during a real production outage.

To validate integrity, we create an isolated staging environment where we download the latest encrypted version from S3 using Wal-G's restore command. The typical procedure executed on the new server follows this structure:

wal-g backup-fetch /var/lib/postgresql/15/main LATEST

After successful extraction, we apply the decryption keys provided in the environment variables and start the service to check if all tables and records are intact.

Final Considerations

Automating encrypted backups using Wal-G and Amazon S3 represents a turning point in the operational maturity of engineering teams. It replaces fragile manual processes with a resilient, cost-effective, and highly secure data protection pipeline.

Investing time in correctly configuring this architecture pays immediate dividends in team peace of mind and business resilience against any future technical mishaps.