Esempio n. 1
0
 function mysqldump($backup)
 {
     //$this->backup_dir = $this->backup_dir;
     $MS = new MasterSlave();
     $dumpoptions = " --quick --add-drop-table --default-character-set=utf8 --extended-insert ";
     $db_to_backup = $this->di['db']->sql($backup['id_connection']);
     $MS->setInstance($db_to_backup);
     $server_config = $db_to_backup->getParams();
     debug($backup['id_connection']);
     debug($server_config);
     $slave = $MS->isSlave();
     $master = $MS->isMaster();
     $userpassword = "******" . $server_config['hostname'] . " -P " . $backup['port'] . " -u " . $server_config['user'] . " -p" . $server_config['password'];
     if ($slave) {
         $stop_slave = "STOP SLAVE;";
         //because option --dump-slave restart replication after made the dump
         if ($db_to_backup->isMultiMaster()) {
             $stop_slave = "STOP ALL SLAVES;";
         }
         $cmd = "mysql " . $userpassword . " -e '" . $stop_slave . ";'";
         $this->cmd($cmd);
         debug($slave);
         $slave = $MS->isSlave();
         $this->slave_data = json_encode($slave);
     }
     if ($master) {
         debug($master);
         $this->master_data = json_encode($master);
     }
     //$backup['path']
     $file_name = $backup['db_name'] . "_" . date("Y-m-d_His") . "__" . $backup['db_name'] . ".sql";
     $this->checkDirectory($this->backup_dir);
     $extra = " ";
     if ($master) {
         $extra .= " --master-data=2 --single-transaction";
     }
     if ($slave) {
         if (version_compare($version, "5.5.3", ">")) {
             $extra .= " --dump-slave=2";
         }
     }
     $this->di['db']->sql(DB_DEFAULT)->sql_close();
     //$this->di['db']->sql(DB_DEFAULT);
     //echo $mysql_dump . "\n";
     Crypt::$key = CRYPT_KEY;
     $mysql_ssh_login = Crypt::decrypt($backup['mysql_ssh_login']);
     $mysql_ssh_password = Crypt::decrypt($backup['mysql_ssh_password']);
     $nas_ssh_login = Crypt::decrypt($backup['nas_ssh_login']);
     $nas_ssh_password = Crypt::decrypt($backup['nas_ssh_password']);
     $pmauser = '******';
     $pmapasswd = Crypt::decrypt(PMACONTROL_PASSWD);
     echo "{$backup['ip']}, 22, {$pmauser}, {$pmapasswd});\n";
     $ccc = new Ssh($backup['ip'], 22, $pmauser, $pmapasswd);
     $ccc->connect();
     $pwd = $ccc->exec('pwd');
     debug($pwd);
     $screen = $ccc->whereis("screen");
     //$screen = "/usr/bin/screen";
     $mysqldump = $ccc->whereis("mysqldump");
     $cmd = $mysqldump . $userpassword . $dumpoptions . $extra . " " . $backup['db_name'] . " > " . $this->backup_dir . "/" . $file_name;
     $id_backup = $backup['id_backup_database'];
     echo "MYSQL_DUMP CMD : " . $cmd . "\n";
     $ccc->exec("echo \"#!/bin/sh\n{$cmd}\" > " . $this->backup_dir . "/mysqldump.{$id_backup}.sh");
     $ccc->exec("echo \"#!/bin/sh\n{$screen} -S backup_database_{$id_backup} -d -m " . $this->backup_dir . "/mysqldump.{$id_backup}.sh\n" . "{$screen} -list | grep backup_database_{$id_backup} | head -n1 | cut -f1 -d'.' | sed 's/\\s//g' > " . $this->backup_dir . "/pid.{$id_backup}.pid\n" . "\" > " . $this->backup_dir . "/backup.{$id_backup}.sh");
     $ccc->exec("chmod +x " . $this->backup_dir . "/mysqldump." . $backup['id_backup_database'] . ".sh");
     $ccc->exec("chmod +x " . $this->backup_dir . "/backup." . $backup['id_backup_database'] . ".sh");
     $exec = $ccc->exec("sh " . $this->backup_dir . "/backup." . $backup['id_backup_database'] . ".sh");
     $pid = trim($ccc->exec("cat " . $this->backup_dir . "/pid." . $backup['id_backup_database'] . ".pid"));
     $waiting = ['/', '-', '\\', '|'];
     $i = 0;
     echo "backup in progress ... ";
     do {
         $i++;
         $mod = $i % 4;
         echo " " . $waiting[$mod];
         echo "";
         sleep(1);
         $nb_thread = $ccc->exec("ps -p {$pid} | grep {$pid} | wc -l");
         switch (trim($nb_thread)) {
             case "1":
                 $continue = true;
                 break;
             case "0":
                 $continue = false;
                 break;
             default:
                 throw new Exception("PMACTRL-085 : more than one thread ({$nb_thread}) have to audit code !");
                 break;
         }
     } while ($continue);
     if (!strpos("dump-slave", $extra) && $slave) {
         $start_slave = "START SLAVE;";
         //because option --dump-slave restart replication after made the dump
         if ($db_to_backup->isMultiMaster()) {
             $start_slave = "START ALL SLAVES;";
         }
         $cmd = "mysql " . $userpassword . " -e '" . $start_slave . ";'";
         $this->cmd($cmd);
     }
     $this->time_backup_end = microtime(true);
     $full_path = $this->backup_dir . "/" . $file_name;
     $file_gz = $this->backup_dir . "/" . $file_name . ".gz";
     //get md5 of file
     $this->md5_file = trim($ccc->exec("md5sum " . $this->backup_dir . "/" . $file_name . " | awk '{ print \$1 }'"));
     //get size of file
     $this->size_file = trim($ccc->exec("du -s " . $this->backup_dir . "/" . $file_name . " | awk '{ print \$1 }'"));
     $cmd = "nice gzip -c " . $this->backup_dir . "/" . $file_name . ">" . $file_gz;
     $ret = $ccc->exec($cmd);
     //get md5 of file
     $this->md5_gz = trim($ccc->exec("md5sum " . $this->backup_dir . "/" . $file_name . ".gz | awk '{ print \$1 }'"));
     $this->size_gz = trim($ccc->exec("du -s " . $this->backup_dir . "/" . $file_name . ".gz | awk '{ print \$1 }'"));
     $this->time_gzip = microtime(true);
     //remove old backup
     $grep = $ccc->whereis("grep");
     $ls = $ccc->whereis("ls");
     $sed = $ccc->whereis("sed");
     $xargs = $ccc->whereis("xargs");
     $rm = $ccc->whereis("rm");
     $cmd = 'cd ' . $this->backup_dir . ' && ' . $ls . ' -t | ' . $grep . ' \'__' . $backup['db_name'] . '.sql\' | ' . $sed . ' -e \'1,2d\' | ' . $xargs . ' -d \'\\n\' ' . $rm . '' . "\n";
     $ret = $ccc->exec($cmd);
     $ret = $ccc->exec("ls " . $file_gz . " | wc -l");
     if (trim($ret) === "1") {
         return $file_gz;
     }
     throw new \Exception("PMACTRL-052 : file not found '" . $file_gz . "'");
     echo "\n";
     return false;
 }