Script to take Last 7 Days containerized PostgreSQL Database Backup

 

  1. Create a directory script and create script.sh file :
    mkdir -p /home/script
    vi /home/script/script.sh
  2. Copy below script:

#!/bin/sh
#For taking backup
# create dir db_backup: mkdir -p /home/db_backup/
DIR=/home/db_backup/
DATESTAMP=$(date +%d-%m-%y-%H-%M)
# remove backups older than $DAYS_KEEP
DAYS_KEEP=7
find ${DIR}* -mtime +$DAYS_KEEP -exec rm -f {} \; 2> /dev/null
# Find Container ID where PostgreSQL is running with ancestor=<image name with tag>
CONTAINER_ID=$(docker ps -q --filter ancestor=postgres:10.1)
# DATABBASE name want to take backup
DB=middleware_production_db
# Destination Path
FILENAME=${DIR}${DB}-${DATESTAMP}.sql
#echo $FILENAME
#echo $CONTAINER_ID
docker exec -t ${CONTAINER_ID} pg_dump -U postgres -d ${DB} > ${FILENAME}

SET CRON JOB FOR THIS SCRIPT

Edit Cron job: crontab -e

Copy the following command:
0 8 * * * bash -l /home/script/script.sh

Comments

Popular posts from this blog

Convert .iso Image to .qcow2 Image

How to reset password in single user mode in Ubuntu 18.04

How to set Grub Password in UBUNTU 18.03 (Password protect Boot Loader)