Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $BaseDir = "/var/www/html";
- $Targets = array();
- $Dst = array();
- function get_mage2_env($dir){
- if ( ! is_dir($dir) ) {
- tsay("Warn: dir not found! " . $dir);
- return false;
- }
- if ( ! is_dir($dir."/app/etc/") ) {
- tsay("Warn: dir 'app/etc' not found in path '" . $dir. "'!");
- return false;
- }
- if ( ! file_exists($dir."/app/etc/env.php") ){
- tsay("Warn: config file `app/etc/env.php` not found in path " . $dir. "!");
- return false;
- }
- try {
- $res = include($dir."/app/etc/env.php");
- if ($res) {
- tsay("INFO: Magento2 found in ". $dir);
- return $res["db"]["connection"];
- }
- // return include($dir."/app/etc/env.php");
- } catch (\Throwable $e) {
- tsay("Error: while parsing config file app/etc/env.php in " . $dir. "!");
- var_dump($e);
- return false;
- }
- }
- function scan_all($targets){
- global $BaseDir;
- $ditems = scandir($BaseDir);
- foreach($ditems as $d_k => $d_v) {
- if ( array_key_exists($d_v, $targets) ){
- tsay("Warn: item '". $d_v ."' exists, skip.");
- continue;
- }
- if ( is_file($BaseDir."/".$d_v) ) {
- continue;
- }
- $m2_cfg = get_mage2_env($BaseDir."/".$d_v);
- if ( $m2_cfg ){
- $targets[$d_v]["conf"]=$m2_cfg;
- }
- }
- return $targets;
- }
- function fatal($data){
- fwrite(STDERR, "FATAL: " . $data . "\n");
- exit(1);
- }
- function tsay($data, $name=null){
- if ($name != null){
- $name=$name . " = ";
- } else {
- $name="";
- }
- fwrite(STDERR, $name . $data . "\n");
- }
- function ask_user_yn($q, $def=false){
- $choises = array( "n" => false, "y" => true );
- return ask_user($q, $choises, $def);
- }
- function ask_user($q, $choises, $def="@null"){
- $choise_idx = array_keys($choises);
- if ( $def == "@null"){
- $_def = $choises[$choise_idx[0]];
- } else {
- $_def = $def;
- }
- global $BATCH;
- if ($BATCH) {
- return $_def; //$choises[$choise_idx[0]];
- }
- fwrite(STDERR, $q . "? Valid answer in [ " . join("/", $choise_idx ) . " ] : " );
- $handle = fopen ("php://stdin","r");
- $line = fgets($handle);
- fwrite(STDERR, "\n");
- if( trim($line) == "" ){
- return $_def; //$choises[$choise_idx[0]];
- }
- if( ! array_key_exists( trim($line), $choises) ){
- fwrite(STDERR, "Wrong answer!\n");
- return ask_user($q, $choises, $def);
- } else {
- return $choises[trim($line)];
- }
- }
- function m2_check_cron($name, $dbConfigs, $result){
- foreach($dbConfigs as $i => $item) {
- if ($i != "default") {
- continue;
- }
- if ( $dbConfigs[$i]['active'] != 1 ) {
- continue;
- }
- tsay("INFO: Check Magento2 cron_schedule table for " . $name);
- $conn = new mysqli($dbConfigs[$i]['host'], $dbConfigs[$i]['username'], $dbConfigs[$i]['password'], $dbConfigs[$i]['dbname']);
- if ($conn->connect_error) {
- tsay("Error: Connection failed: " . $conn->connect_error);
- continue;
- }
- $cron_count_full_sql = "SELECT count(schedule_id) from cron_schedule;";
- $cron_count_week_sql = "SELECT count(schedule_id) FROM cron_schedule WHERE scheduled_at < NOW() - INTERVAL 1 WEEK;";
- $cron_count_last_week_sql = "SELECT count(schedule_id) FROM cron_schedule WHERE scheduled_at > NOW() - INTERVAL 1 WEEK;";
- $cron_count_last_week_res = $conn->query($cron_count_last_week_sql);
- $cron_count_last_week_row = 0;
- if ($cron_count_last_week_res) {
- $cron_count_last_week_row = $cron_count_last_week_res->fetch_row()[0];
- }
- if ( $cron_count_last_week_row > 1000 ) {
- $cron_count_last_week_row = $cron_count_last_week_res->fetch_row()[0];
- $cron_count_week_res = $conn->query($cron_count_week_sql);
- $cron_count_week_row = (int) $cron_count_week_res->fetch_row()[0];
- if ( $cron_count_week_res && $cron_count_week_row > 1000 ) {
- tsay("INFO: task added for: ". $name . ", found old rows: ". $cron_count_week_row);
- $result[$name] = $dbConfigs[$i];
- } else {
- tsay("INFO: SKIP: ". $name . ", found old rows: ". $cron_count_week_row);
- }
- } else {
- tsay("INFO: SKIP: ". $name . ", found new rows: ". $cron_count_last_week_row);
- }
- $conn->close();
- }
- return $result;
- }
- function m2_cron_tasks_run($tasks){
- $execute = array();
- foreach($tasks as $name => $dbConfig) {
- if ( ask_user_yn("Add CLEAN task `cron_schedule` in DB: '". $dbConfig['dbname'] ."' M2:'". $name ."'") ){
- $execute[$name] = $dbConfig;
- } else {
- tsay("INFO: Skip CLEAN task `cron_schedule` in DB: '". $dbConfig['dbname'] ."' M2:'". $name ."'");
- continue;
- }
- }
- foreach($execute as $name => $dbConfig) {
- tsay("INFO: EXECUTE CLEAN task `cron_schedule` in DB: '". $dbConfig['dbname'] ."' M2:'". $name ."'");
- m2_cron_clean_old($name, $dbConfig);
- }
- }
- function shell_run($command_body, &$out=null){
- $rc = 127;
- $shell_body = <<<EOB
- set -e
- php_shell_run(){
- {$command_body}
- }
- php_shell_run "\${@}" || exit "\${?}"
- EOB;
- exec($shell_body, $out, $rc);
- if ( $rc != 0 ) {
- return false;
- }
- return true;
- }
- function m2_cron_clean_old($name, $dbConfig){
- global $BaseDir;
- $backup_dir = $BaseDir."/".$name."/var/backups";
- tsay("\n############################################################");
- if ( ! is_dir($backup_dir) ) {
- if ( ! mkdir($backup_dir,0660 ,true) ){
- tsay("Error: can not create dir: '". $backup_dir . "', skip!");
- return false;
- }
- }
- $backup_name = "cron_schedule." . date("Ymd-His"). ".sql.gz";
- $backup = $backup_dir . "/" . $backup_name;
- tsay("INFO: RUN mysqldump magento2:'" . $name ."' mysql DB:'" . $dbConfig['dbname'] ."' backup:'" . $backup ."'");
- $output = array();
- $shell_body = <<<EOB
- { mysqldump --user={$dbConfig['username']} --password='{$dbConfig['password']}' --host={$dbConfig['host']} {$dbConfig['dbname']} cron_schedule || return "\${?}"; } | gzip -9 >> {$backup}
- EOB;
- if ( shell_run($shell_body, $output) !== true ){
- tsay("Error: mysqldump failed: '{$name}', dbname: {$dbConfig['dbname']}, host: {$dbConfig['host']}, username: {$dbConfig['username']}!");
- var_dump($output);
- tsay("\n############################################################\n");
- return false;
- }
- tsay("INFO: DONE mysqldump magento2:'" . $name ."' mysql DB:'" . $dbConfig['dbname'] ."' backup:'" . $backup ."'\n");
- tsay("INFO: Delete rows older then 1 week:'" . $name ."' mysql DB:'" . $dbConfig['dbname'] ."' backup:'" . $backup ."'");
- $conn = new mysqli($dbConfig['host'], $dbConfig['username'], $dbConfig['password'], $dbConfig['dbname']);
- if ($conn->connect_error) {
- tsay("Error: Connection failed: " . $conn->connect_error);
- tsay("\n############################################################\n");
- return false;
- }
- $sql = "DELETE FROM `cron_schedule` where scheduled_at < NOW() - INTERVAL 1 WEEK;";
- $res = $conn->query($sql);
- var_dump($res);
- var_dump($output);
- tsay("\n############################################################\n");
- return true;
- }
- if ($argc > 1) {
- if ($argv[1] == 'all') {
- $Targets = scan_all($Targets);
- } else {
- foreach($argv as $a_k => $a_v) {
- if ( $a_k == 0 ) {
- continue;
- }
- if ( array_key_exists($a_v, $Targets) ){
- tsay("Warn: item '". $d ."' exists, skip.");
- continue;
- }
- $m2_cfg = get_mage2_env($BaseDir."/".$a_v);
- $Targets[$a_v]["conf"] = $m2_cfg;
- }
- }
- } else {
- $Targets = scan_all($Targets);
- }
- $Fields = array('host' => 'host', 'dbname' => 'database', 'username' => 'user', 'port' => 'port', 'password' => 'password');
- $SecFields = array('password');
- $results = array();
- $tasks = array();
- foreach($Targets as $t => $titem) {
- $tasks = m2_check_cron($t, $titem["conf"], $tasks);
- }
- m2_cron_tasks_run($tasks);
- var_dump($tasks);
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement