Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
- export TERM="xterm"
- if [ "$1" = "--auto" ];then
- echo "Doing automatich run..."
- fi
- LOGDIR="/var/log/postgres/"
- mkdir -p $LOGDIR
- LOGFILE="backup-log-`/bin/date +%F_%H-%M-%S`"
- #exec > $LOGDIR/$LOGFILE 2>&1
- FILE="$1"
- WAL_DIR="/restore/walfiles"
- echo "Stopping Postgres on Target..."
- /etc/init.d/postgres stop
- sleep 3
- set -e
- #Find oldest WAL file...
- OLDEST_WAL="`ls -t /restore/walfiles/0*| tail -n 1`"
- if [ ! -e "$OLDEST_WAL" ];then
- echo "Fatal Error: Oldest WAL file $OLDEST_WAL does not exist"
- exit 1
- else
- echo "Found oldest WAL file: $OLDEST_WAL"
- fi
- echo "Delete all files that are older than our wal files..."
- find /restore/basefiles/ -type f ! -newer $OLDEST_WAL ! -wholename $OLDEST_WAL -exec rm -vf {} \;
- ## get oldest base backup file...
- FILE="`cd /restore/basefiles/ ; ls -t 200*.tgz | tail -n 1`"
- FULL_FILE_PATH="/restore/basefiles/"$FILE
- if [ ! -e "$FULL_FILE_PATH" ];then
- echo "Fatal Error: Base-Backup file $FULL_FILE_PATH does not exist"
- exit 1
- else
- echo "Found oldest Base-Backup file: $FULL_FILE_PATH"
- MAX_DATE="`ls -l --time-style='+%F_%Hh-%Mm' $FULL_FILE_PATH| awk '{print $6}'`"
- fi
- if [ "$1" = "--auto" ]; then
- echo "Skipping Date Prompt...doing auto recovery"
- else
- clear
- echo "You should be able to go back until $MAX_DATE"
- echo ""; echo "Press ENTER to recover the latest or enter the date you want to recover (e.g. 2009-04-27 17:23:33):"
- read RECOVER_DATE
- fi
- ## COMPARE the two files. WAL has to be older than the Base-Backup file
- if [ $OLDEST_WAL -ot $FULL_FILE_PATH ]; then
- echo "";
- echo "Last WAL : `date -r $OLDEST_WAL '+%F %Hh %Mm'`: $OLDEST_WAL"
- echo "last Base: `date -r $FULL_FILE_PATH '+%F %Hh %Mm'`: $FULL_FILE_PATH"
- echo "Looks OK. $OLDEST_WAL is older than $FULL_FILE_PATH"
- echo "";
- sleep 3
- else
- echo "Fatal Error: Base Backup file $FULL_FILE_PATH is newer than the WAL file $OLDEST_WAL"
- exit 1
- fi
- echo "";
- echo "Deleting everything in /data/* ..."
- rm -Rf /data/pgsql
- echo "";
- echo "Unzipping Base-Backup file: $FULL_FILE_PATH"
- cd /data/ && /usr/bin/nice -n 19 tar xzf $FULL_FILE_PATH
- echo "";
- echo "Deleting WAL files from Base-Backup..."
- rm -Rf /data/pgsql/pg_xlog/*
- echo "";
- echo "Disabling WAL on Target..."
- cp /data/pgsql/postgresql.conf /data/pgsql/postgresql.conf.org
- cat /data/pgsql/postgresql.conf.org | sed 's/archive_mode = on/archive_mode = off/g' > /data/pgsql/postgresql.conf
- echo "restore_command = 'cp /restore/walfiles/%f %p'" > /data/pgsql/recovery.conf
- if [ -z "$RECOVER_DATE" ]; then
- echo "recovery_target_timeline = 'latest'" >> /data/pgsql/recovery.conf
- else
- echo "recovery_target_timeline = 'latest'" >> /data/pgsql/recovery.conf
- echo "recovery_target_time = '$RECOVER_DATE'" >> /data/pgsql/recovery.conf
- fi
- echo "Starting Postgres database..."
- /etc/init.d/postgres start
- echo ""; echo "Note: Recovery can take a while until you can connect...."
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement