Skip to content

Instantly share code, notes, and snippets.

@feelinc
Created June 10, 2013 07:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save feelinc/5747165 to your computer and use it in GitHub Desktop.
Save feelinc/5747165 to your computer and use it in GitHub Desktop.
Backup MySQL DB
// 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