Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- PGBIN=/usr/bin/
- PGDATA=/var/lib/postgresql/10/main;
- #ARCHIVE_HOST=IP
- ARCHIVE_USER=postgres
- ARCHIVE_DIR="/mnt/evergreen/database/snapshot/"
- #ARCHIVE_DST="/mnt/cwmars/snapshot/"
- ARCHIVE_LABEL=`date +mydb-prod-backup-%FT%T`
- ARCHIVE_FILE=$ARCHIVE_LABEL.cpio.gz
- # Make sure we're not overwriting an existing backup
- if [ -e $ARCHIVE_DIR/$ARCHIVE_FILE ]; then
- echo "Cannot create backup: $ARCHIVE_DIR/$ARCHIVE_FILE exists";
- exit;
- fi
- # Tell PG we're starting the backup
- START_RESULT=`$PGBIN/psql -tc "SELECT pg_start_backup('$ARCHIVE_LABEL') IS NOT NULL;"|grep t`
- if [ "_" == "_$START_RESULT" ]; then
- echo "Could not start backup labeled $ARCHIVE_LABEL";
- exit;
- fi
- # Grab the data we need (just copy it locally) ...
- (cd $PGDATA && find . -depth -print | grep -v pg_xlog | cpio -o | gzip > $ARCHIVE_DIR/$ARCHIVE_FILE)
- # ... tell PG we're done ...
- STOP_RESULT=`$PGBIN/psql -tc "SELECT pg_stop_backup() IS NOT NULL;"|grep t`
- if [ "_" == "_$STOP_RESULT" ]; then
- echo "Could not stop backup labeled $ARCHIVE_LABEL";
- exit;
- fi
- if [ "_$ARCHIVE_HOST" != "_" ]; then
- # ... then push it over to the backup host
- scp -q $ARCHIVE_DIR/$ARCHIVE_FILE $ARCHIVE_USER@$ARCHIVE_HOST:$ARCHIVE_DST
- SCP_RES=$?
- if [ "$SCP_RES" != "0" ]; then
- echo "Unable to archive $ARCHIVE_DIR/$ARCHIVE_FILE to $ARCHIVE_USER@$ARCHIVE_HOST:$ARCHIVE_DST!!"
- exit;
- fi
- fi
- echo "Backup of database on $HOSTNAME complete. Backup labeled $ARCHIVE_LABEL stored at $ARCHIVE_HOST:$ARCHIVE_DST."
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement