Subversion Repositories qbpwcf-lib(archive)

Rev

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