<?php/*
//文件打包
$date = date("m-d-Y");
// repeat this command for multiple backups, changing the path - e.g. you can have a backup for email, another for files, etc.
shell_exec("tar cvfz /path/to/backup/location/here/file_backup_$date.tar.gz /path/to/files/to/backup/here/");
*/
?>

<?php
        // connect to DB

        $dbhost = 'localhost';

        // edit these 4 lines
        $dbname = 'db_name_here';
        $dbuser = 'db_user_here';
        $dbpw = 'db_pass_here';
        $backupDir = '/full/path/to/backup/dir/here/'; // full path, must be 777 and passwd-protected

        $dbh = mysql_connect($dbhost, $dbuser, $dbpw) or
                die ('I cannot connect to the database because: ' . mysql_error());

        mysql_select_db($dbname, $dbh);

        set_time_limit(0);

        $backup_file = 'db_' . date('Ymd') . '.sql';

        $fp = fopen($backupDir . $backup_file, 'w');

        $schema = '# Database Backup' .
                  '#' . "\n" .
                  '# Backup Date: ' . date('Y-m-d H:i:s') . "\n\n";
        fputs($fp, $schema);

        $tables_query = mysql_query('show tables');
        while ($tables = mysql_fetch_array($tables_query)) {

          list(,$table) = each($tables);

          $schema = 'drop table if exists `' . $table . '`;' . "\n" .
                    'create table `' . $table . '` (' . "\n";

          $table_list = array();
          $fields_query = mysql_query("show fields from `" . $table."`");
          while ($fields = mysql_fetch_array($fields_query)) {

            $table_list[] = "`".$fields['Field']."`"; // bug fix for fields with reserved names thanks to J. Frugier joris.frugier@gmail.com for pointing this out
            //$table_list[] = $fields['Field'];

            $schema .= '  `' . $fields['Field'] . '` ' . $fields['Type'];

            if (strlen($fields['Default']) > 0) $schema .= ' default \'' . $fields['Default'] . '\'';

            if ($fields['Null'] != 'YES') $schema .= ' not null';

            if (isset($fields['Extra'])) $schema .= ' ' . $fields['Extra'];

            $schema .= ',' . "\n";
          }

          $schema = ereg_replace(",\n$", '', $schema);

// add the keys
          $index = array();
          $keys_query = mysql_query("show keys from `" . $table."`");
          while ($keys = mysql_fetch_array($keys_query)) {

            $kname = $keys['Key_name'];

            if (!isset($index[$kname])) {
              $index[$kname] = array('unique' => !$keys['Non_unique'],
                                     'fulltext' => ($keys['Index_type'] == 'FULLTEXT' ? '1' : '0'),
                                     'columns' => array());
            }

            $index[$kname]['columns'][] = "`".$keys['Column_name']."`";
          }

          while (list($kname, $info) = each($index)) {

            $schema .= ',' . "\n";

            $columns = implode($info['columns'], ', ');

            if ($kname == 'PRIMARY') {
              $schema .= '  PRIMARY KEY (' . $columns . ')';
            } elseif ( $info['fulltext'] == '1' ) {
              $schema .= '  FULLTEXT `' . $kname . '` (' . $columns . ')';
            } elseif ($info['unique']) {
              $schema .= '  UNIQUE `' . $kname . '` (' . $columns . ')';
            } else {
              $schema .= '  KEY ' . "`".$kname."`" . ' (' . $columns . ')';
            }
          }

          $schema .= "\n" . ');' . "\n\n";
          fputs($fp, $schema);

// dump the data

            $rows_query = mysql_query("select " . implode(',', $table_list) . " from `" . $table."`");
            while ($rows = mysql_fetch_array($rows_query)) {

              $schema = 'insert into `' . $table . '` (' . implode(', ', $table_list) . ') values (';

              reset($table_list);
              while (list(,$i) = each($table_list)) {
                $i = str_replace('`', '',$i);

                if (!isset($rows[$i])) {
                  $schema .= 'NULL, ';
                } elseif ( trim($rows[$i]) != '' ) {
                  $row = addslashes($rows[$i]);
                  $row = ereg_replace("\n#", "\n".'\#', $row);

                  $schema .= '\'' . $row . '\', ';
                } else {
                  $schema .= '\'\', ';
                }
              }

              $schema = ereg_replace(', $', '', $schema) . ');' . "\n";
              fputs($fp, $schema);
            }        
        }
        fclose($fp);
