Labels

freebsd (12) AWS (9) linux (8) mysql (8) EC2 (7) auto-scaling (7) php (7) unix (7) howto (5) amazon (4) example (4) php5.3 (4) /usr/ports (3) backup (3) bash (3) cloud (3) cloudwatch (3) ffmpeg (3) loadbalancer (3) mycld (3) patch (3) php53 (3) scaling (3) scalr (3) security (3) spot instances (3) sql (3) tutorial (3) AWSSDKforPHP (2) CentOS (2) WSDL (2) ZEND framework (2) alarms (2) bug (2) cache (2) cloudfusion (2) console (2) exploit (2) fileinfo (2) fix (2) lauchconfig (2) mysqldump (2) mysqlse (2) php-fpm (2) policies (2) ports (2) portupgrade (2) redhat (2) sed (2) shell (2) sphinx (2) sphinxse (2) video (2) x264 (2) /etc/motd (1) 9.x (1) ELB (1) MP4Box (1) MySQL status codes HY000 sqlstate sql states (1) OS (1) RFC (1) SIP (1) URI (1) alias (1) apache (1) banner (1) big databases (1) blog (1) bsd (1) bugtraq (1) checklist (1) chkconfig (1) clean (1) cleaning (1) clear (1) code (1) conf (1) configs (1) configuration (1) content type (1) converting (1) core (1) cron (1) cve (1) database (1) debian (1) deinterlasing (1) dependency (1) disable (1) dump (1) editor (1) editors (1) email (1) encoding char utf8 latin1 script linux bash (1) error (1) escaping (1) exim (1) expat (1) exploits (1) extensions (1) faac (1) fastcgi (1) feed (1) figlet (1) fps (1) freebsd-update (1) freeswitch (1) fresh installation (1) gcc (1) geo (1) gop (1) gpac (1) grep (1) h.264 (1) h264 (1) hitrate (1) hostname (1) incremental (1) init (1) init.d (1) innobackupex (1) ivp6 (1) keyint_min (1) libx264 (1) libxml2 (1) list (1) mail (1) make (1) map (1) maxmind (1) mencoder (1) mime (1) mindmeister (1) mindmeister.com (1) mp4 (1) mplayer (1) mta queue (1) mysql charset (1) mysql charset encoding char utf8 latin1 script linux bash (1) networking (1) nginx (1) nmap (1) optimization (1) paranoia (1) paranoiac (1) percona (1) performance (1) portdowngrade (1) portsnap (1) postgre (1) postgres (1) postgresql (1) presets (1) putty (1) query cache (1) rc (1) rc.conf (1) rc.d (1) remote (1) restore (1) rhel (1) rhel5 (1) script (1) scrubing (1) seclist (1) seclist.org (1) seek (1) seeking (1) segfault (1) slow loris (1) slowloris (1) smtp (1) software (1) sphinxsearch (1) stty (1) terminal (1) time (1) timezone (1) tmp (1) transcoding (1) tty (1) update (1) video transcoding (1) vulnerabilities (1) wal-e (1) web (1) web servers (1) wordpress (1) x86_64 (1) xtrabackup (1) yum (1) zend (1)

Friday, 17 February 2012

Automatic incremental MySQL backup with xtrabackup.

#!/bin/bash

now=`date +%Y-%m-%d_%H-%M-%S`
backup_directory="/media/data/backup"
current_backup=$backup_directory/$now"_full"

logfile="/var/log/db-backup/"$now"_full"

mysql_user="root"
mysql_pass=""


echo "========================================="
echo "        MySQL Data Backup (Full)"
echo "========================================="

#perform full backup
innobackupex --ibbackup=xtrabackup_55 --parallel=4 --user=$mysql_user --password=$mysql_pass --no-timestamp $current_backup 2> $logfile

# see if the backup completed sucsessfully
if ! tail $logfile -n 1 | grep completed.OK
then
        rm -R $current_backup
        echo "\nERROR: backup failed! (log: "$logfile")"
  exit 1
fi

echo $current_backup > $backup_directory/last_backup

echo "Backup Completed OK!"


#!/bin/bash

now=`date +%Y-%m-%d_%H-%M-%S`
backup_directory="/media/data/backup"
current_backup=$backup_directory/$now"_in"
last_backup=`tail $backup_directory/last_backup -n1`

logfile="/var/log/db-backup/"$now"_in"

