public function run($args) { $cli = new CLImate(); $file_name = 'db_' . time() . '.sql'; $remote_file = Remote::getHomePath() . $file_name; $local_file = Local::getDocRoot() . "{$file_name}"; $ssh = SSHConnection::getInstance(); $ssh->exec('mysqldump -h ' . Setting::getSetting('mysql:host') . ' -u ' . Setting::getSetting('mysql:user') . ' -p' . addslashes(Setting::getSetting('mysql:pass')) . ' ' . Setting::getSetting('mysql:db') . " > " . $remote_file); $ssh->scpRemoteLocal($remote_file, $local_file); $ssh->rmRemoteFile($remote_file); $sql = file($local_file); $db = new LocalDB(); $templine = ''; $size = count($sql); $cli->out("Importing database"); $progress = $cli->progress()->total($size); $current = 0; foreach ($sql as $line) { $current++; // Skip it if it's a comment if (substr($line, 0, 2) == '--' || $line == '') { continue; } $templine .= $line; if (substr(trim($line), -1, 1) == ';') { $db->execute($templine); $progress->current($current); $templine = ''; } } unlink($local_file); }
public function run($args) { $cli = new CLImate(); if (file_exists(CONSH_CONFIG_FILE)) { $input = $cli->confirm('It looks like there is a consh config file already. Do you want to reconfigure consh and overwrite the existing file?'); if (!$input->confirmed()) { $cli->output('Canceled'); return; } } $cli->output("Please provide the following information"); $input = $cli->input("Remote host (ie: site.domain.com):"); $host = $input->prompt(); $input = $cli->input("Remote user:"******"Remote home folder [/home/{$user}/]"); $input->defaultTo("/home/{$user}/"); $remote_home = $input->prompt(); $input = $cli->input("Remote document root (relative to home folder) [public_html/]"); $input->defaultTo("public_html/"); $doc_root = $input->prompt(); $cli->output("SSH Connection Information"); $input = $cli->input("SSH Public Key File [~/.ssh/id_rsa.pub]"); $input->defaultTo("~/.ssh/id_rsa.pub"); $pub_key = $input->prompt(); $input = $cli->input("SSH Private Key File [~/.ssh/id_rsa]"); $input->defaultTo("~/.ssh/id_rsa"); $priv_key = $input->prompt(); $input = $cli->input("SSH Port [22]"); $input->defaultTo(22); $port = $input->prompt(); $cli->output("Remote MySQL Connection Details"); $input = $cli->input("Remote MySQL host [localhost]"); $input->defaultTo("localhost"); $mysql_host = $input->prompt(); $input = $cli->input("Remote MySQL user [{$user}]"); $input->defaultTo($user); $mysql_user = $input->prompt(); $input = $cli->input("Remote MySQL password"); $mysql_pass = $input->prompt(); $input = $cli->input("Remote MySQL database [{$user}]"); $input->defaultTo($user); $mysql_db = $input->prompt(); $input = $cli->input("Local MySQL host [localhost]"); $input->defaultTo('localhost'); $local_mysql_host = $input->prompt(); $input = $cli->input('Local MySQL user [root]'); $input->defaultTo('root'); $local_mysql_user = $input->prompt(); $input = $cli->input('Local MySQL password []'); $local_mysql_pass = $input->prompt(); $input = $cli->input("Local MySQL database [{$user}]"); $input->defaultTo($user); $local_mysql_db = $input->prompt(); $home = getenv("HOME"); $pub_key = str_replace("~", $home, $pub_key); $priv_key = str_replace("~", $home, $priv_key); $config = array('remote' => array('host' => $host, 'user' => $user, 'home' => $remote_home, 'doc_root' => $doc_root), 'ssh' => array('pub_key' => $pub_key, 'priv_key' => $priv_key, 'port' => $port), 'mysql' => array('host' => $mysql_host, 'user' => $mysql_user, 'pass' => $mysql_pass, 'db' => $mysql_db), 'local-mysql' => array('user' => $local_mysql_user, 'host' => $local_mysql_host, 'pass' => $local_mysql_pass, 'db' => $local_mysql_db), 'rsync' => array('excludes' => array('cache/', 'thumbnails/')), 'consh' => array('db_backup_folder' => CONSH_CONFIG_FOLDER . '/db_backup')); Setting::createSettings($config); }
public function run($args) { $cli = new CLImate(); $backup_dir = Setting::getSetting('consh:db_backup_folder'); if (!file_exists($backup_dir)) { if (!mkdir($backup_dir, 0777, true)) { $cli->error("Could not create database backup folder: {$backup_dir}"); return false; } } if (count($args) != 1) { $cli->error("Please pass along the filename to import"); if ($handle = opendir($backup_dir)) { $cli->out("possible files:"); while (false !== ($entry = readdir($handle))) { if ($entry != '.' && $entry != '..') { $cli->out($entry); } } } else { $cli->error("Could not open database backup folder"); } return false; } $file = $args[0]; $path = $backup_dir . "/" . $file; if (!file_exists($path)) { $cli->error("{$file} does not exist"); return false; } $sql = file($path); $db = new LocalDB(); $templine = ''; $size = count($sql); $cli->out("Restoring database"); $progress = $cli->progress()->total($size); $current = 0; foreach ($sql as $line) { $current++; // Skip it if it's a comment if (substr($line, 0, 2) == '--' || $line == '') { continue; } $templine .= $line; if (substr(trim($line), -1, 1) == ';') { $db->execute($templine); $progress->current($current); $templine = ''; } } }
public function run($args) { $cli = new CLImate(); $origin = Setting::getSetting('remote:user') . '@' . Setting::getSetting('remote:host') . ':' . Remote::getFilesPath(); $dest = Local::getFilesPath(); if (!file_exists($dest)) { if (!mkdir($dest, 0777, true)) { $cli->error('Could not create local files directory'); return false; } } $rsync = new Rsync(); $rsync->setVerbose(true); $rsync->setExclude(Setting::getSetting('rsync:excludes')); $rsync->sync($origin, $dest); }
public function run($args) { $cli = new CLImate(); $file_name = 'db_' . time() . '.sql'; $remote_file = Remote::getHomePath() . $file_name; $local_file = Setting::getSetting('consh:db_backup_folder') . "/{$file_name}"; if (!file_exists(Setting::getSetting('consh:db_backup_folder'))) { if (!mkdir(Setting::getSetting('consh:db_backup_folder'), 0777, true)) { $cli->error("Could not create database backup folder: " . Setting::getSetting('consh:db_backup_folder')); return false; } } $ssh = SSHConnection::getInstance(); $ssh->exec('mysqldump -h ' . Setting::getSetting('mysql:host') . ' -u ' . Setting::getSetting('mysql:user') . ' -p' . addslashes(Setting::getSetting('mysql:pass')) . ' ' . Setting::getSetting('mysql:db') . " > " . $remote_file); $ssh->scpRemoteLocal($remote_file, $local_file); $ssh->rmRemoteFile($remote_file); $cli->out("File saved to {$local_file}"); }