?>
<?php
$sites = array();
$date = date('Ymd');
$dateDash = date('m-d-Y');

// define an entry an the $site array for each site you want to backup定义要备份的每个站点的入口出了$网站阵列
$sites[0] = array('host'=>'ftp.mysite.com', 'user'=>'ftp_username', 'password'=>'ftp_password', 
                    'dbBackupLocation'=>'server_folder_to_backup_db_to_here', 'dbBackupFilename'=>"db_$date".'.sql', 
                      'emailBackupLocation'=>'server_folder_to_backup_email_here', 'emailBackupFilename'=>'backup_mail_'.$dateDash.'.tar.gz',
                        'filesBackupLocation1'=>'server_folder_to_backup_files_here', 'filesBackupFilename1'=>'backup_files_'.$dateDash.'.tar.gz', 
                          'filesBackupLocation2'=>'server_folder_to_backup_2ndSet_files_here', 'filesBackupFilename2'=>'backup_files2_'.$dateDash.'.tar.gz');
  

         
function getBackup($conn, $location, $file, $mode, $local_file, $user) {
  ftp_chdir($conn, $location);
  $local_file = $user.'_'.$file;
  if (ftp_get($conn, $local_file, $file, $mode)) { // FTP_ASCII for ASCII text, FTP_BINARY FOR IMAGES AND VIDEO, ETC.
    echo "Successfully written to $local_file\n";
    
    // delete file on server
    ftp_delete($conn, $file);
    
  } 
  else {
    echo "There was a problem downloading the file\n";
  }
}        


                          

foreach ($sites as $site) {

   // connect to site using FTP
   $conn = ftp_connect($site['host']);
   if (!$conn) {
     echo "Error: Could not connect to FTP server<br>";
     exit;
   }
   echo "Connected to ".$site['host']."<br>";

  // log into host 
  $result = ftp_login($conn, $site['user'], $site['password']);
  if (!$result) {
    echo "Error: Could not log on as ".$site['user']."<br>";
    ftp_quit($conn);
    exit;
  }
  echo "Logged in as ".$site['user']."<br>";


  // download and save DB backup, email backup and file backup(s)
  if ( trim($site['dbBackupFilename']) != '' ) {  
     $local_file = $site['user'].'_'.$site['dbBackupFilename'];
     getBackup($conn, $site['dbBackupLocation'], $site['dbBackupFilename'], FTP_ASCII, $local_file, $site['user']);
  }
  
  if ( trim($site['emailBackupFilename']) != '' ) { 
     $local_file = $site['user'].'_'.$site['emailBackupFilename'];
     getBackup($conn, $site['emailBackupLocation'], $site['emailBackupFilename'], FTP_BINARY, $local_file, $site['user']);
  }  
  
  if ( trim($site['filesBackupFilename1']) != '' ) {   
     $local_file = $site['user'].'_'.$site['filesBackupFilename1'];
     getBackup($conn, $site['filesBackupLocation1'], $site['filesBackupFilename1'], FTP_BINARY, $local_file, $site['user']);
  }
  
  if ( trim($site['filesBackupFilename2']) != '' ) { 
     $local_file = $site['user'].'_'.$site['filesBackupFilename2'];
     getBackup($conn, $site['filesBackupLocation2'], $site['filesBackupFilename2'], FTP_BINARY, $local_file, $site['user']);
  }
  
  // close FTP connection
  ftp_close($conn);

} // end foreach

?>