
PostgreSQL databases require timely backups to recover data and secure records in case of database server failures. Cloud storage solutions such as object storage decentralize PostgreSQL database backups to enable smooth data recoveries in case the original database or server fails.
This article explains how to automatically back up PostgreSQL databases with object storage using the pg_dump and the Cron utilities. You will create and schedule weekly database backup operations to ensure that the latest backups are readily available for recovery and use in other tasks.
Before you begin:
A Debian server is used as the automation system for testing purposes in this article. Ensure to use your existing PostgreSQL database server to automate the database backup operations with Vultr Object Storage.
Vultr Object Storage is an S3-compatible cloud storage service that supports data transfer tools such as Cyberduck, S3 Browser, and S3cmd to securely upload and store files. Follow the steps below to set up Vultr Object Storage, create a new bucket, and upload test data files using S3cmd.
Update the server's package index.
Install s3cmd.
Run s3cmd with the --configure option to set up Vultr Object Storage as the default provider.
Output:
Paste your Vultr Object Storage access key and press Enter to save the new access key.
Paste your secret key and press Enter.
Press Enter to use the default S3 region US.
Enter your Vultr Object Storage hostname in the S3 Endpoint prompt. For example, ew1.vultrobjects.com.
Enter the bucket DNS-style template to use with Vultr Object Storage. For example %(bucket)s.ewr1.vultrobjects.com.
Enter a strong encryption password to protect your Vultr Object Storage connection from unauthorized users on the server.
Press Enter to accept the default GPG program path /usr/bin/gpg to encrypt objects during transfer to Vultr Object Storage.
Press Enter to use the HTTPS protocol with Vultr Object Storage.
Press Enter to use Vultr Object Storage without an HTTP Proxy connection. If your network requires a proxy, enter the server URL to use with s3cmd.
Review your object transfer configuration and enter Y to test access to your Vultr Object Storage.
Press Y then Enter to save the s3cmd configuration when the test is successful.
View the s3cmd configuration file .s3cfg in your user home directory to verify your Vultr Object Storage connection information.
Create a new Vultr Object Storage bucket to store your PostgreSQL backup files using s3cmd. For example postgreSQL-backups.
Output:
Follow the Vultr Object Storage naming requirements when creating new buckets to avoid any errors. Use lowercase, unique and only allowed characters to successfully create a new bucket.
Create a new sample text file PostgreSQL-test.txt.
Transfer the text file to your Vultr Object Storage bucket.
List all objects in the bucket and verify that s3cmd correctly uploads files to your Vultr Object Storage.
Output:
pg_dumppg_dump is pre-installed with the PostgreSQL database server package and enables the backup of PostgreSQL databases on your server. It works by creating a set of reproducable SQL statements that store the original database contents and state. Follow the steps below to back up your PostgreSQL databases using the pg_dump utility.
Create a new directory to store the PostgreSQL backup files. For example, PostgreSQL-backups.
Switch to the directory.
Create a new sample PostgreSQL database to backup. For example, exampledb.
Back up a single exampledb database to a file such as exampledb.sql.
Long list the directory files and verify that a new exampledb.sql file is created.
Output:
Back up all PostgreSQL databases on the server.
Back up all PostgreSQL users, roles and database permissions on the server.
List all files to verify all backup files available in your working directory.
Output:
Create a new directory to organize and store your database backup files. For example, test-backups.
Move all .sql PostgreSQL backup files to the directory for upload to your Vultr Object Storage bucket.
Upload all files in the backup files directory to your object storage bucket.
Output:
List your Vultr Object Storage bucket contents and verify that the database backup files are available.
Output:
Create a new data directory such as w1-08-2024 to organize your backups by date.
Copy all existing PostgreSQL backup files from test-backups to the new directory.
Upload the backup directory to your object storage bucket.
View the object storage bucket data and verify that the new directory files are available.
Output:
Follow the steps below to create a Bash script to automate the PostgreSQL database backup operations you performed earlier and use the Cron utility to run the script on a schedule.
Create a new bash script PostgreSQL-backup-script.sh using a text editor such as Nano.
Add the following contents to the file.
Save and close the file.
The above Bash script backs up the full PostgreSQL database and stores the resultant file to a dedicated backup directory and uses s3cmd to upload the latest backup file to the target Vultr Object Storage bucket.
Run the database backup script using Bash.
Monitor the script process and verify that the file upload completes successfully with the following output:
List your Vultr Object Storage bucket contents and verify that a new backups directory is available.
Output:
Open the Crontab editor to create a new Cron job to automate the back up script.
Add the following directive at the end of the file to run the PostgreSQL backup script once every week at 4:00 AM.
Save and close the file.
The above Cron job runs the PostgreSQL database backup script once every 7 days on Sunday at 4:00 AM based on the expression 0 4 * * 0:
0: Represents the minute (0 to 59).4: Represents the hour (0 to 23).*: Sets the day of the month (1-31). The value * runs the command every day or week.*: Sets the month in a calendar year (1 to 12). The value * runs the command every month.0: Sets the day of the week (0-6). The value 0 represents Sunday. Cron also logs all command outputs to the file automatic-backups.log using >> /home/user/PostgreSQLbackups/automatic-backups.log 2>&1.
Create the PostgreSQL database backup log file referenced in Cron.
Long list your PostgreSQL backup directory contents and verify that your non-root user owns all directory files.
Output:
Run the full Cron job command to verify if it works correctly.
View the log file to verify the available script logging information.
Output:
You have enabled automatic PostgreSQL database backups with Vultr Object Storage. You can continuously back up your PostgreSQL databases to ensure that you can recover specific contents in case the database server fails. In addition, you can use automatic backups with high-traffic web applications or large databases that require consistent monitoring to recover your PostgreSQL database or migrate it to a new server.
0 Comments
Be the first to comment and share your perspective with the community.