Advertisement
Guest User

Untitled

a guest
Mar 21st, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.29 KB | None | 0 0
  1. #!/bin/bash
  2. CLIENT=" <description for the client who we make backup for> "
  3.  
  4. SRC_IP_ADD= <ip_address>
  5. SRC_NAS_BACKUP="/backup-nas"
  6. DST_PG_BACKUP="/tmp/postgres/backuprestore"
  7. NAS_BACKUP_DB="backup-db"
  8.  
  9. ### Operational directory, eg. if we have ssd drive, opinting to it, will
  10. ### significally make process faster !
  11. OPER_DIR="/ssd/postgres/backuprestore"
  12.  
  13. LOG_DIR="/root/scripts/logs"
  14.  
  15. ### here, we specify postgres database password in a file called "DBprod"
  16. cat /root/scripts/sifre/DBprod | sshfs $SRC_IP_ADD:$SRC_NAS_BACKUP $DST_PG_BACKUP -o password_stdin
  17.  
  18. ### sshfs is mounting in the following mount point : $DST_PG_BACKUP
  19. ### and after, we're going in it :
  20. cd $DST_PG_BACKUP
  21. cd $NAS_BACKUP_DB
  22.  
  23. ###
  24. echo "PostgresSQL database backup from a production, started : `date '+[%F] - [%X]'` " >> "$LOG_DIR/pg_restore.log"
  25. cat "$LOG_DIR/pg_restore.log" | tail -n 1 | sh /root/scripts/mail/posaljimail44.sh "dobri7@gmail.com" "$CLIENT, Databse backup started"
  26.  
  27. ### Copy the latest backup file in operational directoy ...
  28. cp -pv "`ls -tr | tail -1`" $OPER_DIR
  29.  
  30. ### The full path for the backup file with "gz" extension :
  31. ZIPPED_BACKUP_FILE="$OPER_DIR/`ls -tr | tail -1`"
  32.  
  33. echo "Transfer for the production backup file completed : `date '+[%F] - [%X]'` " >> "$LOG_DIR/pg_restore.log"
  34.  
  35. ### Current folder MUST be changed, otherwise, unmount of the $DST_PG_BACKUP will fail
  36. cd $OPER_DIR
  37. fusermount -u $DST_PG_BACKUP
  38.  
  39.  
  40. ##################
  41. # #
  42. # DB RESTORE #
  43. # #
  44. ##################
  45.  
  46. PGHOST="localhost"
  47. PGUSER="postgres"
  48. PGPASSWORD=" <postgres password> "
  49. export PGPASSWORD
  50.  
  51. echo "Database restore, start time : `date '+[%F] - [%X]'` " >> "$LOG_DIR/pg_restore.log"
  52.  
  53. yesterdayDate=`date -d "1 days ago" +"%d-%m"`
  54. UNZIPPED_BACKUP_FILE="$OPER_DIR/RC-$yesterdayDate".back
  55.  
  56. nice -n 19 gunzip -c $ZIPPED_BACKUP_FILE > $UNZIPPED_BACKUP_FILE
  57. createdb "RC-$yesterdayDate" -h localhost -U postgres
  58. psql "RC-$yesterdayDate" -h $PGHOST -U $PGUSER -f $UNZIPPED_BACKUP_FILE
  59.  
  60. rm -f $ZIPPED_BACKUP_FILE
  61. rm -f $UNZIPPED_BACKUP_FILE
  62. PGPASSWORD=""
  63.  
  64. echo "Database restore, completition time : `date '+[%F] - [%X]'` " >> "$LOG_DIR/pg_restore.log"
  65.  
  66. ### E-mail notification, last 4 lines of the log file :
  67. cat $LOG_DIR/pg_restore.log | tail -n 4 | sh /root/scripts/mail/posaljimail44.sh "email-address" "$CLIENT, database restore completed."
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement