Advertisement
Guest User

Untitled

a guest
Sep 9th, 2017
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 13.52 KB | None | 0 0
  1. #!/bin/bash
  2. # Installer for Percona Xtrabackup wrapper scripts
  3. #
  4. # Created May 25, 2011 by Michael Halpern
  5. #
  6. # Files:
  7. # mysqlbackup-cronjob:  automatically runs a backup at specified times
  8. #           installs to /etc/cron.d/mysqlbackup-cronjob
  9. #
  10. # mysqllogrotate    rotates the log created by the backup script
  11. #           installs to /etc/logrorate.d/mysqllogrotate
  12. #
  13. # mysqlbackup.sh:   Calls Percona innobackupex to create backups
  14. #           installs to $backupdir/mysqlbackup.sh
  15. #
  16. # mysqlrestore.sh:  Restores backed up database
  17. #           installs to /usr/local/sbin/
  18. #
  19. # Requires mysqld, mysqladmin, Percona Xtrabackup
  20. # Only tested on CentOS 5.6.  May not work correctly on other OSes.
  21. #
  22. echo "Checking requirements:"
  23.  
  24. echo "Checking for Xtrabackup..."
  25. if ! hash xtrabackup 2>/dev/null; then
  26.         echo "Xtrabackup not found, exiting"
  27.         exit 1
  28. fi
  29.  
  30. echo "Checking for InnoBackupEx..."
  31. if ! hash innobackupex 2>/dev/null; then
  32.         echo "InnoBackupEx not found, exiting"
  33.         exit 1
  34. fi
  35.  
  36. echo "Checking for mysqld..."
  37. if ! hash mysqld 2>/dev/null; then
  38.         echo "mysqld not found, exiting"
  39.         exit 1
  40. fi
  41.  
  42. echo "Checking for mysqladmin..."
  43. if ! hash mysqladmin 2>/dev/null; then
  44.         echo "mysqladmin not found, exiting"
  45.         exit 1
  46. fi
  47.  
  48. echo "All requirements available."
  49. echo
  50. echo "Starting installation..."
  51.  
  52. #
  53. #Gather info to be used with install#
  54. #
  55.  
  56. echo "You will be asked a series of questions.  The default responses are in brackets [] and will be used if nothing is entered."
  57.  
  58. echo "Enter the location of where to store Database backups [/opt/mysql/innobackup]: "
  59. read backupdir
  60. [ -z "$backupdir" ] && backupdir="/opt/mysql/innobackup"
  61.  
  62. echo "Enter the user MySQL runs as [mysql]: "
  63. read user
  64. [ -z "$user" ] && user="mysql"
  65.  
  66. echo "Enter the group the MySQL user belongs to [$user]: "
  67. read group
  68. [ -z "$group" ] && group="$user"
  69.  
  70. echo "Enter the database user to run backups as (since this backups up ALL databases, "root" is recommended) [root]: "
  71. read dbuser
  72. [ -z "$dbuser" ] && dbuser="root"
  73.  
  74. while [ -z "$pass" ]
  75. do
  76.     echo "Enter the database password for $dbuser (required):"
  77.     read -s pass
  78. done
  79.  
  80. echo "Enter the time (in minutes) that backups will be kept for [10500]: "
  81. read age
  82. [ -z "$age" ] && age="10500"
  83.  
  84. echo "Enter the location and name of the backup script [$backupdir/mysqlbackup.sh]: "
  85. read backupscriptfile
  86. [ -z "$backupscriptfile" ] && backupscriptfile="$backupdir/mysqlbackup.sh"
  87.  
  88. echo "Enter the location and name of the backup log file [$backupdir/mysqlbackup.log]: "
  89. read logfile
  90. [ -z "$logfile" ] && logfile="$backupdir/mysqlbackup.log"
  91.  
  92. echo "Enter the location and name of the restore script [$backupdir/mysqlrestore.sh]: "
  93. read restorescriptfile
  94. [ -z "$restorescriptfile" ] && restorescriptfile="$backupdir/mysqlrestore.sh"
  95.  
  96. #
  97. # Crontab settings
  98. #
  99.  
  100. echo "The following questions deal with when backups will run.  Answers can be given as a range (9-17 to run backups each hour from 9 am to 5 pm), as a comma separated list (0,30 to run backup every half-hour), * (run every day) or the above followed by /<number> (*/30 to run every 30 minutes, days, etc).  Month and Day-of-week values can also be a three-letter abbreviation.  Do not use any spaces.  For more details, see man 5 crontab."
  101.  
  102. echo "Enter the days of the week the backups will run (0-7, 0 and 7 are Sun, or use names)[*]: "
  103. read daysofweek
  104. [ -z "$daysofweek" ] && daysofweek="*"
  105.  
  106. echo "Enter the months the backups will run: (1-12, or use names)[*]: "
  107. read months
  108. [ -z "$months" ] && months="*"
  109.  
  110. echo "Enter the days of each month the backups will run (1-31)[*]: "
  111. read days
  112. [ -z "$days" ] && days="*"
  113.  
  114. echo "Enter the hours the backups will run (0-23)[7,19]: "
  115. read hours
  116. [ -z "$hours" ] && hours="7,19"
  117.  
  118. echo "Enter the minute the backups will run (0-59)[30]: "
  119. read minutes
  120. [ -z "$minutes" ] && minutes="30"
  121.  
  122. #
  123. # Show settings
  124. #
  125. echo "********************************************************************"
  126. echo "          Please verify your settings!"
  127. echo "********************************************************************"
  128.  
  129. echo "Scripts will be installed at $backupscriptfile and $restorescriptfile."
  130. echo "with ownership as $user:$group."
  131. echo "The logfile will be at $logfile."
  132. echo "Backups will be stored at $backupdir and will be deleted when $age minutes old."
  133. echo
  134. echo "Here is the crontab file for /etc/cron.d/mysqlbackup:"
  135. echo "$minutes $hours $days $months $daysofweek root $backupscriptfile >> $logfile"
  136. echo
  137.  
  138. #########################################################################################
  139. #                               Generate cron script                                    #
  140. #########################################################################################
  141.  
  142. echo "$minutes $hours $days $months $daysofweek root $backupscriptfile >> $logfile" > mysqlbackup
  143. [ ! -f mysqlbackup ] && echo "error creating mysqlbackup"
  144.  
  145. #########################################################################################
  146. #                               End cron script                                         #
  147. #########################################################################################
  148.  
  149. echo "Here is the logrotate file for /etc/logrotate.d/mysqllogrotate:"
  150. echo "$logfile {
  151. weekly
  152. rotate 5
  153. compress
  154. notifempty
  155. create 0600 root root
  156. }"
  157. echo
  158.  
  159. #########################################################################################
  160. #                               Generate logrotate script                               #
  161. #########################################################################################
  162.  
  163. echo "$logfile {
  164. weekly
  165. rotate 5
  166. compress
  167. notifempty
  168. create 0600 root root
  169. }" > mysqllogrotate
  170. [ ! -f mysqllogrotate ] && echo "error creating mysqllogrotate"
  171.  
  172.  
  173. #########################################################################################
  174. #                               End logrotate script                                    #
  175. #########################################################################################
  176.  
  177. echo "Please verify the above information is correct.  Press Enter to continue, Control-C to exit"
  178. read
  179.  
  180.  
  181. #########################################################################################
  182. #               Generate backup script                  #
  183. #########################################################################################
  184. (
  185. cat << 'EOF'
  186. #!/bin/sh
  187. #
  188. # Script to run innobackupex script (for all databases on server), check for success, and apply logs to backups.
  189. #
  190.  
  191. # (C)2010 Owen Carter @ Mirabeau BV
  192. # This script is provided as-is; no liability can be accepted for use.
  193. # You are free to modify and reproduce so long as this attribution is preserved.
  194. #
  195.  
  196. INNOBACKUPEX=innobackupex-1.5.1
  197. INNOBACKUPEXFULL="/usr/bin/$INNOBACKUPEX"
  198. USEROPTIONS="--user=123dbuser321"
  199. BACKUPDIR=123backupdir321
  200. TMPFILE="/tmp/innobackupex-runner.$$.tmp"
  201. USER=123user321
  202. # Age of oldest retained backups in minutes.
  203. AGE=123age321
  204.  
  205. # Some info output
  206.  
  207. echo "----------------------------"
  208. echo
  209. echo "innobackupex-runner.sh: MySQL backup script"
  210. echo "started: `date`"
  211. echo
  212.  
  213. # Check options before proceeding
  214.  
  215. if [ ! -x "$INNOBACKUPEXFULL" ]; then
  216.  error
  217.  echo "$INNOBACKUPEXFULL does not exist."; echo
  218.  exit 1
  219. fi
  220.  
  221. if [ ! -d "$BACKUPDIR" ]; then
  222.  error
  223.  echo "Backup destination folder: $BACKUPDIR does not exist."; echo
  224.  exit 1
  225. fi
  226.  
  227. cd "$BACKUPDIR"
  228. #below code seems too fragile and irrelevant
  229. #if [ -z "`/sbin/service mysql status | grep 'MySQL running'`" ] ; then
  230. # echo "HALTED: mysqld does not appear to be running."; echo
  231. # exit 1
  232. #fi
  233.  
  234. if ! $(echo 'exit' | su - "$USER" -c "/usr/bin/mysql -s $USEROPTIONS") ; then
  235.  echo "HALTED: Either MySQL Server is not running or supplied mysql credientials are incorrect (not copied here for security, see script)"; echo
  236.  exit 1
  237. fi
  238.  
  239. # Now run the command to produce the backup; capture it's output.
  240.  
  241. echo "Check completed OK; running $INNOBACKUPEX command."
  242.  
  243. su "$USER" -c "$INNOBACKUPEXFULL $USEROPTIONS $BACKUPDIR" > "$TMPFILE" 2>&1
  244.  
  245. if [ -z "$(tail -1 $TMPFILE | grep 'completed OK!')" ] ; then
  246.  echo "$INNOBACKUPEX failed:"; echo
  247.  echo "---------- ERROR OUTPUT from $INNOBACKUPEX ----------"
  248.  cat "$TMPFILE"
  249.  rm -f "$TMPFILE"
  250.  exit 1
  251. fi
  252.  
  253. THISBACKUP=`awk -- "/Backup created in directory/ { split( \\\$0, p, \"'\" ) ; print p[2] }" $TMPFILE`
  254.  
  255. rm -f "$TMPFILE"
  256.  
  257. echo "Databases backed up successfully to: $THISBACKUP"
  258. echo
  259. echo "Now applying logs to the backuped databases"
  260.  
  261. # Run the command to apply the logfiles to the backup directory.
  262. su "$USER" -c "$INNOBACKUPEXFULL --apply-log $THISBACKUP" > "$TMPFILE" 2>&1
  263.  
  264. if [ -z "$(tail -1 $TMPFILE | grep 'completed OK!')" ] ; then
  265.  echo "$INNOBACKUPEX --apply-log failed:"; echo
  266.  echo "---------- ERROR OUTPUT from $INNOBACKUPEX --apply-log ----------"
  267.  cat "$TMPFILE"
  268.  rm -f "$TMPFILE"
  269.  exit 1
  270. fi
  271.  
  272. echo "Logs applied to backuped databases"
  273. echo
  274.  
  275. # Cleanup
  276.  
  277. echo "Cleaning up old backups (older than $AGE minutes) and temporary files"
  278. rm -f "$TMPFILE"
  279.  
  280. cd /tmp ; find "$BACKUPDIR" -maxdepth 1 -type d -mmin +"$AGE" -exec echo "removing: "{} \; -exec rm -rf {} \;
  281.  
  282.  
  283. if $(selinuxenabled) ; then
  284.         echo "SELinux enabled, restoring contexts on $BACKUPDIR"
  285.         semanage fcontext -a -t mysqld_db_t "$BACKUPDIR"
  286.         restorecon -RF "$BACKUPDIR"
  287. fi
  288.  
  289.  
  290. echo
  291. echo "completed: `date`"
  292. exit 0
  293. EOF
  294. ) > $(basename "$backupscriptfile")
  295.  
  296. [ ! -f $(basename "$backupscriptfile") ] && echo "error creating $(basename $backupscriptfile)"
  297.  
  298. #########################################################################################
  299. #                               End backup script                                   #
  300. #########################################################################################
  301.  
  302.  
  303.  
  304.  
  305. #########################################################################################
  306. #                               Generate restore script                                 #
  307. #########################################################################################
  308.  
  309. (
  310. cat <<'EOF'
  311. #!/bin/bash
  312. #
  313. #########################################################################
  314. #                                                                       #
  315. # Script to restore MySQL Backups created with xtrabackup               #
  316. #                                                                       #
  317. # Usage:                                                                #
  318. # ./innobackupex-restore [backup-dir]                                   #
  319. # If backup-dir is not specified on the commandline, the most recent    #
  320. # backup in $backupdir (defined below) is used.                         #
  321. #########################################################################
  322.  
  323. #### User Settings ###
  324.  
  325. backupdir=/opt/mysql/innobackup
  326. user=123user321
  327. group=123group321
  328.  
  329. dbuser=123dbuser321
  330.  
  331. datadir=$(dirname $(su - "$user" -c "mysqladmin -u $dbuser variables" |awk '/datadir/{ print $4 }')x)
  332.  
  333. if [ -z "$datadir" ] ; then
  334.         echo "Unable to determine data directory.  Is MySQL Server running?"
  335.         exit 1
  336. fi
  337.  
  338. if [ -z "$1" ] ; then
  339.         files=("$backupdir"/*/) restoredir=${files[0]}
  340.         for f in "${files[@]}"; do
  341.                 if [[ "$f" -nt "$restoredir" ]]; then
  342.                         restoredir="$f"
  343.                 fi
  344.         done
  345. else
  346.         restoredir="$1"
  347. fi
  348.  
  349. echo "Restoring from $restoredir.  Press Enter to continue, Control-C to exit"
  350. read
  351.  
  352. if [ ! -d "$restoredir" ] ; then
  353.         echo "$restoredir is not a directory!"
  354.         exit 2
  355. fi
  356.  
  357. echo "Stopping mysqld.";
  358. #ysqladmin -u "$dbuser" shutdown 2>/dev/null
  359. service mysql stop
  360.  
  361. if [ -d "$datadir.bak" ] ; then
  362.         echo "Removing old backup"
  363.         rm -rf "$datadir.bak"
  364. fi
  365.  
  366. echo "Backing up $datadir to $datadir.bak"
  367. mv "$datadir" "$datadir".bak
  368.  
  369. echo "Restoring backup from $restoredir"
  370. mkdir "$datadir"
  371. chown "$user":"$group" "$datadir"
  372. cp -a "$restoredir"/* "$datadir"/
  373.  
  374. if [ "$?" != 0 ] ; then
  375.         echo "Copy failed!"
  376.         exit 3
  377. fi
  378.  
  379. if $(selinuxenabled) ; then
  380.     restorecon -RF "$datadir"
  381. fi
  382.  
  383. echo "Restore complete! Restarting MySQL"
  384.  
  385. /sbin/service mysql start
  386.  
  387. exit 0
  388. EOF
  389. ) > $(basename "$restorescriptfile")
  390.  
  391. [ ! -f $(basename "$restorescriptfile") ] && echo "error creating $(basename $restorescriptfile)"
  392.  
  393. #########################################################################################
  394. #                               End restore script                                      #
  395. #########################################################################################
  396. echo "Modifying files..."
  397.  
  398. sed -i -e "s/123dbuser321/$dbuser/" -e "s/123user321/$user/" -e "s@123backupdir321@$backupdir@" -e "s/123age321/$age/" $(basename "$backupscriptfile")
  399.  
  400. sed -i -e "s/123dbuser321/$dbuser/" -e "s/123user321/$user/" -e "s/123group321/$group/" $(basename "$restorescriptfile")
  401.  
  402.  
  403. echo "Installing files..."
  404. echo "[client]
  405. user=$dbuser
  406. password=$pass" >>~$user/.my.cnf
  407. chown $dbuser:$group ~$user/.my.cnf
  408. chmod 600 ~$user/.my.cnf
  409. install -o root -g root -m 700 mysqlbackup /etc/cron.d/
  410. service crond restart
  411. install -o root -g root -m 700 mysqllogrotate /etc/logrotate.d/
  412. install -o root -g root -m 700 $(basename "$backupscriptfile") "$backupscriptfile"
  413. install -o root -g root -m 700 $(basename "$restorescriptfile") "$restorescriptfile"
  414.  
  415. if $(selinuxenabled) ; then
  416.     echo "SELinux enabled, restoring contexts"
  417.     restorecon -RF $(dirname "$backupscriptfile")
  418.     restorecon -RF $(dirname "$backupscriptfile")
  419.     restorecon -RF /etc/cron.d/
  420.     restorecon -RF /etc/logrotate.d/
  421. fi
  422.  
  423. echo "Done!"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement