Skip to content

Instantly share code, notes, and snippets.

@danitfk
Forked from sirbrillig/pgsql_backup.sh
Last active October 20, 2018 17:30
Show Gist options
  • Save danitfk/592caa736635e1977078a2edba863a0b to your computer and use it in GitHub Desktop.
Save danitfk/592caa736635e1977078a2edba863a0b to your computer and use it in GitHub Desktop.
Postgresql daily backup script.
#!/bin/bash
#
# Backup a Postgresql database into a daily file.
#
BACKUP_DIR="$1"
DAYS_TO_KEEP=14
FILE_SUFFIX=_pg_backup_jira.sql
DATABASE=jira
USER=postgres
FILE=`date +"%Y%m%d%H%M"`${FILE_SUFFIX}
TMP_DIR=/tmp/backup
OUTPUT_FILE="${TMP_DIR}/$FILE"
# do the database backup (dump)
# use this command for a database server on localhost. add other options if need be.
function backup_db {
mkdir -p $TMP_DIR
chown $USER:$USER $TMP_DIR
sudo su $USER -c "pg_dump ${DATABASE} -F p -f $OUTPUT_FILE"
gzip $OUTPUT_FILE
mv $TMP_DIR/* $BACKUP_DIR/
rm -rf $TMP_DIR
echo "${OUTPUT_FILE}.gz was created:"
}
function retain_backup_db {
find $BACKUP_DIR -maxdepth 1 -mtime +$DAYS_TO_KEEP -name "*${FILE_SUFFIX}.gz" -exec rm -rf '{}' ';'
}
if [ -d "$1" ]
then
mounted=`df -h | grep NFS | awk {'print $1'} | cut -d":" -f1 | grep 192.168.1.1`
if [ "$mounted" == "" ]
then
echo "NFS not mounted"
exit 1
elese
echo "Run Backup"
backup_db
fi
else
echo "Directory not exists"
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment