If you encountered data loss when your web hosting has troubles or you wanna transfer site, you may think about backup your database. It's always better to have a database copy in your mail box. To do that, create a .php file, e.g backup.php with this code: <?php //~ Must edited values //~ MySQL $SQL_USER='username'; $SQL_PASS='password'; $SQL_DB='database'; //~ * Back up mail $MAIL='your@email.com'; $SUBJECT='backup'; $MESSAGE='backup'; //~ * MySQL dump, then zip. shell_exec("mysqldump -u $SQL_USER -p$SQL_PASS $SQL_DB $SQL_DB.sql"); shell_exec("zip $SQL_DB $SQLDB.sql"); //~ * Send mail with attachment. $OB='----=_OuterBoundary_000'; $IB='----=_InnerBoundery_001'; $headers="From: $MAIL\r\n" ."MIME-Version: 1.0\r\n" ."Content-Type: multipart/mixed;\n\tboundary=\"$OB\"\n"; $Msg="This is a multi-part message in MIME format.\n" ."\n--$OB\n" ."Content-Type: multipart/alternative;\n\tboundary=\"$IB\"\n\n" ."\n--$IB\n" ."Content-Type: text/plain;\n\tcharset=\"iso-8859-1\"\n" ."Content-Transfer-Encoding: quoted-printable\n\n" .$MESSAGE ."\n--$IB--\n" . "\n--$OB\n" ."Content-Type: application/octet-stream;\n\tname=www.zip\n" ."Content-Transfer-Encoding: base64\n" ."Content-Disposition: attachment;\n\tfilename=www.zip\n\n" .chunk_split(base64_encode(file_get_contents($SQL_DB.'.zip'))) ."\n\n" ."\n--$OB--\n"; mail($MAIL, $SUBJECT, $Msg, $headers); //~ * Completed message echo 'done'; ?> PHP: This script will dump out mysql database, zip it, then send it to your mailbox. Remember to edit must-edited values such as MySQL username, password, database, your email address. Recommended mailbox is GMail. Then, set a cron job to call that file every day, or every 2, 3, 5 days, etc. Go to www.setcronjob.com for a free one, if your web hosting doesn't support cron jobs feature. Hope this help
Is this work also for php 5 with new mysql version ? Some older backup script only works with php4 / older mysql version
@jaysmyhero: go to your file manager, or FTP client to access your server, create file backup.php under web root, e.g /home/yourname/www, then edit and save that file with the script above. Then set a cron job to that file URL, e.g http://example.com/backup.php Hope this helps.
Thanks and i think most people dont do this. Sooner or later something will happen and I would never trust a webhotel to recover the data. I backup database weekly on all my sites and I do a filebackup now and then.
You mean 'should'? This script will zip and then send database to your email, e.g gmail, which is always better than store it in your web hosting. My friend has some trouble with his web hosting provider, and can't take back his backup in his server. I back up my database every 2 hours However, it's small. Less than 300 KB. When I tested with shared host, I can't send zip file that is larger than 2 MB, so, zip and email only database can help a bit. Files and folders are rarely changed, while database is bigger and bigger.