Subversion Repositories php-qbpwcf

Rev

Rev 3 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
3 liveuser 1
#!/usr/bin/php
2
<?php
3
 
4
/*
5
	QBPWCF, Quick Build PHP website Component base on Fedora Linux.
226 liveuser 6
    Copyright (C) 2014~2025 MIN ZHI, CHEN
3 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/>.
226 liveuser 22
 
3 liveuser 23
*/
24
 
25
/*
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
 
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
 
53
#以該檔案的實際位置的 lib path 為 include path 首位
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
 
226 liveuser 68
#設置 include path
3 liveuser 69
set_include_path($output[0].PATH_SEPARATOR.get_include_path());
70
 
71
#匯入外部套件
72
include("allInOne.php");
73
 
74
#說明函式
75
function help(){
76
 
77
	#印出指令說明
226 liveuser 78
	echo "Usage of ".basename(__FILE__).":".PHP_EOL;
3 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;
88
	echo "--backupFilesToKeep 1~N 至少要保留幾份資料,超過數目會依照產生的日期,從舊的開始移除,直到剩下符合上限的數目,預設為7.".PHP_EOL;
89
	echo "時間到周而復始執行的範例:".PHP_EOL;
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;
91
	echo "時間到只執行一次的範例:".PHP_EOL;
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;
93
	echo "立即執行的範例:".PHP_EOL;
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;
226 liveuser 95
 
3 liveuser 96
	#結束執行
97
	exit;
98
 
99
	}#function help end
100
 
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
#如果解析參數失敗
123
if($parseArgu["status"]==="false"){
124
 
125
	#印出結果
126
	var_dump($parseArgu);
226 liveuser 127
 
3 liveuser 128
	#結束執行
129
	exit;
226 liveuser 130
 
3 liveuser 131
	}#if end
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");
154
$conf["mustBeFilledVariableName"]=array("config","acctVarName","passVarName","dbVarName","dbAddrVarName","dbPortVarName","backupAddr");
155
#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列,元素數量需要跟"mustBeFilledVariableName"參數的元素數量一致,例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double","resource","object"); , null代表不指定變數形態.
156
$conf["mustBeFilledVariableType"]=array("array","array","array","array","array","array","array");
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"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
164
#$conf["skipableVariableCanNotBeEmpty"]=array("backTime");
165
#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
166
$conf["skipableVariableName"]=array("backTime","times","backupFilesToKeep");
167
#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
168
$conf["skipableVariableType"]=array("array","array","array");
169
#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
170
$conf["skipableVarDefaultValue"]=array(null,null,array(7));
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);
226 liveuser 187
 
3 liveuser 188
	#結束執行
189
	exit;
190
 
191
	}#if end
192
 
193
#若檢查不通過
194
if($checkArguments["passed"]==="false"){
195
 
196
	#印出結果
197
	var_dump($checkArguments);
226 liveuser 198
 
3 liveuser 199
	#提示用法
200
	help();
226 liveuser 201
 
3 liveuser 202
	#結束執行
203
	exit;
204
 
205
	}#if end
206
 
207
#取得至少要保留幾份資料,超過數目會依照產生的日期,從舊的開始移除,直到剩下符合上限的數目.
208
$backupFilesToKeep=$parseArgu["content"]["backupFilesToKeep"][0];
209
 
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];
217
$backupFilesToKeep=$parseArgu["content"]["backupFilesToKeep"][0];
218
$backupAddr=$parseArgu["content"]["backupAddr"][0];
219
 
220
#如果有設定 backTime
221
if(isset($parseArgu["content"]["backTime"])){
222
 
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
226 liveuser 235
 
3 liveuser 236
#反之若沒有指定 backTime
237
else if(!isset($backTime)){
238
 
239
	#指定只跑一次
240
	$times=1;
241
 
242
	}#else end
243
 
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"){
226 liveuser 273
 
3 liveuser 274
		#印出結果
275
		var_dump($spiltString);
226 liveuser 276
 
3 liveuser 277
		#結束執行
278
		exit;
226 liveuser 279
 
3 liveuser 280
		}#if end
281
 
282
	#如果取得小時跟分鐘失敗
283
	if($spiltString["found"]==="false"){
226 liveuser 284
 
3 liveuser 285
		#印出結果
286
		var_dump($spiltString);
226 liveuser 287
 
3 liveuser 288
		#結束執行
289
		exit;
290
 
291
		}#if end
292
 
293
	#取得要執行的小時
294
	$runHour=$spiltString["dataArray"][0];
295
 
296
	#取得要執行的分鐘
297
	$runMin=$spiltString["dataArray"][1];
298
 
299
	}#if end
300
 
301
#取得至少要保留幾份資料,超過數目會依照產生的日期,從舊的開始移除,直到剩下符合上限的數目.
302
$backupFilesToKeep=$parseArgu["content"]["backupFilesToKeep"][0];
303
 
304
#初始化上次執行的時間點
305
$lastRunTime=0;
306
 
307
#初始化要繼續執行
308
$continue=true;
309
 
310
#初始化執行的次數
311
$runTimes=0;
312
 
313
#無窮迴圈
314
while($continue){
315
 
316
	#休息一秒
317
	sleep(1);
226 liveuser 318
 
3 liveuser 319
	#取得當前小時
320
	$currentHour=gmdate("H");
226 liveuser 321
 
3 liveuser 322
	#取得當前分鐘
323
	$currentMin=gmdate("i");
226 liveuser 324
 
3 liveuser 325
	#如果有指定執行的時間
326
	if(isset($runHour) && isset($runMin)){
226 liveuser 327
 
3 liveuser 328
		#如果不是要執行的時間
329
		if($currentHour!==$runHour || $currentMin!==$runMin){
226 liveuser 330
 
3 liveuser 331
			#下一輪
332
			continue;
226 liveuser 333
 
3 liveuser 334
			}#if end
226 liveuser 335
 
3 liveuser 336
		}#if end
226 liveuser 337
 
3 liveuser 338
	#如果距離上次執行沒有過了1分鐘
339
	if( time()-$lastRunTime<60 ){
226 liveuser 340
 
3 liveuser 341
		#下一輪
342
		continue;
226 liveuser 343
 
3 liveuser 344
		}#if end
226 liveuser 345
 
3 liveuser 346
	#取得設定檔內容
347
	#函式說明:
348
	#解析PHP檔案裡面的變數.
349
	#回傳結果:
350
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
351
	#$result["error"],錯誤訊息.
352
	#$result["function"],當前執行的函數名稱.
226 liveuser 353
	#$result["argu"],所使用的參數.
3 liveuser 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);
226 liveuser 374
 
3 liveuser 375
	#如果失敗
376
	if($parseVaraiableInPHPfile["status"]==="false"){
226 liveuser 377
 
3 liveuser 378
		#印出結果
379
		var_dump($parseVaraiableInPHPfile);
226 liveuser 380
 
3 liveuser 381
		#結束執行
382
		exit;
226 liveuser 383
 
3 liveuser 384
		}#if end
226 liveuser 385
 
3 liveuser 386
	#取得資料庫帳號
387
	$dbAcct=$parseVaraiableInPHPfile["content"][$acctVarName];
226 liveuser 388
 
3 liveuser 389
	#取得資料庫密碼
390
	$dbPass=$parseVaraiableInPHPfile["content"][$passVarName];
226 liveuser 391
 
3 liveuser 392
	#取得資料庫名稱
393
	$dbName=$parseVaraiableInPHPfile["content"][$dbVarName];
226 liveuser 394
 
3 liveuser 395
	#取得資料位置
396
	$dbAddr=$parseVaraiableInPHPfile["content"][$dbAddrVarName];
226 liveuser 397
 
3 liveuser 398
	#取得資料庫port
399
	$dbPort=$parseVaraiableInPHPfile["content"][$dbPortVarName];
226 liveuser 400
 
3 liveuser 401
	#判斷目標目錄是否可以存取
402
	#函式說明:
403
	#開啟特定目錄,取得底下的檔案路徑清單.
404
	#回傳結果:
405
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
406
	#$result["error"],錯誤訊息.
407
	#$result["function"],當前執行的函數名稱.
226 liveuser 408
	#$result["argu"],所使用的參數.
3 liveuser 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);
226 liveuser 421
 
3 liveuser 422
	#如果失敗
423
	if($listInfo["status"]==="false"){
226 liveuser 424
 
3 liveuser 425
		#印出結果
426
		var_dump($listInfo);
226 liveuser 427
 
3 liveuser 428
		#結束執行
429
		exit;
226 liveuser 430
 
3 liveuser 431
		}#if end
226 liveuser 432
 
3 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__的內容.
452
	$conf["external::callShell"]["fileArgu"]=__FILE__;
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".
466
	$conf["external::callShell"]["escapeshellarg"]="true";
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"]);
226 liveuser 486
	unset($conf["external::callShell"]);
487
 
3 liveuser 488
	#如果執行失敗
489
	if($callShell["status"]==="false"){
226 liveuser 490
 
3 liveuser 491
		#設置執行不正常
492
		$result	["status"]="false";
493
 
494
		#設置執行錯誤
495
		$result["error"]=$callShell;
496
 
497
		#回傳結果
498
		return $result;
226 liveuser 499
 
3 liveuser 500
		}#if end
501
 
502
	#取得uuid
503
	$uuid=$callShell["output"][0];
226 liveuser 504
 
3 liveuser 505
	#函式說明:
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"]);
226 liveuser 521
 
3 liveuser 522
	#如果執行失敗
523
	if($tildeToPath["status"]==="false"){
226 liveuser 524
 
3 liveuser 525
		#設置執行不正常
526
		$result	["status"]="false";
527
 
528
		#設置執行錯誤
529
		$result["error"]=$tildeToPath;
530
 
531
		#回傳結果
532
		return $result;
226 liveuser 533
 
3 liveuser 534
		}#if end
226 liveuser 535
 
3 liveuser 536
	#取得家目錄路徑
537
	$homePath=$tildeToPath["content"];
226 liveuser 538
 
3 liveuser 539
	#函式說明:
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"],字串,要將輸出檔案儲存到哪邊,預設爲當前目錄.
559
	#$conf["storePlace"]=$backupAddr;
560
	$conf["storePlace"]=$homePath."/";
561
	#$conf["exportFileName"],字串,要輸出的檔案名稱,預設爲其export
562
	$sqlFileName=$conf["exportFileName"]=gmdate("Y-m-d_H:i:s")."_".$uuid;
226 liveuser 563
 
3 liveuser 564
	#如果資料庫不是位於 localhost
565
	if($conf["dbAddress"]!=="localhost"){
226 liveuser 566
 
3 liveuser 567
		#$conf["dbPort"],字串,爲連線到mysql-Server時要使用的port,預設為3306.
568
		$conf["dbPort"]=$dbPort;
226 liveuser 569
 
3 liveuser 570
		}#if end
226 liveuser 571
 
3 liveuser 572
	#備註:
573
	#無.
574
	$backupDatabase=db::backupDatabase($conf);
575
	unset($conf);
226 liveuser 576
 
3 liveuser 577
	#如果執行失敗
578
	if($backupDatabase["status"]==="false"){
226 liveuser 579
 
3 liveuser 580
		#印出結果
581
		var_dump($backupDatabase);
226 liveuser 582
 
3 liveuser 583
		#結束執行
584
		exit;
226 liveuser 585
 
3 liveuser 586
		}#if end
226 liveuser 587
 
3 liveuser 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"],字串,要打包的檔案.
599
	$conf["target"]=$sqlFileName.".sql";
600
	#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑.
601
	$conf["fileArgu"]=__FILE__;
602
	#可省略參數:
603
	#$conf["newLineBreak"],字串,是否$conf["commentsArray"]的每個元素後面都要斷行,"false"代表不要,預設為"true"要斷行.
604
	#$conf["newLineBreak"]="false";
605
	#$conf["path"],字串,要打包的檔案的路徑,預設為當前路徑.
606
	$conf["path"]=$homePath;
607
	#$conf["delAfterSuc"],字串,是否壓縮後將原始檔案移除,預設為"false"代表不要;反之為"true"代表要.
608
	$conf["delAfterSuc"]="true";
609
	$makeZstFile=cmd::makeZstFile($conf);
610
	unset($conf);
226 liveuser 611
 
3 liveuser 612
	#如果執行失敗
613
	if($makeZstFile["status"]==="false"){
226 liveuser 614
 
3 liveuser 615
		#印出結果
616
		var_dump($makeZstFile);
226 liveuser 617
 
3 liveuser 618
		#結束執行
619
		exit;
226 liveuser 620
 
3 liveuser 621
		}#if end
226 liveuser 622
 
3 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__的內容.
642
	$conf["external::callShell"]["fileArgu"]=__FILE__;
643
	#可省略參數:
644
	#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
645
	$conf["external::callShell"]["argu"][]=$makeZstFile["storePlace"];
646
	$conf["external::callShell"]["argu"][]=$backupAddr."/";
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".
657
	$conf["external::callShell"]["escapeshellarg"]="true";
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"]);
226 liveuser 677
	unset($conf["external::callShell"]);
678
 
3 liveuser 679
	#如果執行失敗
680
	if($callShell["status"]==="false"){
226 liveuser 681
 
3 liveuser 682
		#印出結果
683
		var_dump($callShell);
226 liveuser 684
 
3 liveuser 685
		#結束執行
686
		exit;
226 liveuser 687
 
3 liveuser 688
		}#if end
226 liveuser 689
 
3 liveuser 690
	#更新最後執行的時間點
691
	$lastRunTime=time();
226 liveuser 692
 
3 liveuser 693
	#增加已經執行的次數
694
	$runTimes++;
695
 
696
	#如果有指定次數
697
	if(isset($times)){
226 liveuser 698
 
3 liveuser 699
		#如果已經達到次數的限制
700
		if($runTimes>=$times){
226 liveuser 701
 
3 liveuser 702
			#設定停止執行
703
			$continue=false;
226 liveuser 704
 
3 liveuser 705
			}#if end
226 liveuser 706
 
3 liveuser 707
		}#if end
226 liveuser 708
 
3 liveuser 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);
740
 
741
	#如果執行失敗
742
	if($ls["status"]==="false"){
226 liveuser 743
 
3 liveuser 744
		#印出結果
745
		var_dump($ls);
226 liveuser 746
 
3 liveuser 747
		#結束執行
748
		exit;
226 liveuser 749
 
3 liveuser 750
		}#if end
751
 
752
	#初始化儲存存在的備份檔案清單
753
	$backupFiles=array();
754
 
755
	#針對每個節點
756
	foreach($ls["content"] as $fileInfo){
226 liveuser 757
 
3 liveuser 758
		#如果是檔案
759
		if($fileInfo["nType"]==="-"){
226 liveuser 760
 
3 liveuser 761
			#記錄起來
762
			$backupFiles[]=$backupAddr."/".$fileInfo["name"];
226 liveuser 763
 
3 liveuser 764
			}#if end
226 liveuser 765
 
3 liveuser 766
		}#foreach end
767
 
768
	#如果節點數量大於 $backupFilesToKeep
769
	if(count($backupFiles)>$backupFilesToKeep){
226 liveuser 770
 
3 liveuser 771
		#有多少超過數量的節點就執行幾次
772
		for($i=$backupFilesToKeep;$i<count($backupFiles);$i++){
226 liveuser 773
 
3 liveuser 774
			#設置要移除的檔案
775
			$file2remove=$backupFiles[$i];
226 liveuser 776
 
3 liveuser 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)行的描述.
793
			$conf["commentsArray"]=array("已移除 ".$file2remove);
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);
226 liveuser 804
 
3 liveuser 805
			#如果執行失敗
806
			if($delFile["status"]==="false"){
226 liveuser 807
 
3 liveuser 808
				#印出結果
809
				var_dump($ls);
226 liveuser 810
 
3 liveuser 811
				#結束執行
812
				exit;
226 liveuser 813
 
3 liveuser 814
				}#if end
226 liveuser 815
 
3 liveuser 816
			}#for end
226 liveuser 817
 
3 liveuser 818
		}#if end
819
 
226 liveuser 820
	}#while end
3 liveuser 821
 
226 liveuser 822
?>