Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # Installer for Percona Xtrabackup wrapper scripts
- #
- # Created May 25, 2011 by Michael Halpern
- #
- # Files:
- # mysqlbackup-cronjob: automatically runs a backup at specified times
- # installs to /etc/cron.d/mysqlbackup-cronjob
- #
- # mysqllogrotate rotates the log created by the backup script
- # installs to /etc/logrorate.d/mysqllogrotate
- #
- # mysqlbackup.sh: Calls Percona innobackupex to create backups
- # installs to $backupdir/mysqlbackup.sh
- #
- # mysqlrestore.sh: Restores backed up database
- # installs to /usr/local/sbin/
- #
- # Requires mysqld, mysqladmin, Percona Xtrabackup
- # Only tested on CentOS 5.6. May not work correctly on other OSes.
- #
- echo "Checking requirements:"
- echo "Checking for Xtrabackup..."
- if ! hash xtrabackup 2>/dev/null; then
- echo "Xtrabackup not found, exiting"
- exit 1
- fi
- echo "Checking for InnoBackupEx..."
- if ! hash innobackupex 2>/dev/null; then
- echo "InnoBackupEx not found, exiting"
- exit 1
- fi
- echo "Checking for mysqld..."
- if ! hash mysqld 2>/dev/null; then
- echo "mysqld not found, exiting"
- exit 1
- fi
- echo "Checking for mysqladmin..."
- if ! hash mysqladmin 2>/dev/null; then
- echo "mysqladmin not found, exiting"
- exit 1
- fi
- echo "All requirements available."
- echo
- echo "Starting installation..."
- #
- #Gather info to be used with install#
- #
- echo "You will be asked a series of questions. The default responses are in brackets [] and will be used if nothing is entered."
- echo "Enter the location of where to store Database backups [/opt/mysql/innobackup]: "
- read backupdir
- [ -z "$backupdir" ] && backupdir="/opt/mysql/innobackup"
- echo "Enter the user MySQL runs as [mysql]: "
- read user
- [ -z "$user" ] && user="mysql"
- echo "Enter the group the MySQL user belongs to [$user]: "
- read group
- [ -z "$group" ] && group="$user"
- echo "Enter the database user to run backups as (since this backups up ALL databases, "root" is recommended) [root]: "
- read dbuser
- [ -z "$dbuser" ] && dbuser="root"
- while [ -z "$pass" ]
- do
- echo "Enter the database password for $dbuser (required):"
- read -s pass
- done
- echo "Enter the time (in minutes) that backups will be kept for [10500]: "
- read age
- [ -z "$age" ] && age="10500"
- echo "Enter the location and name of the backup script [$backupdir/mysqlbackup.sh]: "
- read backupscriptfile
- [ -z "$backupscriptfile" ] && backupscriptfile="$backupdir/mysqlbackup.sh"
- echo "Enter the location and name of the backup log file [$backupdir/mysqlbackup.log]: "
- read logfile
- [ -z "$logfile" ] && logfile="$backupdir/mysqlbackup.log"
- echo "Enter the location and name of the restore script [$backupdir/mysqlrestore.sh]: "
- read restorescriptfile
- [ -z "$restorescriptfile" ] && restorescriptfile="$backupdir/mysqlrestore.sh"
- #
- # Crontab settings
- #
- 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."
- echo "Enter the days of the week the backups will run (0-7, 0 and 7 are Sun, or use names)[*]: "
- read daysofweek
- [ -z "$daysofweek" ] && daysofweek="*"
- echo "Enter the months the backups will run: (1-12, or use names)[*]: "
- read months
- [ -z "$months" ] && months="*"
- echo "Enter the days of each month the backups will run (1-31)[*]: "
- read days
- [ -z "$days" ] && days="*"
- echo "Enter the hours the backups will run (0-23)[7,19]: "
- read hours
- [ -z "$hours" ] && hours="7,19"
- echo "Enter the minute the backups will run (0-59)[30]: "
- read minutes
- [ -z "$minutes" ] && minutes="30"
- #
- # Show settings
- #
- echo "********************************************************************"
- echo " Please verify your settings!"
- echo "********************************************************************"
- echo "Scripts will be installed at $backupscriptfile and $restorescriptfile."
- echo "with ownership as $user:$group."
- echo "The logfile will be at $logfile."
- echo "Backups will be stored at $backupdir and will be deleted when $age minutes old."
- echo
- echo "Here is the crontab file for /etc/cron.d/mysqlbackup:"
- echo "$minutes $hours $days $months $daysofweek root $backupscriptfile >> $logfile"
- echo
- #########################################################################################
- # Generate cron script #
- #########################################################################################
- echo "$minutes $hours $days $months $daysofweek root $backupscriptfile >> $logfile" > mysqlbackup
- [ ! -f mysqlbackup ] && echo "error creating mysqlbackup"
- #########################################################################################
- # End cron script #
- #########################################################################################
- echo "Here is the logrotate file for /etc/logrotate.d/mysqllogrotate:"
- echo "$logfile {
- weekly
- rotate 5
- compress
- notifempty
- create 0600 root root
- }"
- echo
- #########################################################################################
- # Generate logrotate script #
- #########################################################################################
- echo "$logfile {
- weekly
- rotate 5
- compress
- notifempty
- create 0600 root root
- }" > mysqllogrotate
- [ ! -f mysqllogrotate ] && echo "error creating mysqllogrotate"
- #########################################################################################
- # End logrotate script #
- #########################################################################################
- echo "Please verify the above information is correct. Press Enter to continue, Control-C to exit"
- read
- #########################################################################################
- # Generate backup script #
- #########################################################################################
- (
- cat << 'EOF'
- #!/bin/sh
- #
- # Script to run innobackupex script (for all databases on server), check for success, and apply logs to backups.
- #
- # (C)2010 Owen Carter @ Mirabeau BV
- # This script is provided as-is; no liability can be accepted for use.
- # You are free to modify and reproduce so long as this attribution is preserved.
- #
- INNOBACKUPEX=innobackupex-1.5.1
- INNOBACKUPEXFULL="/usr/bin/$INNOBACKUPEX"
- USEROPTIONS="--user=123dbuser321"
- BACKUPDIR=123backupdir321
- TMPFILE="/tmp/innobackupex-runner.$$.tmp"
- USER=123user321
- # Age of oldest retained backups in minutes.
- AGE=123age321
- # Some info output
- echo "----------------------------"
- echo
- echo "innobackupex-runner.sh: MySQL backup script"
- echo "started: `date`"
- echo
- # Check options before proceeding
- if [ ! -x "$INNOBACKUPEXFULL" ]; then
- error
- echo "$INNOBACKUPEXFULL does not exist."; echo
- exit 1
- fi
- if [ ! -d "$BACKUPDIR" ]; then
- error
- echo "Backup destination folder: $BACKUPDIR does not exist."; echo
- exit 1
- fi
- cd "$BACKUPDIR"
- #below code seems too fragile and irrelevant
- #if [ -z "`/sbin/service mysql status | grep 'MySQL running'`" ] ; then
- # echo "HALTED: mysqld does not appear to be running."; echo
- # exit 1
- #fi
- if ! $(echo 'exit' | su - "$USER" -c "/usr/bin/mysql -s $USEROPTIONS") ; then
- echo "HALTED: Either MySQL Server is not running or supplied mysql credientials are incorrect (not copied here for security, see script)"; echo
- exit 1
- fi
- # Now run the command to produce the backup; capture it's output.
- echo "Check completed OK; running $INNOBACKUPEX command."
- su "$USER" -c "$INNOBACKUPEXFULL $USEROPTIONS $BACKUPDIR" > "$TMPFILE" 2>&1
- if [ -z "$(tail -1 $TMPFILE | grep 'completed OK!')" ] ; then
- echo "$INNOBACKUPEX failed:"; echo
- echo "---------- ERROR OUTPUT from $INNOBACKUPEX ----------"
- cat "$TMPFILE"
- rm -f "$TMPFILE"
- exit 1
- fi
- THISBACKUP=`awk -- "/Backup created in directory/ { split( \\\$0, p, \"'\" ) ; print p[2] }" $TMPFILE`
- rm -f "$TMPFILE"
- echo "Databases backed up successfully to: $THISBACKUP"
- echo
- echo "Now applying logs to the backuped databases"
- # Run the command to apply the logfiles to the backup directory.
- su "$USER" -c "$INNOBACKUPEXFULL --apply-log $THISBACKUP" > "$TMPFILE" 2>&1
- if [ -z "$(tail -1 $TMPFILE | grep 'completed OK!')" ] ; then
- echo "$INNOBACKUPEX --apply-log failed:"; echo
- echo "---------- ERROR OUTPUT from $INNOBACKUPEX --apply-log ----------"
- cat "$TMPFILE"
- rm -f "$TMPFILE"
- exit 1
- fi
- echo "Logs applied to backuped databases"
- echo
- # Cleanup
- echo "Cleaning up old backups (older than $AGE minutes) and temporary files"
- rm -f "$TMPFILE"
- cd /tmp ; find "$BACKUPDIR" -maxdepth 1 -type d -mmin +"$AGE" -exec echo "removing: "{} \; -exec rm -rf {} \;
- if $(selinuxenabled) ; then
- echo "SELinux enabled, restoring contexts on $BACKUPDIR"
- semanage fcontext -a -t mysqld_db_t "$BACKUPDIR"
- restorecon -RF "$BACKUPDIR"
- fi
- echo
- echo "completed: `date`"
- exit 0
- EOF
- ) > $(basename "$backupscriptfile")
- [ ! -f $(basename "$backupscriptfile") ] && echo "error creating $(basename $backupscriptfile)"
- #########################################################################################
- # End backup script #
- #########################################################################################
- #########################################################################################
- # Generate restore script #
- #########################################################################################
- (
- cat <<'EOF'
- #!/bin/bash
- #
- #########################################################################
- # #
- # Script to restore MySQL Backups created with xtrabackup #
- # #
- # Usage: #
- # ./innobackupex-restore [backup-dir] #
- # If backup-dir is not specified on the commandline, the most recent #
- # backup in $backupdir (defined below) is used. #
- #########################################################################
- #### User Settings ###
- backupdir=/opt/mysql/innobackup
- user=123user321
- group=123group321
- dbuser=123dbuser321
- datadir=$(dirname $(su - "$user" -c "mysqladmin -u $dbuser variables" |awk '/datadir/{ print $4 }')x)
- if [ -z "$datadir" ] ; then
- echo "Unable to determine data directory. Is MySQL Server running?"
- exit 1
- fi
- if [ -z "$1" ] ; then
- files=("$backupdir"/*/) restoredir=${files[0]}
- for f in "${files[@]}"; do
- if [[ "$f" -nt "$restoredir" ]]; then
- restoredir="$f"
- fi
- done
- else
- restoredir="$1"
- fi
- echo "Restoring from $restoredir. Press Enter to continue, Control-C to exit"
- read
- if [ ! -d "$restoredir" ] ; then
- echo "$restoredir is not a directory!"
- exit 2
- fi
- echo "Stopping mysqld.";
- #ysqladmin -u "$dbuser" shutdown 2>/dev/null
- service mysql stop
- if [ -d "$datadir.bak" ] ; then
- echo "Removing old backup"
- rm -rf "$datadir.bak"
- fi
- echo "Backing up $datadir to $datadir.bak"
- mv "$datadir" "$datadir".bak
- echo "Restoring backup from $restoredir"
- mkdir "$datadir"
- chown "$user":"$group" "$datadir"
- cp -a "$restoredir"/* "$datadir"/
- if [ "$?" != 0 ] ; then
- echo "Copy failed!"
- exit 3
- fi
- if $(selinuxenabled) ; then
- restorecon -RF "$datadir"
- fi
- echo "Restore complete! Restarting MySQL"
- /sbin/service mysql start
- exit 0
- EOF
- ) > $(basename "$restorescriptfile")
- [ ! -f $(basename "$restorescriptfile") ] && echo "error creating $(basename $restorescriptfile)"
- #########################################################################################
- # End restore script #
- #########################################################################################
- echo "Modifying files..."
- sed -i -e "s/123dbuser321/$dbuser/" -e "s/123user321/$user/" -e "s@123backupdir321@$backupdir@" -e "s/123age321/$age/" $(basename "$backupscriptfile")
- sed -i -e "s/123dbuser321/$dbuser/" -e "s/123user321/$user/" -e "s/123group321/$group/" $(basename "$restorescriptfile")
- echo "Installing files..."
- echo "[client]
- user=$dbuser
- password=$pass" >>~$user/.my.cnf
- chown $dbuser:$group ~$user/.my.cnf
- chmod 600 ~$user/.my.cnf
- install -o root -g root -m 700 mysqlbackup /etc/cron.d/
- service crond restart
- install -o root -g root -m 700 mysqllogrotate /etc/logrotate.d/
- install -o root -g root -m 700 $(basename "$backupscriptfile") "$backupscriptfile"
- install -o root -g root -m 700 $(basename "$restorescriptfile") "$restorescriptfile"
- if $(selinuxenabled) ; then
- echo "SELinux enabled, restoring contexts"
- restorecon -RF $(dirname "$backupscriptfile")
- restorecon -RF $(dirname "$backupscriptfile")
- restorecon -RF /etc/cron.d/
- restorecon -RF /etc/logrotate.d/
- fi
- echo "Done!"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement