<?php
//*** BEGIN variables ***//
// Mysql Database Information:
$database = 'dbname'; // name of the database.
$username = 'user'; // username with access to database.
$password = 'pass'; // password for username.
// Database Backup Filename & Location
$backupto = '/path/to/file'; // absolute path to folder containing database - no trailing slash.
$backupas = $database . '.sql.' . date ( "mdY" ) . '.gz';
// E-mail Information:
$mailname = 'from address'; // who the mail is from.
$mailfrom = ''; // reply address (not needed)
$mailto = 'your address'; // email address to send the database to.
$subject = 'email subject'; // subject of email.
$message = 'Here's the latest backup!'; // message body.
// FTP Information
$ftpserver = 'remote.ftp.server'; // Remote FTP hostname
$ftppath = 'path/to/files'; // RELATIVE PATH from your ftp home directory (with trailing slash)
$ftpuser = 'username'; // FTP Username
$ftppassword = 'password'; // FTP Password
// Options:
$send_email = '1'; // 1 = send backup copy via e-mail. 0 = just backup data, don't e-mail.
$send_ftp = '0'; // 1 = send backup copy via ftp. 0 = just backup data, don't ftp.
$delete_local = '1'; // 1 = delete local copy when done. 0 = leave local copy when done.
//*** END of variables. ***//
//*** Do Not Edit Beyond This Point ***//
// Call Functions
backupdb();
if ($send_email) {makeandsend();}
if ($send_ftp) {ftpcopy();}
if ($delete_local) {removedb();}
// Functions
function backupdb() {
global $username,$password,$database,$backupto,$backupas;
$backupcommand = "mysqldump -u$username -p$password $database | gzip >$backupto/$backupas";
passthru ("$backupcommand", $error);
if($error) { echo ("Problem: $errorn"); exit;}
}
function removedb() {
global $backupto,$backupas;
if(!unlink("$backupto/$backupas")) { echo ("Cannot Remove $backupto/$backupas"); exit;}
}
function makeandsend() {
global $backupto,$backupas,$message,$mailto,$subject,$mailname,$mailfrom;
$mail_boundary = '--=nextpart_' . md5(uniqid(time()));
$mail_head = "From: $mailnamernReply-to: $mailfromrn";
$mail_head .= "MIME-Version: 1.0rn";
$mail_head .= "Content-type: multipart/mixed; boundary="$mail_boundary"";
$mail_head .= "rnrn";
$mail_head .= "This is a multi-part message in MIME format.";
$mail_head .= "rnrn";
$db_file = "$backupto/$backupas";
$fp = fopen($db_file, "r");
$file = fread($fp, filesize($db_file));
$file = chunk_split(base64_encode($file));
$mail_body = "--$mail_boundaryrn";
$mail_body .= "Content-type: text/plain; charset=us-asciirn";
$mail_body .= "Content-transfer-encoding: 8bitrnrn";
$mail_body .= " $messagern";
$mail_body .= "--$mail_boundaryrn";
$filename = basename($db_file);
$mail_body .= "Content-type: application/octet-stream; name="$filename"rn";
$mail_body .= "Content-transfer-encoding:base64rn";
$mail_body .= "Content-Disposition: attachment; filename="$filename"rnrn";
$mail_body .= $file. "rnrn";
$mail_body .= "--$mail_boundary--";
mail($mailto, $subject, $mail_body, $mail_head);
}
function ftpcopy () {
global $ftpserver,$ftppath,$ftpuser,$ftppassword,$backupas;
$fp = 0;
$fp = ftp_connect($ftpserver);
$login = ftp_login ($fp, $ftpuser, $ftppassword);
ftp_chdir($fp, $ftppath);
ftp_put($fp, $backupas, $backupas, FTP_BINARY);
ftp_quit($fp);
}
?>