Subversion Repositories qbpwcf-lib(archive)

Rev

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

Rev Author Line No. Line
779 liveuser 1
#!/bin/php
2
<?php
3
 
4
/*
5
	QBPWCF, Quick Build PHP website Component base on Fedora Linux.
6
    Copyright (C) 2015~2024 Min-Jhin,Chen
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
/*
26
說明:
27
輔助使用rsync指令
28
 
29
範例:
30
rsync.php --source ~/Downloads/ --target ~/backupOfDownloads/ 
31
*/
32
 
33
#使用命名空間qbpwcf
34
namespace qbpwcf;
35
 
953 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
 
779 liveuser 53
#以該檔案的實際位置的 lib path 為 include path 首位
953 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 
779 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
	#印出指令說明
78
	echo "Usage of ".basename(__FILE__).":".PHP_EOL; 
834 liveuser 79
	echo "--source [path to source] 代表要複製的來源.".PHP_EOL;
80
	echo "--target [path to target] 代表要複製到的目的地.".PHP_EOL;
81
	echo "--recursive [on/off] 代表是否要包含目錄底下的所有內容,預設為on.".PHP_EOL;
82
	echo "--exclude [path to excluded source] 代表要排除指定的檔案或目錄,若有多個請指定多次.".PHP_EOL;
83
	echo "--excludeFileType [file type to exclude] 代表要排除指定的檔案類型,若有多個請指定多次.".PHP_EOL;
84
	echo "--bandWidthLimit [transfer bandwidth limit] 代表要指定傳輸的頻寬上限,單為可為K(KB)、M(MB)、G(GB).".PHP_EOL;
85
	echo "--newFileOnly [on/off] 代表是否只複製來源比目標新的檔案或目錄,預設為off.".PHP_EOL;
86
	echo "--appendMode [on/off] 代表是否要從檔案尾部新增不足的部分,預設為off.".PHP_EOL;
779 liveuser 87
 
88
	#結束執行
89
	exit;
90
}
91
 
92
#函式說明:
93
#解析參數.
94
#回傳結果:
95
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
96
#$reuslt["error"],執行不正常結束的錯訊息陣列.
97
#$result["function"],當前執行的函式名稱.
98
#$result["content"],解析好的參數陣列.
99
#$result["content"][$key][$i],參數 $key 的 $i+1 個參數數值內容.
100
#$result["program"],字串,執行的程式名稱.
101
#必填參數:
102
#無
103
#可省略參數:
104
#$conf["helpFunc"],如果解析的參數不成對,則要執行的函式名稱.
105
$conf["helpFunc"]="help";
106
#備註:
107
#僅能在命令列底下執行.
108
#建議:
109
#以後可將參數 --a--b 的名稱與後面的數值 $value 存成 $result["a"]["b"][$i]=$value .
110
$parseArgu=cmd::parseArgu($conf);
111
unset($conf);
112
 
113
#如果解析參數失敗
114
if($parseArgu["status"]==="false")
115
{
116
	#印出結果
117
	var_dump($parseArgu);
118
 
119
	#結束執行
120
	exit;
121
 
122
}#if end
123
 
124
#檢查參數
125
#函式說明:
126
#檢查必填與可省略參數,可省略參數可指定預設要給與什麼數值內容。
127
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
128
#$reuslt["error"],執行不正常結束的錯訊息陣列.
129
#$result["function"],當前執行的函式名稱.
130
#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
131
#$result[$shouldBeCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
132
#$result[$shouldBeCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
133
#$result[$shouldBeCheckedVarName]["error"],每個參數設定的錯誤訊息
134
#$result["shouldNotBeEmpty"],不應該為空字串或控陣列的變數.
135
#$result["argu"],字串陣列,目前輸入的參數名稱陣列.
136
#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.
137
#$result["notNeedVar"],字串陣列,多餘的參數名稱.
138
#必填寫的參數:
139
#$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
140
$conf["varInput"]=&$parseArgu["content"];
141
#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
142
$conf["referenceVarKey"]="variableCheck::checkArguments";
143
#可以省略的參數:
144
#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,元素數量需要跟"mustBeFilledVariableType"參數的元素數量一致,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
145
$conf["mustBeFilledVariableName"]=array("source","target");
146
#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列,元素數量需要跟"mustBeFilledVariableName"參數的元素數量一致,例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double","resource","object"); , null代表不指定變數形態.
147
$conf["mustBeFilledVariableType"]=array("array","array");
148
#$conf["canBeEmptyString"],字串,必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true",可以為空字串.
149
#$conf["canBeEmptyString"]="false";
150
#$conf["canNotBeEmpty"],字串陣列,哪些必填參數的內容不得為空字串或空陣列,僅當$conf["canBeEmptyString"]為"true"時會生效.
151
#$conf["canNotBeEmpty"]=array();
152
#$conf["canBeEmpty"],字串陣列,哪些必填參數的內容可為空字串或空陣列,僅當$conf["canBeEmptyString"]為"false"時會生效.
153
#$conf["canBeEmpty"]=array();
154
#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
155
#$conf["skipableVariableCanNotBeEmpty"]=array("backTime");
156
#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
157
$conf["skipableVariableName"]=array("recursive","exclude","excludeFileType","bandWidthLimit","newFileOnly","appendMode");
158
#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
159
$conf["skipableVariableType"]=array("array","array","array","array","array","array");
160
#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
161
$conf["skipableVarDefaultValue"]=array(array("on"),null,null,null,array("off"),array("off"));
162
#$conf["disallowAllSkipableVarIsEmpty"],字串,是否允許每個可省略參數都為空字串,預設為"true"允許,反之為"false".
163
#$conf["variableCheck::checkArguments"]["disallowAllSkipableVarIsEmpty"]="false";
164
#$conf["disallowAllSkipableVarIsEmptyArray"],字串,是否允許每個可省略參數都為空陣列,預設為"true"允許,反之為"false".
165
#$conf["disallowAllSkipableVarIsEmptyArray"]="";
166
#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
167
#$conf["arrayCountEqualCheck"][]=array();
168
#參考資料來源:
169
#array_keys=>http://php.net/manual/en/function.array-keys.php
170
$checkArguments=variableCheck::checkArguments($conf);
171
unset($conf);
172
 
