Posts

Showing posts with the label Script to take Last 7 Days containerized PostgreSQL Database Backup

Script to take Last 7 Days containerized PostgreSQL Database Backup

  Create a directory script and create script.sh file : mkdir -p /home/script vi /home/script/script.sh C opy 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 fol l owing command: 0 8 * * * bash -l /home/script/script.sh