Created
September 8, 2011 03:47
-
-
Save bcse/1202559 to your computer and use it in GitHub Desktop.
Backup all MySQL databases and send to my E-mail
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 | |
$DBUSERNAME = 'YOUR_USERNAME'; | |
$DBPASSWORD = 'YOUR_PASSWORD'; | |
$EMAIL = 'YOUR_EMAIL'; | |
$boundary = '==DBBACKUP-'.md5(uniqid(rand(), true)); | |
$date = date('Ymd'); | |
$header = "Content-Type: multipart/mixed; boundary=\"$boundary\""; | |
$message = | |
"This is a multi-part message in MIME format.\n". | |
"--$boundary\n"; | |
mysql_connect('localhost', $DBUSERNAME, $DBPASSWORD); | |
$result = mysql_query("SHOW DATABASES LIKE '$DBUSERNAME%'"); | |
while ($dbname = mysql_fetch_array($result, MYSQL_NUM)) { | |
echo "Backup $dbname[0]... "; | |
$data = chunk_split(base64_encode(shell_exec("mysqldump --opt --complete-insert --default-character-set=utf8 --user=\"$DBUSERNAME\" --password=\"$DBPASSWORD\" $dbname[0] | gzip --best -f"))); | |
$filename = "$dbname[0]-$date.sql.gz"; | |
$message .= | |
"Content-Transfer-Encoding: base64\n". | |
"Content-Type: application/octet-stream; name=\"$filename\"\n". | |
"Content-Disposition: attachment; filename=\"$filename\"\n". | |
"\n$data\n". | |
"--$boundary\n"; | |
echo "OK!\n"; | |
} | |
echo "Mail to $EMAIL... "; | |
mail($EMAIL, "Database Backup ($date)", $message, $header); | |
echo "OK!\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment