<?php
error_reporting(E_STRICT | E_ALL);

if ($argv[1] == "")
    die ("php archive_server.php \"서버명\" \"DB명\" \"DB아이디\", \"DB암호\"");

$dbHost = $argv[1];
$dbName = $argv[2];
$dbUserId = $argv[3];
$dbPassword = $argv[4];

// print_r(get_defined_vars());
system("clear");
echo "=========================================\n";
say("Starting archive $dbName in $dbHost\n");
echo "=========================================\n";
archive($dbHost, $dbName, $dbUserId, $dbPassword);
// moveFile($STORYSHARE, $srcIp, $srcDbUserId, $srcDbPassword);
say2("finished");

function archive($dbHost, $dbName, $dbUserId, $dbPassword)
{
    $date = date("Ymd_His");
    $fileName = "{$dbName}_${date}.sql";
    $path = "/root/pty";

    // www 와 db 압축
    if (true) {
        echo "=========================================\n";
        say("Archiving www and db in $dbHost\n");
        echo "=========================================\n";
        cmd("ssh $dbHost -l root 'rm /root/{$dbName}_db.tgz;'");

        // www 와 db 의 tgz 압축
        // cmd("ssh $dbHost -l root 'cd /root; tar czf {$dbName}_www.tgz www'");

        $cmd = "mysqldump -h 127.0.0.1 -u $dbUserId -p $dbName --password='$dbPassword' --default-character-set=utf8mb4_unicode_ci --complete-insert --routines --triggers --single-transaction > /root/{$dbName}_db.sql";
        $cmd = str_replace("'", "'\"'\"'", $cmd);
        cmd("ssh $dbHost -l root '$cmd'");
        cmd("ssh $dbHost -l root 'cd /root/; tar czf {$dbName}_db.tgz {$dbName}_db.sql; rm /home/{$dbName}_db.sql;'");
    }
}

function moveFile($accountName, $srcHost, $srcDbPassword, $dst)
{
    cmd("ssh $dst -l root 'adduser $accountName;'");
    cmd("ssh $dst -l root 'mkdir /home/$accountName;'");

    {
        echo "=========================================\n";
        say("Downloading src's www and db to dst (with local)\n");
        echo "=========================================\n";
        @mkdir("tmp");
        cmd("sftp root@{$srcHost}:/home/{$accountName}/{$accountName}_db.tgz tmp/{$accountName}_db.tgz");
        cmd("sftp root@{$srcHost}:/home/{$accountName}/{$accountName}_www.tgz tmp/{$accountName}_www.tgz");
        cmd("ssh $srcHost -l root 'rm /home/$accountName/{$accountName}_www.tgz; rm /home/$accountName/{$accountName}_db.tgz;'");

        /*
        echo "=========================================\n";
        say("Uploading www and db to $dst (with local)\n");
        echo "=========================================\n";
        cmd("sftp root@{$dst}:/home/{$STORYSHARE} <<< $'put tmp/{$STORYSHARE}_db.tgz'");
        cmd("sftp root@{$dst}:/home/{$STORYSHARE} <<< $'put tmp/{$STORYSHARE}_www.tgz'");
        */
    }

    /*
    // 대상에 압축 풀기
    if (true) {
        echo "=========================================\n";
        say("Extracting www and db in $dst\n");
        echo "=========================================\n";
        cmd("ssh $dst -l root 'cd /home/{$STORYSHARE}/; tar xzf /home/{$STORYSHARE}/{$STORYSHARE}_db.tgz'");
        cmd("ssh $dst -l root 'cd /home/{$STORYSHARE}/; tar xzf /home/{$STORYSHARE}/{$STORYSHARE}_www.tgz'");
    }
    */
}

function say($msg)
{
    echo $msg;
    // system("say --voice Samantha $msg");
}

function say2($msg)
{
    echo $msg;
    system("say --voice Samantha $msg");
}


function cmd($cmd)
{
    echo "# {$cmd}\n";
    system($cmd);
}


?>