173
#若執行失敗
174
if($checkArguments["status"]==="false"){
175
 
176
	#印出結果
177
	var_dump($checkArguments);
178
 
179
	#結束執行
180
	exit;
181
 
182
	}#if end
183
 
184
#若檢查不通過
185
if($checkArguments["passed"]==="false"){
186
 
187
	#debug
188
	var_dump($checkArguments);
189
 
190
	#提示用法
191
	help();
192
 
193
	#結束執行
194
	exit;
195
 
196
	}#if end
197
 
198
#取得來源參數
199
$source=$parseArgu["content"]["source"][0];
200
 
201
#取得目標參數
202
$target=$parseArgu["content"]["target"][0];
203
 
204
#取得recursive參數
205
$recursive=$parseArgu["content"]["recursive"][0];
206
 
207
#如果是 "on"
208
if($recursive==="on"){
209
 
210
	#轉換為 "true"
211
	$recursive="true";
212
 
213
	}#if end
214
 
215
#反之
216
else{
217
 
218
	#轉換為 "false"
219
	$recursive="false";
220
 
221
	}#else end
222
 
223
#取得newFileOnly參數
224
$newFileOnly=$parseArgu["content"]["newFileOnly"][0];
225
 
226
#如果是 "on"
227
if($newFileOnly==="on"){
228
 
229
	#轉換為 "true"
230
	$newFileOnly="true";
231
 
232
	}#if end
233
 
234
#反之
235
else{
236
 
237
	#轉換為 "false"
238
	$newFileOnly="false";
239
 
240
	}#else end
241
 
242
#取得appendMode參數
243
$appendMode=$parseArgu["content"]["appendMode"][0];
244
 
245
#如果是 "on"
246
if($appendMode==="on"){
247
 
248
	#轉換為 "true"
249
	$appendMode="true";
250
 
251
	}#if end
252
 
253
#反之
254
else{
255
 
256
	#轉換為 "false"
257
	$appendMode="false";
258
 
259
	}#else end
260
 
261
#如果有 exclude 參數
262
if(isset($parseArgu["content"]["exclude"])){
263
 
264
	#設置 exclude 變數
265
	$exclude=$parseArgu["content"]["exclude"];
266
 
267
	}#if end
268
 
269
#如果有 excludeFileType 參數
270
if(isset($parseArgu["content"]["excludeFileType"])){
271
 
272
	#設置 excludeFileType 變數
273
	$excludeFileType=$parseArgu["content"]["excludeFileType"];
274
 
275
	}#if end
276
 
277
#如果有 bandWidthLimit 參數
278
if(isset($parseArgu["content"]["bandWidthLimit"])){
279
 
280
	#bandWidthLimit
281
	$bandWidthLimit=$parseArgu["content"]["bandWidthLimit"][0];
282
 
283
	}#if end
284
 
285
#函式說明:
286
#使用 linux 的 rsync 指令來進行備份檔案.
287
#回傳結果:
288
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
289
#$result["error"],錯誤訊息.
290
#$result["function"],當前執行的函式名稱.
291
#$result["cmd"],執行的指令.
292
#必填參數:
293
#$conf["source"],字串,需要進行備份的目錄或檔案
294
$conf["source"]=$source;
295
#$conf["target"],字串,備份到哪個地方或檔案
296
$conf["target"]=$target;
830 liveuser 297
#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑
298
$conf["fileArgu"]=__FILE__;
779 liveuser 299
#可省略參數:
300
#$conf["recursive"],字串,"true"代表目標目錄底下的內容都要複製,預設為"false".
301
$conf["recursive"]=$recursive;
302
 
303
#如果有 $exclude 變數
304
if(isset($exclude)){
305
 
306
	#$conf["exclude"],字串陣列,每個元素代表要排除的項目,預設不指定.
307
	$conf["exclude"]=$exclude;
308
 
309
	}#if end
310
 
311
#如果有 $excludeFileType 變數
312
if(isset($excludeFileType)){
313
 
314
	#$conf["excludeFileType"],字串陣列,每個元素代表要排除的副檔名,預設不指定.
315
	$conf["excludeFileType"]=$excludeFileType;
316
 
317
	}#if end
318
 
319
#如果有 $bandWidthLimit 變數
320
if(isset($bandWidthLimit)){
321
 
322
	#$conf["bandWidthLimit"],字串,代表頻寬上限,單為可為K、M、G等,預設不指定.
323
	$conf["bandWidthLimit"]=$bandWidthLimit;
324
 
325
	}#if end
326
 
327
#$conf["newFileOnly"],字串,是否只複製比較新的檔案,預設為"true"代表要;反之為"false".
328
$conf["newFileOnly"]=$newFileOnly;
329
#$conf["appendMode"],字串,是否用append的方式複製,適用於檔案內同位置內容一樣的檔案,預設值"false"代表不要;反之為"true".
330
$conf["appendMode"]=$appendMode;
331
#參考資料:
332
#無
333
#備註:
334
#無.
335
$rsync=cmd::rsync($conf);
336
unset($conf);
337
 
338
#若執行失敗
339
if($rsync["status"]==="false"){
340
 
341
	#印出結果
342
	var_dump($rsync);
343
 
344
	#結束執行
345
	exit;
346
 
347
	}#if end