Last active
August 29, 2015 13:58
-
-
Save neuni/10017128 to your computer and use it in GitHub Desktop.
MySQL full / incremental backup script using innobackupex
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Backup script to create full and incremental backups using innobackupex | |
* | |
* Creates a full backup once a day and periodically incremental backups using innobackupex by Percona | |
* | |
* Install: create backup folder, change mysql username/password and add a crontab calling "php -f /PATH_TO_SCRIPT/backup.php" every hour | |
* | |
* --- | |
* Warning: | |
* Incremental backups only affect XtraDB or InnoDB-based tables. | |
* Other tables with a different storage engine, e.g. MyISAM, will be copied entirely each time an incremental backup is performed. | |
* --- | |
* | |
* innobackupex: http://www.percona.com/doc/percona-xtrabackup/2.1/innobackupex/innobackupex_script.html | |
* | |
* @author Tobias Kempkensteffen <[email protected]> | |
*/ | |
//MySQL user for backups | |
$username = "backup"; | |
$password = "123456"; | |
//folder to store backups | |
$backupFolder = "/var/mysql_backups/backups"; | |
//number of backups (days) to keep (including today's backup) | |
$numberOfBackups = 7; | |
//period in hours to create incremental backups | |
$diffBackupPeriod = 3; | |
//backup script path | |
$backupScript = "/usr/bin/innobackupex"; | |
////////////////////////////////////////////////////////////////////////////////// | |
//remove trailing slash | |
if (substr($backupFolder, -1) == "/") { | |
$backupFolder = substr($backupFolder, 0, -1); | |
} | |
$backupFolderToday = $backupFolder."/".date("Y-m-d"); | |
//check if todayi's backup folder exists | |
if (!file_exists($backupFolderToday)) { | |
mkdir($backupFolderToday); | |
} | |
if (file_exists($backupFolderToday."/full")) { | |
//create diff backup | |
if ((date("G")%$diffBackupPeriod) == 0 AND date("G") > 0) { | |
$currentBackupFolder = $backupFolderToday."/".date("H"); | |
if (file_exists($currentBackupFolder)) die("The current backup already exists\n"); //stop if backup already exists | |
//get folder of last incremental backup (or of the full backup if no incremental backup exists) | |
$lastBackup = "full"; | |
foreach (scandir($backupFolderToday) as $file) { | |
if (substr($file, 0, 1) == ".") continue; | |
if (!is_dir($file)) continue; | |
if (((int) $file) != $file) continue; | |
if ($file == "full") continue; | |
$lastBackup = $file; | |
} | |
$lastBackupFolder = $backupFolderToday."/".$lastBackup; | |
echo "Create incremental backup (".$currentBackupFolder.") based on ".$lastBackupFolder."\n"; | |
system($backupScript." --user=".$username." --password=".$password." --incremental ".$currentBackupFolder."/ --incremental-basedir=".$lastBackupFolder." --no-timestamp"); | |
} | |
} else { | |
//create full backup | |
$currentBackupFolder = $backupFolderToday."/full"; | |
echo "Create full backup (".$currentBackupFolder.")\n"; | |
system($backupScript." --user=".$username." --password=".$password." ".$currentBackupFolder."/ --no-timestamp"); | |
} | |
//remove old backups | |
$todayTS = mktime(0,0,0); | |
$lastValidTS = $todayTS-($numberOfBackups*(60*60*24)); | |
foreach (scandir($backupFolder) as $folder) { | |
//skip hidden files and folders | |
if (substr($folder, 0, 1) == ".") continue; | |
//get timestamp for backup from folder name | |
$folderTS = strtotime($folder); | |
//skip files and folders with invalid timestamps | |
if ($folderTS == 0) continue; | |
//skip up-to-date backups | |
if ($folderTS > $lastValidTS) continue; | |
//delete old backup | |
echo 'Delete old backup '.$folder."\n"; | |
system("rm -R ".$backupFolder."/".$folder); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment