Advertisement
Guest User

Untitled

a guest
Nov 3rd, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1.  
  2. #! /bin/bash
  3. # Dijeesh Padinhaethil
  4.  
  5. # Script to take MySQL backups to remote server using Percona Xtrabackup
  6.  
  7.  
  8.  
  9. # Confirm log directory exist, create on if doesn't.
  10.  
  11. if [[ ! -e /var/log/xtrabackup ]]; then
  12. mkdir /var/log/xtrabackup
  13. fi
  14.  
  15.  
  16. # Define ENV Variables
  17. readonly JOB_ID=$(/bin/date +%Y%m%d%H)
  18.  
  19. LOG_DIR="/var/log/xtrabackup"
  20. LOG="$LOG_DIR/${JOB_ID}_$(hostname)_backup.log"
  21. XBLOG="$LOG_DIR/${JOB_ID}_$(hostname)_xb.log"
  22.  
  23. BACKUP_HOST="BACKUP_VAULT_IPADDR"
  24. BACKUP_USER="SSHUSER"
  25. MYSQL_USER="MYSQLUSER"
  26. MYSQL_PASS="MYSQLPASSWORD"
  27. BACKUP_FILE="$(hostname)_$(date +%Y%m%d%H)".tar
  28.  
  29.  
  30.  
  31.  
  32. ## Initiate Log file
  33. echo "$(date +%F-%H:%M:%S) $JOB_ID SSH Starting full backup process, job id $JOB_ID" >> "$LOG"
  34.  
  35. ## Verify SSH access to the MySQL server and exit if no connections can be made.
  36.  
  37. status=$(ssh -o BatchMode=yes -o ConnectTimeout=5 "$BACKUP_USER"@$BACKUP_HOST echo ok 2>&1)
  38. echo "$status"
  39. if [[ $status == ok ]]
  40. then # Proceed with backup process
  41. echo "$(date +%F-%H:%M:%S) $JOB_ID SSH Connection success, proceeding with backup process" >> "$LOG"
  42. innobackupex --user="$MYSQL_USER" --password="$MYSQL_PASS" --stream=tar ./ 2>> "$XBLOG" | ssh "$BACKUP_USER"@"$BACKUP_HOST" \ "cat - > $BACKUP_FILE "
  43.  
  44. # Verify xtrabackup logs and notify if MySQL backup has failed
  45. if [ -z $(tail -1 "$XBLOG"| grep 'completed OK!') ]
  46. then
  47. echo "$(date +%F-%H:%M:%S) $JOB_ID Xtrabackup failed, check backups xtrabackup logs for more information" >> "$LOG"
  48. else
  49. echo "$(date +%F-%H:%M:%S) $JOB_ID backup completes successfully" >> "$LOG"
  50. fi
  51. else
  52. echo "Xtrabackup failed, unable to connect remote server" >> "$LOG"
  53. fi
  54. exit
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement