fbpx

Automate Postgresql Daily Database Backup using Pgbackreast and bash

				
					#!/bin/bash

# Set the backup directory
BACKUP_DIR=/backups

# Set the PGBackRest configuration file path
PGCONF=/etc/pgbackrest.conf

# Set the date format for the backup file name
DATE=$(date +%Y-%m-%d)

# Create the backup directory if it doesn't exist
if [ ! -d "$BACKUP_DIR" ]; then
  mkdir -p $BACKUP_DIR
fi

# Perform a full backup and store it in the backup directory
pgbackrest backup --type=full --target=$BACKUP_DIR/$DATE --config=$PGCONF

				
			

Save this script to a file (e.g., pgbackup.sh), and make it executable using the chmod command:

				
					chmod +x pgbackup.sh

				
			

Then, schedule the script to run daily using a cron job. To do this, open the crontab file using the following command:

				
					crontab -e

				
			

Add the following line to the crontab file to run the backup script at 2:00 AM every day:

				
					0 2 * * * /path/to/pgbackup.sh

				
			

Replace /path/to/pgbackup.sh with the full path to the backup script file.

Save and exit the crontab file. The backup script will now run daily at the scheduled time and store the backups in the specified directory.

Note that this is a basic example, and you may need to modify the script and cron job settings depending on your specific needs and environment. For more information on cron jobs and scheduling tasks in Linux, you can refer to the Linux man pages or online resources.

Share:

Facebook
Twitter
Pinterest
LinkedIn

Social Media

Most Popular

Get The Latest Updates

Subscribe To Our Weekly Newsletter

No spam, notifications only about new products, updates.

Categories