Advertisement
Guest User

mario

a guest
Aug 19th, 2009
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.16 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
  4. export TERM="xterm"
  5.  
  6. if [ "$1" = "--auto" ];then
  7.         echo "Doing automatich run..."
  8. fi
  9.  
  10.  
  11. LOGDIR="/var/log/postgres/"
  12. mkdir -p $LOGDIR
  13.  
  14. LOGFILE="backup-log-`/bin/date +%F_%H-%M-%S`"
  15. #exec > $LOGDIR/$LOGFILE  2>&1
  16.  
  17.  
  18. FILE="$1"
  19. WAL_DIR="/restore/walfiles"
  20.  
  21.  
  22. echo "Stopping Postgres on Target..."
  23. /etc/init.d/postgres stop
  24. sleep 3
  25.  
  26.  
  27. set -e
  28.  
  29. #Find oldest WAL file...
  30. OLDEST_WAL="`ls -t /restore/walfiles/0*| tail -n 1`"
  31.  
  32. if [ ! -e "$OLDEST_WAL" ];then
  33.         echo "Fatal Error: Oldest WAL file $OLDEST_WAL does not exist"
  34.         exit 1
  35. else
  36.         echo "Found oldest WAL file: $OLDEST_WAL"
  37.  
  38. fi
  39.  
  40.  
  41. echo "Delete all files that are older than our wal files..."
  42. find /restore/basefiles/ -type f ! -newer $OLDEST_WAL ! -wholename $OLDEST_WAL -exec rm -vf {} \;
  43.  
  44.  
  45.  
  46. ## get oldest base backup file...
  47. FILE="`cd /restore/basefiles/ ; ls -t 200*.tgz | tail -n 1`"
  48. FULL_FILE_PATH="/restore/basefiles/"$FILE
  49.  
  50. if [ ! -e "$FULL_FILE_PATH" ];then
  51.         echo "Fatal Error: Base-Backup file $FULL_FILE_PATH does not exist"
  52.         exit 1
  53. else
  54.         echo "Found oldest Base-Backup file: $FULL_FILE_PATH"
  55.         MAX_DATE="`ls -l --time-style='+%F_%Hh-%Mm' $FULL_FILE_PATH| awk '{print $6}'`"
  56. fi
  57.  
  58.  
  59. if [ "$1" = "--auto" ]; then
  60.         echo "Skipping Date Prompt...doing auto recovery"
  61. else
  62.  
  63.         clear
  64.         echo "You should be able to go back until $MAX_DATE"
  65.         echo ""; echo "Press ENTER to recover the latest or enter the date you want to recover (e.g. 2009-04-27 17:23:33):"
  66.         read RECOVER_DATE
  67. fi
  68.  
  69.  
  70.  
  71.  
  72.  
  73. ## COMPARE the two files. WAL has to be older than the Base-Backup file
  74. if [ $OLDEST_WAL -ot $FULL_FILE_PATH ]; then
  75.         echo "";
  76.         echo "Last WAL : `date -r $OLDEST_WAL '+%F %Hh %Mm'`: $OLDEST_WAL"
  77.         echo "last Base: `date -r $FULL_FILE_PATH '+%F %Hh %Mm'`: $FULL_FILE_PATH"
  78.         echo "Looks OK. $OLDEST_WAL is older than $FULL_FILE_PATH"
  79.         echo "";
  80.         sleep 3
  81.  
  82. else
  83.         echo "Fatal Error: Base Backup file $FULL_FILE_PATH is newer than the WAL file $OLDEST_WAL"
  84.         exit 1
  85. fi
  86.  
  87. echo "";
  88.  
  89. echo "Deleting everything in /data/* ..."
  90. rm -Rf /data/pgsql
  91. echo "";
  92.  
  93. echo "Unzipping Base-Backup file: $FULL_FILE_PATH"
  94. cd /data/ && /usr/bin/nice -n 19 tar xzf $FULL_FILE_PATH
  95. echo "";
  96.  
  97. echo "Deleting WAL files from Base-Backup..."
  98. rm -Rf /data/pgsql/pg_xlog/*
  99. echo "";
  100.  
  101.  
  102. echo "Disabling WAL on Target..."
  103. cp /data/pgsql/postgresql.conf /data/pgsql/postgresql.conf.org
  104. cat /data/pgsql/postgresql.conf.org | sed 's/archive_mode = on/archive_mode = off/g' > /data/pgsql/postgresql.conf
  105.  
  106.  
  107. echo "restore_command = 'cp /restore/walfiles/%f %p'" > /data/pgsql/recovery.conf
  108.  
  109. if [ -z "$RECOVER_DATE" ]; then
  110.         echo "recovery_target_timeline = 'latest'" >> /data/pgsql/recovery.conf
  111. else
  112.          echo "recovery_target_timeline = 'latest'" >> /data/pgsql/recovery.conf
  113.          echo "recovery_target_time = '$RECOVER_DATE'" >> /data/pgsql/recovery.conf
  114. fi
  115.  
  116. echo "Starting Postgres database..."
  117. /etc/init.d/postgres start
  118.  
  119. echo ""; echo "Note: Recovery can take a while until you can connect...."
  120.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement