· 6 years ago · Sep 11, 2019, 02:34 AM
1<?php
2
3$BaseDir = "/var/www/html";
4
5$Targets = array();
6
7$Dst = array();
8
9
10function get_mage2_env($dir){
11 if ( ! is_dir($dir) ) {
12 tsay("Warn: dir not found! " . $dir);
13 return false;
14 }
15
16 if ( ! is_dir($dir."/app/etc/") ) {
17 tsay("Warn: dir 'app/etc' not found in path '" . $dir. "'!");
18 return false;
19 }
20
21 if ( ! file_exists($dir."/app/etc/env.php") ){
22 tsay("Warn: config file `app/etc/env.php` not found in path " . $dir. "!");
23 return false;
24 }
25
26 try {
27 $res = include($dir."/app/etc/env.php");
28 if ($res) {
29 tsay("INFO: Magento2 found in ". $dir);
30 return $res["db"]["connection"];
31 }
32// return include($dir."/app/etc/env.php");
33 } catch (\Throwable $e) {
34 tsay("Error: while parsing config file app/etc/env.php in " . $dir. "!");
35 var_dump($e);
36 return false;
37 }
38}
39
40function scan_all($targets){
41 global $BaseDir;
42
43 $ditems = scandir($BaseDir);
44
45 foreach($ditems as $d_k => $d_v) {
46
47 if ( array_key_exists($d_v, $targets) ){
48 tsay("Warn: item '". $d_v ."' exists, skip.");
49 continue;
50 }
51
52 if ( is_file($BaseDir."/".$d_v) ) {
53 continue;
54 }
55
56 $m2_cfg = get_mage2_env($BaseDir."/".$d_v);
57
58 if ( $m2_cfg ){
59 $targets[$d_v]["conf"]=$m2_cfg;
60 }
61
62 }
63
64 return $targets;
65}
66
67
68function fatal($data){
69 fwrite(STDERR, "FATAL: " . $data . "\n");
70 exit(1);
71}
72
73function tsay($data, $name=null){
74 if ($name != null){
75 $name=$name . " = ";
76 } else {
77 $name="";
78 }
79 fwrite(STDERR, $name . $data . "\n");
80}
81
82function ask_user_yn($q, $def=false){
83 $choises = array( "n" => false, "y" => true );
84 return ask_user($q, $choises, $def);
85}
86
87function ask_user($q, $choises, $def="@null"){
88
89 $choise_idx = array_keys($choises);
90
91 if ( $def == "@null"){
92 $_def = $choises[$choise_idx[0]];
93 } else {
94 $_def = $def;
95 }
96
97 global $BATCH;
98
99 if ($BATCH) {
100 return $_def; //$choises[$choise_idx[0]];
101 }
102
103 fwrite(STDERR, $q . "? Valid answer in [ " . join("/", $choise_idx ) . " ] : " );
104 $handle = fopen ("php://stdin","r");
105 $line = fgets($handle);
106
107 fwrite(STDERR, "\n");
108
109 if( trim($line) == "" ){
110 return $_def; //$choises[$choise_idx[0]];
111 }
112
113 if( ! array_key_exists( trim($line), $choises) ){
114 fwrite(STDERR, "Wrong answer!\n");
115 return ask_user($q, $choises, $def);
116 } else {
117 return $choises[trim($line)];
118 }
119
120}
121
122
123function m2_check_cron($name, $dbConfigs, $result){
124
125 foreach($dbConfigs as $i => $item) {
126 if ($i != "default") {
127 continue;
128 }
129
130 if ( $dbConfigs[$i]['active'] != 1 ) {
131 continue;
132 }
133
134 tsay("INFO: Check Magento2 cron_schedule table for " . $name);
135
136 $conn = new mysqli($dbConfigs[$i]['host'], $dbConfigs[$i]['username'], $dbConfigs[$i]['password'], $dbConfigs[$i]['dbname']);
137
138 if ($conn->connect_error) {
139 tsay("Error: Connection failed: " . $conn->connect_error);
140 continue;
141 }
142
143 $cron_count_full_sql = "SELECT count(schedule_id) from cron_schedule;";
144 $cron_count_week_sql = "SELECT count(schedule_id) FROM cron_schedule WHERE scheduled_at < NOW() - INTERVAL 1 WEEK;";
145 $cron_count_last_week_sql = "SELECT count(schedule_id) FROM cron_schedule WHERE scheduled_at > NOW() - INTERVAL 1 WEEK;";
146
147 $cron_count_last_week_res = $conn->query($cron_count_last_week_sql);
148 $cron_count_last_week_row = 0;
149 if ($cron_count_last_week_res) {
150 $cron_count_last_week_row = $cron_count_last_week_res->fetch_row()[0];
151 }
152
153
154 if ( $cron_count_last_week_row > 1000 ) {
155 $cron_count_last_week_row = $cron_count_last_week_res->fetch_row()[0];
156
157 $cron_count_week_res = $conn->query($cron_count_week_sql);
158 $cron_count_week_row = (int) $cron_count_week_res->fetch_row()[0];
159
160 if ( $cron_count_week_res && $cron_count_week_row > 1000 ) {
161 tsay("INFO: task added for: ". $name . ", found old rows: ". $cron_count_week_row);
162 $result[$name] = $dbConfigs[$i];
163 } else {
164 tsay("INFO: SKIP: ". $name . ", found old rows: ". $cron_count_week_row);
165 }
166
167 } else {
168 tsay("INFO: SKIP: ". $name . ", found new rows: ". $cron_count_last_week_row);
169 }
170
171 $conn->close();
172 }
173
174 return $result;
175
176}
177
178function m2_cron_tasks_run($tasks){
179 $execute = array();
180
181 foreach($tasks as $name => $dbConfig) {
182
183 if ( ask_user_yn("Add CLEAN task `cron_schedule` in DB: '". $dbConfig['dbname'] ."' M2:'". $name ."'") ){
184 $execute[$name] = $dbConfig;
185 } else {
186 tsay("INFO: Skip CLEAN task `cron_schedule` in DB: '". $dbConfig['dbname'] ."' M2:'". $name ."'");
187 continue;
188 }
189 }
190
191 foreach($execute as $name => $dbConfig) {
192 tsay("INFO: EXECUTE CLEAN task `cron_schedule` in DB: '". $dbConfig['dbname'] ."' M2:'". $name ."'");
193 m2_cron_clean_old($name, $dbConfig);
194 }
195
196}
197
198function shell_run($command_body, &$out=null){
199 $rc = 127;
200 $shell_body = <<<EOB
201 set -e
202 php_shell_run(){
203 {$command_body}
204 }
205
206 php_shell_run "\${@}" || exit "\${?}"
207
208EOB;
209 exec($shell_body, $out, $rc);
210
211 if ( $rc != 0 ) {
212 return false;
213 }
214
215 return true;
216
217}
218
219function m2_cron_clean_old($name, $dbConfig){
220 global $BaseDir;
221
222 $backup_dir = $BaseDir."/".$name."/var/backups";
223
224 tsay("\n############################################################");
225
226 if ( ! is_dir($backup_dir) ) {
227 if ( ! mkdir($backup_dir,0660 ,true) ){
228 tsay("Error: can not create dir: '". $backup_dir . "', skip!");
229 return false;
230 }
231 }
232
233 $backup_name = "cron_schedule." . date("Ymd-His"). ".sql.gz";
234
235 $backup = $backup_dir . "/" . $backup_name;
236
237
238 tsay("INFO: RUN mysqldump magento2:'" . $name ."' mysql DB:'" . $dbConfig['dbname'] ."' backup:'" . $backup ."'");
239
240 $output = array();
241 $shell_body = <<<EOB
242 { mysqldump --user={$dbConfig['username']} --password='{$dbConfig['password']}' --host={$dbConfig['host']} {$dbConfig['dbname']} cron_schedule || return "\${?}"; } | gzip -9 >> {$backup}
243EOB;
244 if ( shell_run($shell_body, $output) !== true ){
245 tsay("Error: mysqldump failed: '{$name}', dbname: {$dbConfig['dbname']}, host: {$dbConfig['host']}, username: {$dbConfig['username']}!");
246 var_dump($output);
247 tsay("\n############################################################\n");
248 return false;
249 }
250
251 tsay("INFO: DONE mysqldump magento2:'" . $name ."' mysql DB:'" . $dbConfig['dbname'] ."' backup:'" . $backup ."'\n");
252
253 tsay("INFO: Delete rows older then 1 week:'" . $name ."' mysql DB:'" . $dbConfig['dbname'] ."' backup:'" . $backup ."'");
254 $conn = new mysqli($dbConfig['host'], $dbConfig['username'], $dbConfig['password'], $dbConfig['dbname']);
255
256 if ($conn->connect_error) {
257 tsay("Error: Connection failed: " . $conn->connect_error);
258 tsay("\n############################################################\n");
259 return false;
260 }
261
262 $sql = "DELETE FROM `cron_schedule` where scheduled_at < NOW() - INTERVAL 1 WEEK;";
263
264 $res = $conn->query($sql);
265
266 var_dump($res);
267 var_dump($output);
268
269 tsay("\n############################################################\n");
270
271 return true;
272}
273
274
275if ($argc > 1) {
276 if ($argv[1] == 'all') {
277 $Targets = scan_all($Targets);
278 } else {
279 foreach($argv as $a_k => $a_v) {
280 if ( $a_k == 0 ) {
281 continue;
282 }
283 if ( array_key_exists($a_v, $Targets) ){
284 tsay("Warn: item '". $d ."' exists, skip.");
285 continue;
286 }
287
288 $m2_cfg = get_mage2_env($BaseDir."/".$a_v);
289
290 $Targets[$a_v]["conf"] = $m2_cfg;
291 }
292 }
293} else {
294 $Targets = scan_all($Targets);
295}
296
297$Fields = array('host' => 'host', 'dbname' => 'database', 'username' => 'user', 'port' => 'port', 'password' => 'password');
298$SecFields = array('password');
299$results = array();
300
301$tasks = array();
302
303
304foreach($Targets as $t => $titem) {
305
306 $tasks = m2_check_cron($t, $titem["conf"], $tasks);
307
308}
309
310m2_cron_tasks_run($tasks);
311
312var_dump($tasks);
313
314?>