mysql_user="root"
mysql_pass=""


echo "========================================="
echo "      MySQL Data Backup (Incremental)"
echo "========================================="

#perform incremental backup from where the last backup stoped
innobackupex --ibbackup=xtrabackup_55 --parallel=4 --user=$mysql_user --password=$mysql_pass --no-timestamp --incremental --incremental-basedir=$last_backup $current_backup 2> $logfile

# see if the backup completed sucsessfully
if ! tail $logfile -n 1 | grep completed.OK
then
        rm -R $current_backup
        echo "\nERROR: backup failed! (log: "$logfile")"
  exit 1
fi

echo $current_backup >> $backup_directory/last_backup

echo "Backup Completed OK!"

#!/bin/bash

export PATH=$PATH:/usr/local/mysql/bin

echo "========================================="
echo "           MySQL Data Restore"
echo "========================================="

if [ $# -ne 1 ];
then
 echo "ERROR: missing parameter. Needs a filename that contains a list of backups to restore...."
exit 1
fi

full_backup=`head $1 -n 1`

active_backup="/media/data/backup/active_restore"
active_incremental="/media/data/backup/active_incremental"

logfile="/var/log/db-backup/restore_"`date +%Y-%m-%d_%H-%M-%S`

mysql_user="innobackup"
mysql_pass="b@ckstabber"
mysql_data_path="/media/data/mysql"
mysql_log_path="/media/logs/mysql"


if [ -d $active_backup ];
then
 rm -R $active_backup
fi

echo "copying full backup files: " $full_backup
mkdir $active_backup
cp -r $full_backup/* $active_backup

echo "preparing full backup..."
innobackupex --ibbackup=xtrabackup_55 --parallel=4 --user=$mysql_user --password=$mysql_pass --apply-log --redo-only $active_backup 2> $logfile

# see if the apply-log completed sucsessfully
if ! tail $logfile -n 1 | grep completed.OK
then
        echo "\nERROR: restore failed! (log: "$logfile")"
        rm -R $active_backup
        exit 1
fi


grep _in $1 | while read incremental_backup ; do

 if [ -d $active_incremental ];
 then
  rm -R $active_incremental
 fi

 echo "copying incremental backup: " $incremental_backup
 mkdir $active_incremental
 cp -r $incremental_backup/* $active_incremental

 find $incremental_backup -name .delta -empty -maxdepth 2 -exec rm {} \;

        echo "preparing incremetal backup..."
 innobackupex --ibbackup=xtrabackup_55 --parallel=4 --user=$mysql_user --password=$mysql_pass --apply-log --redo-only $active_backup --incremental-dir=$active_incremental 2>> $logfile

 rm -R $active_incremental

 # see if the apply incremental completed sucsessfully
 if ! tail $logfile -n 1 | grep completed.OK
 then
         echo "\nERROR: restore failed! (log: "$logfile")"
         rm -R $active_backup
         exit 1
 fi

done

echo "applying Logs"
innobackupex --ibbackup=xtrabackup_55 --parallel=4 --user=$mysql_user --password=$mysql_pass --apply-log $active_backup 2>> $logfile

# see if the apply log completed sucsessfully
if ! tail $logfile -n 1 | grep completed.OK
then
 echo "\nERROR: restore failed! (log: "$logfile")"
        rm -R $active_backup
        exit 1
fi

service mysql stop

echo "deleting mysql data files..."
rm -R $mysql_data_path/*


echo "restoring data files..."

#can use the innobackup script to restore data
#innobackupex --ibbackup=xtrabackup_55 --user=$mysql_user --password=$mysql_pass --copy-back $active_backup 2>> $logfile

#or just copy the data directory back
mv $active_backup/* $mysql_data_path/.

#delete all log files to start with fresh bin and mysql logs
rm -R $mysql_log_path/*
mkdir $mysql_log_path/bin

echo "setting file permissions.."
chmod 700 -R $mysql_data_path/ && chown mysql:mysql -R $mysql_data_path/
chmod 700 -R $mysql_log_path/ && chown mysql:mysql -R $mysql_log_path/

rm -R $active_backup/*
rmdir $active_backup
#rm $mysql_data_path/xtrabackup*

service mysql start

echo "Restore Completed OK!"
 

-- Vladimir Getmanshcuk aka vladget

No comments:

Post a Comment