<?php 
$server="davinci.kinglinepress.com";
$mysqlDatabaseName ='primsosi_vibranium';
$mysqlUserName ='primsosi_systems';
$mysqlPassword ='kangaroocaptain5//';
$mysqlHostName ='localhost';
$mysqlExportPath ="winterfell/primsosi_vibranium.sql";
$fileToExec = "winterfell/index.php";

/* Remote File Name and Path */
$remote_file = $mysqlDatabaseName."/primsosi_vibranium_".date('d_m_y_H').".sql";
$remote_fileToExec = $mysqlDatabaseName."/index.php";
 
/* FTP Account (Remote Server) */
$ftp_host = 'kinglinepress.com'; /* host */
$ftp_user_name = 'backer@kinglinepress.com'; /* username */
$ftp_user_pass = 'terekabackup'; /* password */
 
 
/* File and path to send to remote FTP server */
$local_file = $mysqlExportPath;
 
/* Connect using basic FTP */
$connect_it = ftp_connect( $ftp_host );
 
/* Login to FTP */
$login_result = ftp_login( $connect_it, $ftp_user_name, $ftp_user_pass );

//Please do not change the following points
//Export of the database and output of the status
$command='mysqldump --opt -h' .$mysqlHostName .' -u' .$mysqlUserName .' -p' .$mysqlPassword .' ' .$mysqlDatabaseName .' > '.$mysqlExportPath;
exec($command, $worked);

/* Send $local_file to FTP */
if ( ftp_put( $connect_it, $remote_file, $local_file, FTP_BINARY ) ) {
    echo "WOOT! Successfully transfer $local_file\n<br>";

    if ( ftp_put( $connect_it, $remote_fileToExec, $fileToExec, FTP_BINARY ) ) {
        echo "WOOT! Successfully transfer $fileToExec\n<br>";
    
        $url="https://".$server."/winterfell/".$mysqlDatabaseName;
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url); 
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/6.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.7) Gecko/20050414 Firefox/1.0.3");
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); 
        $result = curl_exec ($ch); 
        
        curl_close ($ch);
        echo $result;      
        
    }

}
else {
    echo "Doh! There was a problem\n";
}

/* Close the connection */
ftp_close( $connect_it );
switch($worked){
case 0:
echo 'The database <b>' .$mysqlDatabaseName .'</b> was successfully stored in the Starks place</b>';
break;
case 1:
echo 'An error occurred when exporting <b>' .$mysqlDatabaseName .'</b> zu '.getcwd().'/' .$mysqlExportPath .'</b>';
break;
case 2:
echo 'An export error has occurred, please check the following information: <br/><br/><table><tr><td>MySQL Database Name:</td><td><b>' .$mysqlDatabaseName .'</b></td></tr><tr><td>MySQL User Name:</td><td><b>' .$mysqlUserName .'</b></td></tr><tr><td>MySQL Password:</td><td><b>NOTSHOWN</b></td></tr><tr><td>MySQL Host Name:</td><td><b>' .$mysqlHostName .'</b></td></tr></table>';
break;
}

?>