Subversion Repositories qbpwcf-lib(archive)

Rev

Rev 918 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
126 liveuser 1
#!/bin/php
2
<?php
3
 
4
/*
370 liveuser 5
	QBPWCF, Quick Build PHP website Component base on Fedora Linux.
622 liveuser 6
    Copyright (C) 2015~2024 Min-Jhin,Chen
370 liveuser 7
 
8
    This file is part of QBPWCF.
9
 
10
    QBPWCF is free software: you can redistribute it and/or modify
11
    it under the terms of the GNU General Public License as published by
12
    the Free Software Foundation, either version 3 of the License, or
13
    (at your option) any later version.
14
 
15
    QBPWCF is distributed in the hope that it will be useful,
16
    but WITHOUT ANY WARRANTY; without even the implied warranty of
17
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
    GNU General Public License for more details.
19
 
20
    You should have received a copy of the GNU General Public License
21
    along with QBPWCF.  If not, see <http://www.gnu.org/licenses/>.
22
 
23
*/
24
 
25
/*
126 liveuser 26
說明:
27
每天特定時間備份Mariadb資料庫資料到指定的位置
28
 
29
範例:
30
backupDb.php --backTime 09:01 --config  database.php --acctVarName "db['default']['username']" --passVarName "db['default']['password']" --dbVarName "db['default']['database']" --dbAddrVarName "db['default']['hostname']" --dbPortVarName "db['default']['port']" --backupAddr "./"
31
*/
32
 
33
#使用命名空間qbpwcf
34
namespace qbpwcf;
35
 
951 liveuser 36
#取得 lib path
37
exec("php -f ".escapeshellarg(pathinfo(__FILE__)["dirname"]."/libexec/folderOfUsrLib.php"),$output,$status);
38
 
39
#如果執行失敗
40
if($status!==0){
41
 
42
	#debug
43
	var_dump(__LINE__,$output);
44
 
45
	#結束執行,回傳shell 1.
46
	exit(1);
47
 
48
	}#if end
49
 
50
#儲存lib path
51
$folderOfUsrLib=$output[0];
52
 
466 liveuser 53
#以該檔案的實際位置的 lib path 為 include path 首位
951 liveuser 54
$output=array();
55
exec("cd ".escapeshellarg(pathinfo(__FILE__)["dirname"]."/../".$folderOfUsrLib."/qbpwcf").";pwd;",$output,$status);
56
 
57
#如果執行失敗
58
if($status!==0){
59
 
60
	#debug
61
	var_dump(__LINE__,$output);
62
 
63
	#結束執行,回傳shell 1.
64
	exit(1);
65
 
66
	}#if end
67
 
68
#設置 include path 
466 liveuser 69
set_include_path($output[0].PATH_SEPARATOR.get_include_path());
70
 
126 liveuser 71
#匯入外部套件
466 liveuser 72
include("allInOne.php");
126 liveuser 73
 
74
#說明函式
565 liveuser 75
function help(){
76
 
126 liveuser 77
	#印出指令說明
370 liveuser 78
	echo "Usage of ".basename(__FILE__).":".PHP_EOL; 
381 liveuser 79
	echo "--backTime HourHour:MinMin 代表當下為為HourHour:MinMin時要進行備份,若不設定代表立即執行.".PHP_EOL;
80
	echo "--config addr 代表設定檔的位置,裡面寫有要備份的資料庫資訊.".PHP_EOL;
81
	echo "--acctVarName name 設定檔中資料庫名稱的變數名稱.".PHP_EOL;
82
	echo "--passVarName name 設定檔中資料庫密碼的變數名稱.".PHP_EOL;
83
	echo "--dbVarName name 設定檔中資料庫名稱的變數名稱".PHP_EOL;
84
	echo "--dbAddrVarName name 設定檔中資料庫位置的變數名稱".PHP_EOL;
85
	echo "--dbPortVarName name 設定檔中資料庫port的變數名稱".PHP_EOL;
86
	echo "--backupAddr addr 備份檔案要存放的位置".PHP_EOL;
87
	echo "--times 1~N 指定要執行幾次,若有 --backTime 參數則預設為無限次;反之若無 --backTime 參數則強制為一次.".PHP_EOL;
486 liveuser 88
	echo "--backupFilesToKeep 1~N 至少要保留幾份資料,超過數目會依照產生的日期,從舊的開始移除,直到剩下符合上限的數目,預設為7.".PHP_EOL;
381 liveuser 89
	echo "時間到周而復始執行的範例:".PHP_EOL;
126 liveuser 90
	echo "\t".basename(__FILE__)." --backTime 09:01 --config  database.php --acctVarName \"db['default']['username']\" --passVarName \"db['default']['password']\" --dbVarName \"db['default']['database']\" --dbAddrVarName \"db['default']['hostname']\" --dbPortVarName \"db['default']['port']\" --backupAddr \"./\"".PHP_EOL;
381 liveuser 91
	echo "時間到只執行一次的範例:".PHP_EOL;
370 liveuser 92
	echo "\t".basename(__FILE__)." --backTime 09:01 --config  database.php --acctVarName \"db['default']['username']\" --passVarName \"db['default']['password']\" --dbVarName \"db['default']['database']\" --dbAddrVarName \"db['default']['hostname']\" --dbPortVarName \"db['default']['port']\" --backupAddr \"./\" --times 1".PHP_EOL;
381 liveuser 93
	echo "立即執行的範例:".PHP_EOL;
370 liveuser 94
	echo "\t".basename(__FILE__)." --config  database.php --acctVarName \"db['default']['username']\" --passVarName \"db['default']['password']\" --dbVarName \"db['default']['database']\" --dbAddrVarName \"db['default']['hostname']\" --dbPortVarName \"db['default']['port']\" --backupAddr \"./\"".PHP_EOL;
95
 
126 liveuser 96
	#結束執行
97
	exit;
98
 
565 liveuser 99
	}#function help end
100
 
126 liveuser 101
#函式說明:
102
#解析參數.
103
#回傳結果:
104
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
105
#$reuslt["error"],執行不正常結束的錯訊息陣列.
106
#$result["function"],當前執行的函式名稱.
107
#$result["content"],解析好的參數陣列.
108
#$result["content"][$key][$i],參數 $key 的 $i+1 個參數數值內容.
109
#$result["program"],字串,執行的程式名稱.
110
#必填參數:
111
#無
112
#可省略參數:
113
#$conf["helpFunc"],如果解析的參數不成對,則要執行的函式名稱.
114
$conf["helpFunc"]="help";
115
#備註:
116
#僅能在命令列底下執行.
117
#建議:
118
#以後可將參數 --a--b 的名稱與後面的數值 $value 存成 $result["a"]["b"][$i]=$value .
119
$parseArgu=cmd::parseArgu($conf);
120
unset($conf);
121
 
122
#如果解析參數失敗
706 liveuser 123
if($parseArgu["status"]==="false"){
124
 
126 liveuser 125
	#印出結果
126
	var_dump($parseArgu);
127
 
128
	#結束執行
129
	exit;
130
 
706 liveuser 131
	}#if end
126 liveuser 132
 
133
#檢查參數
134
#函式說明:
135
#檢查必填與可省略參數,可省略參數可指定預設要給與什麼數值內容。
136
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
137
#$reuslt["error"],執行不正常結束的錯訊息陣列.
138
#$result["function"],當前執行的函式名稱.
139
#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
140
#$result[$shouldBeCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
141
#$result[$shouldBeCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
142
#$result[$shouldBeCheckedVarName]["error"],每個參數設定的錯誤訊息
143
#$result["shouldNotBeEmpty"],不應該為空字串或控陣列的變數.
144
#$result["argu"],字串陣列,目前輸入的參數名稱陣列.
145
#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.
146
#$result["notNeedVar"],字串陣列,多餘的參數名稱.
147
#必填寫的參數:
148
#$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
149
$conf["varInput"]=&$parseArgu["content"];
150
#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
151
$conf["referenceVarKey"]="variableCheck::checkArguments";
152
#可以省略的參數:
153
#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,元素數量需要跟"mustBeFilledVariableType"參數的元素數量一致,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
370 liveuser 154
$conf["mustBeFilledVariableName"]=array("config","acctVarName","passVarName","dbVarName","dbAddrVarName","dbPortVarName","backupAddr");
126 liveuser 155
#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列,元素數量需要跟"mustBeFilledVariableName"參數的元素數量一致,例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double","resource","object"); , null代表不指定變數形態.
370 liveuser 156
$conf["mustBeFilledVariableType"]=array("array","array","array","array","array","array","array");
126 liveuser 157
#$conf["canBeEmptyString"],字串,必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true",可以為空字串.
158
$conf["canBeEmptyString"]="false";
159
#$conf["canNotBeEmpty"],字串陣列,哪些必填參數的內容不得為空字串或空陣列,僅當$conf["canBeEmptyString"]為"true"時會生效.
160
#$conf["canNotBeEmpty"]=array();
161
#$conf["canBeEmpty"],字串陣列,哪些必填參數的內容可為空字串或空陣列,僅當$conf["canBeEmptyString"]為"false"時會生效.
162
#$conf["canBeEmpty"]=array();
163
#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
370 liveuser 164
#$conf["skipableVariableCanNotBeEmpty"]=array("backTime");
126 liveuser 165
#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
486 liveuser 166
$conf["skipableVariableName"]=array("backTime","times","backupFilesToKeep");
126 liveuser 167
#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
486 liveuser 168
$conf["skipableVariableType"]=array("array","array","array");
126 liveuser 169
#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
486 liveuser 170
$conf["skipableVarDefaultValue"]=array(null,null,array(7));
126 liveuser 171
#$conf["disallowAllSkipableVarIsEmpty"],字串,是否允許每個可省略參數都為空字串,預設為"true"允許,反之為"false".
172
#$conf["variableCheck::checkArguments"]["disallowAllSkipableVarIsEmpty"]="false";
173
#$conf["disallowAllSkipableVarIsEmptyArray"],字串,是否允許每個可省略參數都為空陣列,預設為"true"允許,反之為"false".
174
#$conf["disallowAllSkipableVarIsEmptyArray"]="";
175
#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
176
#$conf["arrayCountEqualCheck"][]=array();
177
#參考資料來源:
178
#array_keys=>http://php.net/manual/en/function.array-keys.php
179
$checkArguments=variableCheck::checkArguments($conf);
180
unset($conf);
181
 
182
#若執行失敗
183
if($checkArguments["status"]==="false"){
184
 
185
	#印出結果
186
	var_dump($checkArguments);
187
 
188
	#結束執行
189
	exit;
190
 
191
	}#if end
192
 
193
#若檢查不通過
194
if($checkArguments["passed"]==="false"){
195
 
196
	#印出結果
197
	var_dump($checkArguments);
198
 
780 liveuser 199
	#提示用法
200
	help();
201
 
126 liveuser 202
	#結束執行
203
	exit;
204
 
205
	}#if end
206
 
486 liveuser 207
#取得至少要保留幾份資料,超過數目會依照產生的日期,從舊的開始移除,直到剩下符合上限的數目.
208
$backupFilesToKeep=$parseArgu["content"]["backupFilesToKeep"][0];
209
 
126 liveuser 210
#取得各項參數
211
$config=$parseArgu["content"]["config"][0];
212
$acctVarName=$parseArgu["content"]["acctVarName"][0];
213
$passVarName=$parseArgu["content"]["passVarName"][0];
214
$dbVarName=$parseArgu["content"]["dbVarName"][0];
215
$dbAddrVarName=$parseArgu["content"]["dbAddrVarName"][0];
216
$dbPortVarName=$parseArgu["content"]["dbPortVarName"][0];
486 liveuser 217
$backupFilesToKeep=$parseArgu["content"]["backupFilesToKeep"][0];
126 liveuser 218
$backupAddr=$parseArgu["content"]["backupAddr"][0];
219
 
370 liveuser 220
#如果有設定 backTime
221
if(isset($parseArgu["content"]["backTime"])){
126 liveuser 222
 
370 liveuser 223
	#設置 $backTime
224
	$backTime=$parseArgu["content"]["backTime"][0];
225
 
226
	}#if end
227
 
228
#如果有設定 times
229
if(isset($parseArgu["content"]["times"])){
230
 
231
	#設置 $times
232
	$times=$parseArgu["content"]["times"][0];
233
 
234
	}#if end
465 liveuser 235
 
236
#反之若沒有指定 backTime
237
else if(!isset($backTime)){
370 liveuser 238
 
465 liveuser 239
	#指定只跑一次
240
	$times=1;
241
 
242
	}#else end
243
 
370 liveuser 244
#如果有設置 backTime
245
if(isset($backTime)){
246
 
247
	#函式說明:
248
	#將固定格式的字串分開,並回傳分開的結果。
249
	#回傳結果:
250
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
251
	#$result["error"],錯誤訊息陣列
252
	#$result["function"],當前執行的函數名稱.
253
	#$result["argu"],使用的參數.
254
	#$result["oriStr"],要分割的原始字串內容
255
	#$result["dataArray"],爲分割好字串的陣列內容,$result["dataArray"][$i]爲第($i+1)段的內容。
256
	#$result["dataCounts"],爲總共分成幾段
257
	#$result["found"],是否有在$conf["stringIn"]找到$conf["spiltSymbol"],"true"代表有找到,"false"代表沒有找到.
258
	#必填參數:
259
	#$conf["stringIn"],字串,要處理的字串.
260
	$conf["stringIn"]=$backTime;
261
	#$conf["spiltSymbol"],字串,爲以哪個符號作爲分割.
262
	$conf["spiltSymbol"]=":";
263
	#可省略參數:
264
	#$conf["allowEmptyStr"],是否允許分割出來空字串,預設為"false"不允許;"true"代表允許.
265
	$conf["allowEmptyStr"]="false";
266
	#備註:
267
	#無.
268
	$spiltString=stringProcess::spiltString($conf);
269
	unset($conf);
270
 
271
	#如果取得小時跟分鐘失敗
272
	if($spiltString["status"]==="false"){
126 liveuser 273
 
370 liveuser 274
		#印出結果
275
		var_dump($spiltString);
276
 
277
		#結束執行
278
		exit;
279
 
280
		}#if end
126 liveuser 281
 
370 liveuser 282
	#如果取得小時跟分鐘失敗
283
	if($spiltString["found"]==="false"){
126 liveuser 284
 
370 liveuser 285
		#印出結果
286
		var_dump($spiltString);
287
 
288
		#結束執行
289
		exit;
126 liveuser 290
 
370 liveuser 291
		}#if end
126 liveuser 292
 
370 liveuser 293
	#取得要執行的小時
294
	$runHour=$spiltString["dataArray"][0];
126 liveuser 295
 
370 liveuser 296
	#取得要執行的分鐘
297
	$runMin=$spiltString["dataArray"][1];
298
 
299
	}#if end
300
 
486 liveuser 301
#取得至少要保留幾份資料,超過數目會依照產生的日期,從舊的開始移除,直到剩下符合上限的數目.
302
$backupFilesToKeep=$parseArgu["content"]["backupFilesToKeep"][0];
303
 
126 liveuser 304
#初始化上次執行的時間點
305
$lastRunTime=0;
306
 
370 liveuser 307
#初始化要繼續執行
308
$continue=true;
309
 
310
#初始化執行的次數
311
$runTimes=0;
312
 
126 liveuser 313
#無窮迴圈
370 liveuser 314
while($continue){
315
 
126 liveuser 316
	#休息一秒
317
	sleep(1);
318
 
319
	#取得當前小時
320
	$currentHour=gmdate("H");
321
 
322
	#取得當前分鐘
323
	$currentMin=gmdate("i");
324
 
370 liveuser 325
	#如果有指定執行的時間
326
	if(isset($runHour) && isset($runMin)){
126 liveuser 327
 
370 liveuser 328
		#如果不是要執行的時間
329
		if($currentHour!==$runHour || $currentMin!==$runMin){
330
 
331
			#下一輪
332
			continue;
333
 
334
			}#if end
335
 
336
		}#if end
337
 
126 liveuser 338
	#如果距離上次執行沒有過了1分鐘
370 liveuser 339
	if( time()-$lastRunTime<60 ){
340
 
126 liveuser 341
		#下一輪
342
		continue;
343
 
370 liveuser 344
		}#if end
345
 
126 liveuser 346
	#取得設定檔內容
347
	#函式說明:
348
	#解析PHP檔案裡面的變數.
349
	#回傳結果:
350
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
351
	#$result["error"],錯誤訊息.
352
	#$result["function"],當前執行的函數名稱.
353
	#$result["argu"],所使用的參數.	
354
	#$result["content"],找到的變數內容陣列.
355
	#$result["content"]["value"],依找到變數順序的數值.
356
	#$result["content"]["struc"],依找到變數順序的階層結構.
357
	#$result["content"]["direct"],變數名稱對應的數值內容.
358
	#必填參數:
359
	#$conf["file"],字串,檔案的路徑與名稱.
360
	$conf["file"]=$config;
361
	#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑
362
	$conf["fileArgu"]=__FILE__;
363
	#$conf["varName"],字串陣列,要搜尋的變數名稱,例如要搜尋變數$email則輸入"email".
364
	$conf["varName"]=array($acctVarName,$passVarName,$dbVarName,$dbAddrVarName,$dbPortVarName);
365
	#可省略參數:
366
	#$conf["web"],是要取得網路上的檔案則為"true";反之則為"false",預設為"false".
367
	$conf["web"]="false";
368
	#參考資料:
369
	#https://www.php.net/manual/en/function.parse-str.php
370
	#備註:
371
	#無.
372
	$parseVaraiableInPHPfile=fileAccess::parseVaraiableInPHPfile($conf);
373
	unset($conf);
374
 
375
	#如果失敗
370 liveuser 376
	if($parseVaraiableInPHPfile["status"]==="false"){
126 liveuser 377
 
378
		#印出結果
379
		var_dump($parseVaraiableInPHPfile);
380
 
381
		#結束執行
382
		exit;
383
 
370 liveuser 384
		}#if end
126 liveuser 385
 
386
	#取得資料庫帳號
537 liveuser 387
	$dbAcct=$parseVaraiableInPHPfile["content"][$acctVarName];
126 liveuser 388
 
389
	#取得資料庫密碼
537 liveuser 390
	$dbPass=$parseVaraiableInPHPfile["content"][$passVarName];
126 liveuser 391
 
392
	#取得資料庫名稱
537 liveuser 393
	$dbName=$parseVaraiableInPHPfile["content"][$dbVarName];
126 liveuser 394
 
395
	#取得資料位置
537 liveuser 396
	$dbAddr=$parseVaraiableInPHPfile["content"][$dbAddrVarName];
126 liveuser 397
 
398
	#取得資料庫port
537 liveuser 399
	$dbPort=$parseVaraiableInPHPfile["content"][$dbPortVarName];
126 liveuser 400
 
401
	#判斷目標目錄是否可以存取
402
	#函式說明:
403
	#開啟特定目錄,取得底下的檔案路徑清單.
404
	#回傳結果:
405
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
406
	#$result["error"],錯誤訊息.
407
	#$result["function"],當前執行的函數名稱.
408
	#$result["argu"],所使用的參數.	
409
	#$result["content"],讀取到的內容陣列.
410
	#必填參數:
411
	#$conf["path"],字串,要取得檔案資訊的所屬路徑.
412
	$conf["path"]=$backupAddr;
413
	#可省略參數:
414
	#無.
415
	#參考資料
416
	#無.
417
	#備註:
418
	#無.
419
	$listInfo=fileAccess::listInfo($conf);
420
	unset($conf);
421
 
422
	#如果失敗
370 liveuser 423
	if($listInfo["status"]==="false"){
126 liveuser 424
 
425
		#印出結果
426
		var_dump($listInfo);
427
 
428
		#結束執行
429
		exit;
430
 
370 liveuser 431
		}#if end
126 liveuser 432
 
141 liveuser 433
	#產生uuid
434
	#涵式說明:
435
	#呼叫shell執行系統命令,並取得回傳的內容.
436
	#回傳的結果:
437
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
438
	#$result["error"],錯誤訊息陣列.
439
	#$result["function"],當前執行的函數名稱.
440
	#$result["argu"],使用的參數.
441
	#$result["cmd"],執行的指令內容.
442
	#$result["fullCmd"],如果參數 $conf["inBackGround"] 為 "true" 則會回傳該值.
443
	#$result["output"],爲執行完二元碼後的輸出陣列,若 $conf["inBackGround"] 為 "true",則為當下的輸出.
444
	#$result["tmpFileOutput"],儲存輸出的暫存檔案名稱,若 $conf["inBackGround"] 為 "true" 則會回傳該值.
445
	#$result["running"],是否還在執行.
446
	#$result["pid"],pid.
447
	#$result["statusCode"],執行結束後的代碼.
448
	#必填的參數
449
	#$conf["command"],字串,要執行的指令與.
450
	$conf["external::callShell"]["command"]="uuid";
451
	#$conf["fileArgu"],字串,變數__FILE__的內容.
142 liveuser 452
	$conf["external::callShell"]["fileArgu"]=__FILE__;
141 liveuser 453
	#可省略參數:
454
	#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
455
	#$conf["argu"]=array("");
456
	#$conf["arguIsAddr"],陣列字串,指令搭配的哪些參數為路徑,為路徑的參數會進行轉換以便符合呼叫當前函數的位置,預設不指定,若有3個參數,其中第3個參數為路徑,則表示為array("false","false","true").
457
	#$conf["arguIsAddr"]=array();
458
	#$conf["pre"],陣列,要在本指令前執行的每個指令與參數.
459
	#$conf["pre"][$i]["cmd"],字串,要在本指令前執行的第$i+1個指令.
460
	#$conf["pre"][$i]["param"],陣列字串,要在本指令前執行的第$i+1個指令的參數.
461
	#$conf["enablePrintDescription"],字串,是否要印出$conf["printDescription"]的內容,"true"代表要,"false"代表不要,預設為"false".
462
	#$conf["enablePrintDescription"]="true";
463
	#$conf["printDescription"],字串,執行該外部程式前要印出來的的文字,預設為$conf["command"]的內容加上使用的$conf["argu"]參數.
464
	#$conf["printDescription"]="";
465
	#$conf["escapeshellarg"],字串,是否要啟用過濾參數,用了比較安全,但可能會出錯,"true"為啟用,"false"為不啟用,預設為"false".
453 liveuser 466
	$conf["external::callShell"]["escapeshellarg"]="true";
141 liveuser 467
	#$conf["username"],字串,要用什麼使用者來執行,預設為執行php的使用者,該參數不適用於apache環境.
468
	#$conf["username"]="";
469
	#$conf["password"],字串,root的使用者密碼,預設不使用密碼,該參數不適用於apache環境.
470
	#$conf["password"]="";
471
	#$conf["useScript"],字串,是否要啟用Linux的script指令來記錄輸出,"true"代表要,Fedora的selinux會擋住該操作;"false"代表不要,預設為"false".
472
	#$conf["useScript"]="";
473
	#$conf["logFilePath"],字串,當 $conf["useScript"] 為 "true" 時,輸出的內容要暫存到哪裡,預設為 "/tmp/.qbpwcf_tmp/external/callShell/".
474
	#$conf["logFilePath"]=".qbpwcf_tmp/external/callShell/";
475
	#$conf["inBackGround"],字串,是否要在背景執行,且不會等待程式執行結束再執行下一個指令,"true"代表是,"false"代表不要,預設為"false",如果$conf["command"]有用「;」區隔的多個指令將會出錯.
476
	#$conf["inBackGround"]="";
477
	#$conf["getErr"],字串,"true"代表將錯誤輸出變成標準輸出,反之"false"為不變動.
478
	#$conf["getErr"]="false";
479
	#備註:
480
	#不是所有指令都能用apache的身份執行,目前已知java,javac指令無法執行,使用root身份可能會被selinux阻擋.
481
	#參考資料:
482
	#exec=>http://php.net/manual/en/function.exec.php
483
	#escapeshellcmd=>http://php.net/manual/en/function.escapeshellcmd.php
484
	#escapeshellarg=>http://php.net/manual/en/function.escapeshellarg.php
485
	$callShell=external::callShell($conf["external::callShell"]);
486
	unset($conf["external::callShell"]);		
487
 
488
	#如果執行失敗
489
	if($callShell["status"]==="false"){
490
 
491
		#設置執行不正常
492
		$result	["status"]="false";
493
 
494
		#設置執行錯誤
495
		$result["error"]=$callShell;
496
 
497
		#回傳結果
498
		return $result;
499
 
500
		}#if end
501
 
502
	#取得uuid
503
	$uuid=$callShell["output"][0];
504
 
126 liveuser 505
	#函式說明:
382 liveuser 506
	#將 ~ 轉換為家目錄路徑
507
	#回傳結果:
508
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
509
	#$result["error"],錯誤訊息.
510
	#$result["function"],當前執行的函數名稱.
511
	#$result["content"],轉換好的內容.
512
	#必填參數:
513
	#$conf["fileArgu"],字串,變數__FILE__的內容.
514
	$conf["fileAccess::tildeToPath"]["fileArgu"]=__FILE__;
515
	#可省略參數:
516
	#無.
517
	#參考資料:
518
	#https://www.businessweekly.com.tw/careers/Blog/14307
519
	$tildeToPath=fileAccess::tildeToPath($conf["fileAccess::tildeToPath"]);
520
	unset($conf["fileAccess::tildeToPath"]);
521
 
522
	#如果執行失敗
523
	if($tildeToPath["status"]==="false"){
524
 
525
		#設置執行不正常
526
		$result	["status"]="false";
527
 
528
		#設置執行錯誤
529
		$result["error"]=$tildeToPath;
530
 
531
		#回傳結果
532
		return $result;
533
 
534
		}#if end
535
 
536
	#取得家目錄路徑
537
	$homePath=$tildeToPath["content"];
538
 
539
	#函式說明:
126 liveuser 540
	#備份資料庫
541
	#回傳結果::
542
	#$result["status"],"true"代表執行正常,"false"代表執行有誤.
543
	#$result["error"],錯誤訊息陣列.
544
	#$result["sqlAddress"],sql檔案的位置.
545
	#$result["function"],當前執行的函數名稱
546
	#必填參數:
547
	#$conf["backedDatabaseName"],字串.
548
	$conf["backedDatabaseName"]=$dbName;#爲要備份的資料庫名稱.
549
	#$conf["dbAddress"],字串,資料庫的位置.
550
	$conf["dbAddress"]=$dbAddr;
551
	#$conf["dbAccount"],字串,爲用於連入mysql-Server時要使用的帳號.
552
	$conf["dbAccount"]=$dbAcct;
553
	#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑
554
	$conf["fileArgu"]=__FILE__;
555
	#可省略參數:
556
	#$conf["dbPassword"],字串,爲連線到mysql-Server時要使用的密碼,可省略,若省略則代表不使用密碼
557
	$conf["dbPassword"]=$dbPass;
558
	#$conf["storePlace"],字串,要將輸出檔案儲存到哪邊,預設爲當前目錄.
141 liveuser 559
	#$conf["storePlace"]=$backupAddr;
382 liveuser 560
	$conf["storePlace"]=$homePath."/";
126 liveuser 561
	#$conf["exportFileName"],字串,要輸出的檔案名稱,預設爲其export
151 liveuser 562
	$sqlFileName=$conf["exportFileName"]=gmdate("Y-m-d_H:i:s")."_".$uuid;
126 liveuser 563
 
564
	#如果資料庫不是位於 localhost
370 liveuser 565
	if($conf["dbAddress"]!=="localhost"){
126 liveuser 566
 
567
		#$conf["dbPort"],字串,爲連線到mysql-Server時要使用的port,預設為3306.
568
		$conf["dbPort"]=$dbPort;
569
 
370 liveuser 570
		}#if end
126 liveuser 571
 
572
	#備註:
573
	#無.
574
	$backupDatabase=db::backupDatabase($conf);
575
	unset($conf);
576
 
577
	#如果執行失敗
370 liveuser 578
	if($backupDatabase["status"]==="false"){
126 liveuser 579
 
580
		#印出結果
581
		var_dump($backupDatabase);
582
 
583
		#結束執行
584
		exit;
585
 
370 liveuser 586
		}#if end
130 liveuser 587
 
588
	#函式說明:
589
	#將資料夾打包成.zst檔案
590
	#回傳的結果:
591
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
592
	#$result["error"],錯誤訊息.
593
	#$result["function"],當前執行的函式名稱.
594
	#$result["storePlace"],檔案輸出後的位置與名稱.
595
	#必填參數:
596
	#$conf["commentsArray"],字串陣列,提示輸入的文字描述,$conf["commentsArray"][$i]代表第($+1)行的描述.
597
	$conf["commentsArray"]=array("正透過zstd壓縮");
598
	#$conf["target"],字串,要打包的檔案.
151 liveuser 599
	$conf["target"]=$sqlFileName.".sql";
130 liveuser 600
	#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑.
601
	$conf["fileArgu"]=__FILE__;
602
	#可省略參數:
603
	#$conf["newLineBreak"],字串,是否$conf["commentsArray"]的每個元素後面都要斷行,"false"代表不要,預設為"true"要斷行.
604
	#$conf["newLineBreak"]="false";
605
	#$conf["path"],字串,要打包的檔案的路徑,預設為當前路徑.
486 liveuser 606
	$conf["path"]=$homePath;
130 liveuser 607
	#$conf["delAfterSuc"],字串,是否壓縮後將原始檔案移除,預設為"false"代表不要;反之為"true"代表要.
608
	$conf["delAfterSuc"]="true";
609
	$makeZstFile=cmd::makeZstFile($conf);
610
	unset($conf);
126 liveuser 611
 
130 liveuser 612
	#如果執行失敗
370 liveuser 613
	if($makeZstFile["status"]==="false"){
130 liveuser 614
 
615
		#印出結果
616
		var_dump($makeZstFile);
617
 
618
		#結束執行
619
		exit;
620
 
370 liveuser 621
		}#if end
130 liveuser 622
 
141 liveuser 623
	#移動檔案到指定的位置
624
	#涵式說明:
625
	#呼叫shell執行系統命令,並取得回傳的內容.
626
	#回傳的結果:
627
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
628
	#$result["error"],錯誤訊息陣列.
629
	#$result["function"],當前執行的函數名稱.
630
	#$result["argu"],使用的參數.
631
	#$result["cmd"],執行的指令內容.
632
	#$result["fullCmd"],如果參數 $conf["inBackGround"] 為 "true" 則會回傳該值.
633
	#$result["output"],爲執行完二元碼後的輸出陣列,若 $conf["inBackGround"] 為 "true",則為當下的輸出.
634
	#$result["tmpFileOutput"],儲存輸出的暫存檔案名稱,若 $conf["inBackGround"] 為 "true" 則會回傳該值.
635
	#$result["running"],是否還在執行.
636
	#$result["pid"],pid.
637
	#$result["statusCode"],執行結束後的代碼.
638
	#必填的參數
639
	#$conf["command"],字串,要執行的指令與.
640
	$conf["external::callShell"]["command"]="mv";
641
	#$conf["fileArgu"],字串,變數__FILE__的內容.
142 liveuser 642
	$conf["external::callShell"]["fileArgu"]=__FILE__;
141 liveuser 643
	#可省略參數:
644
	#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
645
	$conf["external::callShell"]["argu"][]=$makeZstFile["storePlace"];
153 liveuser 646
	$conf["external::callShell"]["argu"][]=$backupAddr."/";
141 liveuser 647
	#$conf["arguIsAddr"],陣列字串,指令搭配的哪些參數為路徑,為路徑的參數會進行轉換以便符合呼叫當前函數的位置,預設不指定,若有3個參數,其中第3個參數為路徑,則表示為array("false","false","true").
648
	#$conf["arguIsAddr"]=array();
649
	#$conf["pre"],陣列,要在本指令前執行的每個指令與參數.
650
	#$conf["pre"][$i]["cmd"],字串,要在本指令前執行的第$i+1個指令.
651
	#$conf["pre"][$i]["param"],陣列字串,要在本指令前執行的第$i+1個指令的參數.
652
	#$conf["enablePrintDescription"],字串,是否要印出$conf["printDescription"]的內容,"true"代表要,"false"代表不要,預設為"false".
653
	#$conf["enablePrintDescription"]="true";
654
	#$conf["printDescription"],字串,執行該外部程式前要印出來的的文字,預設為$conf["command"]的內容加上使用的$conf["argu"]參數.
655
	#$conf["printDescription"]="";
656
	#$conf["escapeshellarg"],字串,是否要啟用過濾參數,用了比較安全,但可能會出錯,"true"為啟用,"false"為不啟用,預設為"false".
453 liveuser 657
	$conf["external::callShell"]["escapeshellarg"]="true";
141 liveuser 658
	#$conf["username"],字串,要用什麼使用者來執行,預設為執行php的使用者,該參數不適用於apache環境.
659
	#$conf["username"]="";
660
	#$conf["password"],字串,root的使用者密碼,預設不使用密碼,該參數不適用於apache環境.
661
	#$conf["password"]="";
662
	#$conf["useScript"],字串,是否要啟用Linux的script指令來記錄輸出,"true"代表要,Fedora的selinux會擋住該操作;"false"代表不要,預設為"false".
663
	#$conf["useScript"]="";
664
	#$conf["logFilePath"],字串,當 $conf["useScript"] 為 "true" 時,輸出的內容要暫存到哪裡,預設為 "/tmp/.qbpwcf_tmp/external/callShell/".
665
	#$conf["logFilePath"]=".qbpwcf_tmp/external/callShell/";
666
	#$conf["inBackGround"],字串,是否要在背景執行,且不會等待程式執行結束再執行下一個指令,"true"代表是,"false"代表不要,預設為"false",如果$conf["command"]有用「;」區隔的多個指令將會出錯.
667
	#$conf["inBackGround"]="";
668
	#$conf["getErr"],字串,"true"代表將錯誤輸出變成標準輸出,反之"false"為不變動.
669
	#$conf["getErr"]="false";
670
	#備註:
671
	#不是所有指令都能用apache的身份執行,目前已知java,javac指令無法執行,使用root身份可能會被selinux阻擋.
672
	#參考資料:
673
	#exec=>http://php.net/manual/en/function.exec.php
674
	#escapeshellcmd=>http://php.net/manual/en/function.escapeshellcmd.php
675
	#escapeshellarg=>http://php.net/manual/en/function.escapeshellarg.php
676
	$callShell=external::callShell($conf["external::callShell"]);
677
	unset($conf["external::callShell"]);		
678
 
679
	#如果執行失敗
680
	if($callShell["status"]==="false"){
681
 
153 liveuser 682
		#印出結果
683
		var_dump($callShell);
684
 
685
		#結束執行
686
		exit;
141 liveuser 687
 
688
		}#if end
689
 
126 liveuser 690
	#更新最後執行的時間點
691
	$lastRunTime=time();
370 liveuser 692
 
693
	#增加已經執行的次數
694
	$runTimes++;
126 liveuser 695
 
370 liveuser 696
	#如果有指定次數
697
	if(isset($times)){
698
 
699
		#如果已經達到次數的限制
700
		if($runTimes>=$times){
701
 
702
			#設定停止執行
703
			$continue=false;
704
 
705
			}#if end
706
 
707
		}#if end
486 liveuser 708
 
709
	#函式說明:
710
	#取得目錄底下的詳細資訊.
711
	#回傳結果:
712
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
713
	#$result["error"],錯誤訊息.
714
	#$result["function"],當前執行的函數名稱.
715
	#$result["size"],該清單的大小,單位為bytes.
716
	#$result["dataCount"],該清單的長度.
717
	#$result["content"],指定目錄底下的所有檔案資訊.
718
	#$result["content"][$i]["nType&permission"],第$i個節點類型、權限.
719
	#$result["content"][$i]["nType"],第$i個節點類型.
720
	#$result["content"][$i]["permission"],第$i個節點權限.
721
	#$result["content"][$i]["ownByUser"],第$i個節點擁有者賬號.
722
	#$result["content"][$i]["ownByGroup"],第$i個節點擁有者群組.
723
	#$result["content"][$i]["bytes"],第$i個節點大小.
724
	#$result["content"][$i]["date"],第$i個節點最後變更日期.
725
	#$result["content"][$i]["time"],第$i個節點最後異動時間.
726
	#$result["content"][$i]["timeDetail"],第$i個節點最後異動詳細時間.
727
	#$result["content"][$i]["timezone"],第$i個節點的時區.
728
	#$result["content"][$i]["name"],第$i個節點的名稱.
729
	#必填參數:
730
	#$conf["path"],字串,要檢視的路徑.
731
	$conf["path"]=$backupAddr;
732
	#可省略參數:
733
	#無.
734
	#參考資料:
735
	#https://www.businessweekly.com.tw/careers/Blog/14307
736
	#備註:
737
	#無.
738
	$ls=fileAccess::ls($conf);
739
	unset($conf);
370 liveuser 740
 
486 liveuser 741
	#如果執行失敗
742
	if($ls["status"]==="false"){
743
 
744
		#印出結果
745
		var_dump($ls);
746
 
747
		#結束執行
748
		exit;
749
 
750
		}#if end
751
 
752
	#初始化儲存存在的備份檔案清單
753
	$backupFiles=array();
754
 
755
	#針對每個節點
756
	foreach($ls["content"] as $fileInfo){
757
 
758
		#如果是檔案
490 liveuser 759
		if($fileInfo["nType"]==="-"){
486 liveuser 760
 
761
			#記錄起來
762
			$backupFiles[]=$backupAddr."/".$fileInfo["name"];
763
 
764
			}#if end
765
 
766
		}#foreach end
767
 
768
	#如果節點數量大於 $backupFilesToKeep
769
	if(count($backupFiles)>$backupFilesToKeep){
770
 
771
		#有多少超過數量的節點就執行幾次
772
		for($i=$backupFilesToKeep;$i<count($backupFiles);$i++){
773
 
774
			#設置要移除的檔案
492 liveuser 775
			$file2remove=$backupFiles[$i];
486 liveuser 776
 
777
			#移除超過限制的舊資料
778
			#函式說明:
779
			#移除檔案
780
			#回傳結果:
781
			#$result["status"],"true"代表移除成功,"false"代表移除失敗.
782
			#$result["error"],錯誤訊息陣列.
783
			#$result["warning"],警告訊息陣列.
784
			#$result["function"],當前執行的函數名稱.
785
			#$result["argu"],當前函式使用的參數.
786
			#必填參數:
787
			#$conf["fileAddress"],字串,要移除檔案的位置.
788
			$conf["fileAddress"]=$file2remove;
789
			#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑.
790
			$conf["fileArgu"]=__FILE__;
791
			#可省略參數:
792
			#$conf["commentsArray"],字串陣列,提示的文字描述,$conf["commentsArray"][$i]代表第($+1)行的描述.
492 liveuser 793
			$conf["commentsArray"]=array("已移除 ".$file2remove);
486 liveuser 794
			#$conf["allowDelSymlink"],字串,預設為"false",不移除軟連結;"true"代表要移除軟連結.
795
			#$conf["allowDelSymlink"]="true";
796
			#$conf["allowDelFolder"],字串,預設為"false",不移除目錄;"true"代表要移除目錄.
797
			#$conf["allowDelFolder"]="true";
798
			#參考資料:
799
			#無.
800
			#備註:
801
			#無.
802
			$delFile=fileAccess::delFile($conf);
803
			unset($conf);
804
 
805
			#如果執行失敗
806
			if($delFile["status"]==="false"){
807
 
808
				#印出結果
809
				var_dump($ls);
810
 
811
				#結束執行
812
				exit;
813
 
814
				}#if end
492 liveuser 815
 
486 liveuser 816
			}#for end
817
 
818
		}#if end
819
 
370 liveuser 820
	}#while end	
821
 
126 liveuser 822
?>