Created
June 10, 2013 07:55
-
-
Save feelinc/5747165 to your computer and use it in GitHub Desktop.
Backup MySQL DB
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
// location of your temp directory | |
$tmpDir = "/var/www/_back/"; | |
$dbs = array( | |
array( | |
'user' => 'the_db_user', | |
'password' => 'the_db_password', | |
'name' => 'the_db_name' | |
) | |
); | |
// the zip file emailed to you will have this prefixed | |
$prefix = "db_"; | |
// Create the database backup file | |
foreach($dbs as $db) { | |
// sql file to be created upon backup | |
$sqlFile = $tmpDir.$prefix.$db['name'].'_'.date('Y_m_d').".sql"; | |
$backupFilename = $prefix.$db['name'].'_'.date('Y_m_d').".tgz"; | |
// zip sql file to be created upon backup | |
$backupFile = $tmpDir.$backupFilename; | |
$createBackup = "mysqldump -u ".$db['user']." --password=".$db['password']." ".$db['name']." > ".$sqlFile; | |
$createZip = "tar cvzf $backupFile $sqlFile"; | |
exec($createBackup); | |
exec($createZip); | |
// Delete the temporary files | |
// if you wish, just uncomment below codes | |
// unlink($sqlFile); | |
// unlink($backupFile); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment