daily pastebin goal
90%
SHARE
TWEET

Untitled

a guest May 26th, 2018 88 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/sh
  2.  
  3. TMPFILE="/tmp/xtrabackup-runner.$$.tmp"
  4. USEROPTIONS="--user=${MYSQL_USER} --password=${MYSQL_PASSWORD} --host=${MYSQL_HOST}"
  5. BACKDIR=/srv/mysql-bak
  6. BASEBACKDIR=$BACKDIR/base
  7. INCRBACKDIR=$BACKDIR/incr
  8. FULLBACKUPCYCLE=604800 # Create a new full backup every X seconds
  9. KEEP=1 # Number of additional backups cycles a backup should kept for.
  10. START=`date +%s`
  11.  
  12. echo "----------------------------"
  13. echo
  14. echo " run-xtrabackup.sh: MySQL backup script"
  15. echo "started: `date`"
  16. echo
  17.  
  18. if test ! -d $BASEBACKDIR
  19. then
  20.   mkdir -p $BASEBACKDIR
  21. fi
  22.  
  23. # Check base dir exists and is writable
  24. if test ! -d $BASEBACKDIR -o ! -w $BASEBACKDIR
  25. then
  26.   error
  27.   echo $BASEBACKDIR 'does not exist or is not writable'; echo
  28.   exit 1
  29. fi
  30.  
  31. if test ! -d $INCRBACKDIR
  32. then
  33.   mkdir -p $INCRBACKDIR
  34. fi
  35.  
  36. # check incr dir exists and is writable
  37. if test ! -d $INCRBACKDIR -o ! -w $INCRBACKDIR
  38. then
  39.   error
  40.   echo $INCRBACKDIR 'does not exist or is not writable'; echo
  41.   exit 1
  42. fi
  43.  
  44. if [ -z "`mysqladmin $USEROPTIONS status | grep 'Uptime'`" ]
  45. then
  46.   echo "HALTED: MySQL does not appear to be running."; echo
  47.   exit 1
  48. fi
  49.  
  50. if ! `echo 'exit' | /usr/bin/mysql -s $USEROPTIONS`
  51. then
  52.   echo "HALTED: Supplied mysql username or password appears to be incorrect (not copied here for security, see script)"; echo
  53.   exit 1
  54. fi
  55.  
  56. echo "Check completed OK"
  57.  
  58. # Find latest backup directory
  59. LATEST=`find $BASEBACKDIR -mindepth 1 -maxdepth 1 -type d -printf "%P\n" | sort -nr | head -1`
  60.  
  61. AGE=`stat -c %Y $BASEBACKDIR/$LATEST`
  62.  
  63. if [ "$LATEST" -a `expr $AGE + $FULLBACKUPCYCLE + 5` -ge $START ]
  64. then
  65.   echo 'New incremental backup'
  66.   # Create an incremental backup
  67.  
  68.   # Check incr sub dir exists
  69.   # try to create if not
  70.   if test ! -d $INCRBACKDIR/$LATEST
  71.   then
  72.     mkdir -p $INCRBACKDIR/$LATEST
  73.   fi
  74.  
  75.   # Check incr sub dir exists and is writable
  76.   if test ! -d $INCRBACKDIR/$LATEST -o ! -w $INCRBACKDIR/$LATEST
  77.   then
  78.     echo $INCRBACKDIR/$LATEST 'does not exist or is not writable'
  79.     exit 1
  80.   fi
  81.  
  82.   LATESTINCR=`find $INCRBACKDIR/$LATEST -mindepth 1  -maxdepth 1 -type d | sort -nr | head -1`
  83.   if [ ! $LATESTINCR ]
  84.   then
  85.     # This is the first incremental backup
  86.     INCRBASEDIR=$BASEBACKDIR/$LATEST
  87.   else
  88.     # This is a 2+ incremental backup
  89.     INCRBASEDIR=$LATESTINCR
  90.   fi
  91.  
  92.   TARGETDIR=$INCRBACKDIR/$LATEST/`date +%F_%H-%M-%S`
  93.  
  94.   # Create incremental Backup
  95.   xtrabackup --backup $USEROPTIONS --target-dir=$TARGETDIR --incremental-basedir=$INCRBASEDIR > $TMPFILE 2>&1
  96. else
  97.   echo 'New full backup'
  98.  
  99.   TARGETDIR=$BASEBACKDIR/`date +%F_%H-%M-%S`
  100.  
  101.   # Create a new full backup
  102.   xtrabackup --backup $USEROPTIONS --target-dir=$TARGETDIR > $TMPFILE 2>&1
  103. fi
  104.  
  105. if [ -z "`tail -1 $TMPFILE | grep 'completed OK!'`" ]
  106. then
  107.   echo "xtrabackup failed:"; echo
  108.   echo "---------- ERROR OUTPUT from xtrabackup ----------"
  109.   cat $TMPFILE
  110.   rm -f $TMPFILE
  111.   exit 1
  112. fi
  113.  
  114. THISBACKUP=`awk -- "/Backup created in directory/ { split( \\\$0, p, \"'\" ) ; print p[2] }" $TMPFILE`
  115.  
  116. echo "Databases backed up successfully to: $THISBACKUP"
  117. echo
  118.  
  119. MINS=$(($FULLBACKUPCYCLE * ($KEEP + 1 ) / 60))
  120. echo "Cleaning up old backups (older than $MINS minutes) and temporary files"
  121.  
  122. # Delete tmp file
  123. rm -f $TMPFILE
  124. # Delete old bakcups
  125. for DEL in `find $BASEBACKDIR -mindepth 1 -maxdepth 1 -type d -mmin +$MINS -printf "%P\n"`
  126. do
  127.   echo "deleting $DEL"
  128.   rm -rf $BASEBACKDIR/$DEL
  129.   rm -rf $INCRBACKDIR/$DEL
  130. done
  131.  
  132.  
  133. SPENT=$(((`date +%s` - $START) / 60))
  134. echo
  135. echo "took $SPENT minutes"
  136. echo "completed: `date`"
  137. exit 0
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top