<?php
/*
* It keeps only 31 backups of past 31 days, and backups of each 1st day of past months.
*/
define('DB_HOST', 'localhost');
define('DB_NAME', 'your_database_name');
define('DB_USER', 'your_database_username');
define('DB_PASSWORD', 'your_username_password');
define('BACKUP_SAVE_TO', 'backup_storage_path'); // without trailing slash
$time = time();
$day = date('j', $time);
if ($day == 1) {
$date = date('Y-m-d', $time);
} else {
$date = $day;
}
$backupFile = BACKUP_SAVE_TO . '/' . DB_NAME . '_' . $date . '.gz';
if (file_exists($backupFile)) {
unlink($backupFile);
}
$command = 'mysqldump --opt -h ' . DB_HOST . ' -u ' . DB_USER . ' -p\'' . DB_PASSWORD . '\' ' . DB_NAME . ' | gzip > ' . $backupFile;
system($command);
?>