Here is a PHP backup script for MySQL and PostgreSQL databases with remote FTP backup capability.
<?php
chdir('..');
require_once "includes/config.php";
$file_name = date("Y-m-d_H-i-s",returnTime()).".sql"; // backup file name
// local backup settings
$backup_dir = $config["dir"]["backups"]; // local backup directory under kiwi folder
$backup_file = $config["dir"]["backups"].$file_name; // path to backup file
// remote ftp backup settings
$ftp_backup = TRUE; // Set to true to transfer backup file to a remote ftp server in addition to the local backup
$ftp_passive = TRUE; // enable disable passive ftp connections.. For ftp users behind a firewall, the server will LISTEN for a connection if set to TRUE...
$ftp_server = "ftp.kiwimanager.com";
$ftp_user = "";
$ftp_pass = "";
$ftp_dir = "/public_html/backups"; // remote directory
$ftp_file = $file_name; // remote file name
if(is_writable($backup_dir)){
if(is_array($config["database"])){
foreach($config["database"] as $k=>$v){
$config["database"][$k] = preg_replace("/[^a-z0-9_\-\.]/i","",$v); // sanitize
}
}
switch($config["database"]["system"]){
case "mysql":
system("mysqldump --single-transaction --add-drop-database --quick --host=".$config["database"]["host"]." --user=".$config["database"]["user"]." --password=".$config["database"]["pass"] ." --databases ".$config["database"]["name"]." > ".$backup_file,$output);
break;
case "pgsql":
system("echo -n -e \"".$config["database"]["pass"]."\n\" | pg_dump -W --host=".$config["database"]["host"]." -U ".$config["database"]["user"]." ".$config["database"]["name"]." > ".$backup_file,$output);
break;
default:
die("Backups for <b>".$config["database"]["system"]."<b> database systems is not supported!");
break;
}
if(!empty($output)){
echo "<pre>".$output."</pre>";
}
if($ftp_backup){
if(is_readable($backup_file)){
$connection = ftp_connect($ftp_server,21,5) or die("Connection failed!"); // connect to ftp server
ftp_login($connection,$ftp_user,$ftp_pass) or die("Login failed!"); // login to ftp serer
ftp_pasv($connection,$ftp_passive); // set passive mode
ftp_chdir($connection,$ftp_dir); // change to remote backup dir
if(ftp_put($connection,$ftp_file,$backup_file,FTP_BINARY)){ // upload file
echo "FTP upload successful!";
}else{
echo "FTP upload failed!";
}
ftp_close($connection); // close ftp connection
}else{
echo("Backup file is not readable!");
}
}
}else{
echo("Local backup directory is not writable!");
}
?>