Created
April 5, 2020 07:32
-
-
Save skipperTux/06abbcae7d9e20b54d8c5cd844dfc94d to your computer and use it in GitHub Desktop.
PHP script for MySQL backup-dump with backup rotation (all-inkl.com)
This file contains 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 | |
### BEGIN CONFIGURATION ### | |
$backup_root = "/www/htdocs/{kas_user}/backup"; | |
$database = [ | |
"{database1}" => "{password1}", | |
"{database2}" => "{password2}" | |
]; | |
$rotateDays = 14; | |
### END CONFIGURATION ##### | |
$dump_suffix = "-dump.sql"; | |
$dump_suffix_gz = $dump_suffix . ".gz"; | |
is_dir($backup_root) || mkdir($backup_root, 0700); | |
foreach ($database as $db_username => $db_password) { | |
$now = time(); | |
$iso_date = gmdate(DATE_ISO8601, $now); | |
$dump_path = $backup_root . "/" . $iso_date . "_" . $db_username . $dump_suffix; | |
exec("mysqldump -u {$db_username} -p'{$db_password}' --allow-keywords --add-drop-table --complete-insert --quote-names {$db_username} > {$dump_path}"); | |
exec("gzip $dump_path"); | |
$fileSystemIterator = new FilesystemIterator($backup_root); | |
foreach ($fileSystemIterator as $file) { | |
$pathname = $file->getPathname(); | |
if( ($now - $file->getCTime() >= 60 * 60 * 24 * $rotateDays) && | |
(fnmatch("*_" . $db_username . $dump_suffix_gz, $pathname)) ) { | |
unlink($pathname); | |
} | |
} | |
} | |
echo "Success backup timestamp " . gmdate(DATE_ISO8601); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment