Guest User

Untitled

a guest
Feb 14th, 2017
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.18 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. TMPFILE="/tmp/innobackupex-runner.$$.tmp"
  4. USEROPTIONS="--user=XXX --password=XXX"
  5. FILTERTABLES="--include=.*[.].*"
  6. BACKDIR=/var/mysql-bak
  7. BASEBACKDIR=$BACKDIR/base
  8. INCRBACKDIR=$BACKDIR/incr
  9. FULLBACKUPLIFE=3600 #604800 # How long to keep incrementing a backup for, minimum 60
  10. KEEP=1 # Keep this number of backups, appart form the one currently being incremented
  11. START=`date +%s`
  12.  
  13. echo "----------------------------"
  14. echo
  15. echo "innobackupex-runner.sh: MySQL backup script"
  16. echo "started: `date`"
  17. echo
  18.  
  19. # Check base dir exists and is writable
  20. if test ! -d $BASEBACKDIR -o ! -w $BASEBACKDIR
  21. then
  22. error
  23. echo $BASEBACKDIR 'does not exist or is not writable'; echo
  24. exit 1
  25. fi
  26.  
  27. # check incr dir exists and is writable
  28. if test ! -d $INCRBACKDIR -o ! -w $INCRBACKDIR
  29. then
  30. error
  31. echo $INCRBACKDIR 'does not exist or is not writable'; echo
  32. exit 1
  33. fi
  34.  
  35. if [ -z "`mysqladmin $USEROPTIONS status | grep 'Uptime'`" ]
  36. then
  37. echo "HALTED: MySQL does not appear to be running."; echo
  38. exit 1
  39. fi
  40.  
  41. if ! `echo 'exit' | /usr/bin/mysql -s $USEROPTIONS`
  42. then
  43. echo "HALTED: Supplied mysql username or password appears to be incorrect (not copied here for security, see script)"; echo
  44. exit 1
  45. fi
  46.  
  47. echo "Check completed OK"
  48.  
  49. # Find latest backup directory
  50. LATEST=`find $BASEBACKDIR -mindepth 1 -maxdepth 1 -type d -printf "%P\n" | sort -nr | head -1`
  51.  
  52. AGE=`stat -c %Y $BASEBACKDIR/$LATEST`
  53.  
  54. if [ "$LATEST" -a `expr $AGE + $FULLBACKUPLIFE + 5` -ge $START ]
  55. then
  56. echo 'New incremental backup'
  57. # Create an incremental backup
  58.  
  59. # Check incr sub dir exists
  60. # try to create if not
  61. if test ! -d $INCRBACKDIR/$LATEST
  62. then
  63. mkdir $INCRBACKDIR/$LATEST
  64. fi
  65.  
  66. # Check incr sub dir exists and is writable
  67. if test ! -d $INCRBACKDIR/$LATEST -o ! -w $INCRBACKDIR/$LATEST
  68. then
  69. echo $INCRBASEDIR 'does not exist or is not writable'
  70. exit 1
  71. fi
  72.  
  73. LATESTINCR=`find $INCRBACKDIR/$LATEST -mindepth 1 -maxdepth 1 -type d | sort -nr | head -1`
  74. if [ ! $LATESTINCR ]
  75. then
  76. # This is the first incremental backup
  77. INCRBASEDIR=$BASEBACKDIR/$LATEST
  78. else
  79. # This is a 2+ incremental backup
  80. INCRBASEDIR=$LATESTINCR
  81. fi
  82.  
  83. # Create incremental Backup
  84. innobackupex $USEROPTIONS $FILTERTABLES --incremental $INCRBACKDIR/$LATEST --incremental-basedir=$INCRBASEDIR > $TMPFILE 2>&1
  85. else
  86. echo 'New full backup'
  87. # Create a new full backup
  88. innobackupex $USEROPTIONS $FILTERTABLES $BASEBACKDIR > $TMPFILE 2>&1
  89. fi
  90.  
  91. if [ -z "`tail -1 $TMPFILE | grep 'completed OK!'`" ]
  92. then
  93. echo "$INNOBACKUPEX failed:"; echo
  94. echo "---------- ERROR OUTPUT from $INNOBACKUPEX ----------"
  95. cat $TMPFILE
  96. rm -f $TMPFILE
  97. exit 1
  98. fi
  99.  
  100. THISBACKUP=`awk -- "/Backup created in directory/ { split( \\\$0, p, \"'\" ) ; print p[2] }" $TMPFILE`
  101.  
  102. echo "Databases backed up successfully to: $THISBACKUP"
  103. echo
  104.  
  105. MINS=$(($FULLBACKUPLIFE * ($KEEP + 1 ) / 60))
  106. echo "Cleaning up old backups (older than $MINS minutes) and temporary files"
  107.  
  108. # Delete tmp file
  109. rm -f $TMPFILE
  110. # Delete old bakcups
  111. for DEL in `find $BASEBACKDIR -mindepth 1 -maxdepth 1 -type d -mmin +$MINS -printf "%P\n"`
  112. do
  113. echo "deleting $DEL"
  114. rm -rf $BASEBACKDIR/$DEL
  115. rm -rf $INCRBACKDIR/$DEL
  116. done
  117.  
  118.  
  119. SPENT=$(((`date +%s` - $START) / 60))
  120. echo
  121. echo "took $SPENT minutes"
  122. echo "completed: `date`"
  123. exit 0
Add Comment
Please, Sign In to add comment