Rev 883 | Rev 918 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
#!/bin/php<?php/*QBPWCF, Quick Build PHP website Component base on Fedora Linux.Copyright (C) 2015~2024 Min-Jhin,ChenThis file is part of QBPWCF.QBPWCF is free software: you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation, either version 3 of the License, or(at your option) any later version.QBPWCF is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See theGNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with QBPWCF. If not, see <http://www.gnu.org/licenses/>.*//*說明:每天特定時間備份Mariadb資料庫資料到指定的位置範例: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 "./"*/#使用命名空間qbpwcfnamespace qbpwcf;#以該檔案的實際位置的 lib path 為 include path 首位exec("cd ".pathinfo(__FILE__)["dirname"]."/../lib;pwd;",$output,$status);set_include_path($output[0].PATH_SEPARATOR.get_include_path());#匯入外部套件include("allInOne.php");#說明函式function help(){#印出指令說明echo "Usage of ".basename(__FILE__).":".PHP_EOL;echo "--backTime HourHour:MinMin 代表當下為為HourHour:MinMin時要進行備份,若不設定代表立即執行.".PHP_EOL;echo "--config addr 代表設定檔的位置,裡面寫有要備份的資料庫資訊.".PHP_EOL;echo "--acctVarName name 設定檔中資料庫名稱的變數名稱.".PHP_EOL;echo "--passVarName name 設定檔中資料庫密碼的變數名稱.".PHP_EOL;echo "--dbVarName name 設定檔中資料庫名稱的變數名稱".PHP_EOL;echo "--dbAddrVarName name 設定檔中資料庫位置的變數名稱".PHP_EOL;echo "--dbPortVarName name 設定檔中資料庫port的變數名稱".PHP_EOL;echo "--backupAddr addr 備份檔案要存放的位置".PHP_EOL;echo "--times 1~N 指定要執行幾次,若有 --backTime 參數則預設為無限次;反之若無 --backTime 參數則強制為一次.".PHP_EOL;echo "--backupFilesToKeep 1~N 至少要保留幾份資料,超過數目會依照產生的日期,從舊的開始移除,直到剩下符合上限的數目,預設為7.".PHP_EOL;echo "時間到周而復始執行的範例:".PHP_EOL;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;echo "時間到只執行一次的範例:".PHP_EOL;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;echo "立即執行的範例:".PHP_EOL;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;#結束執行exit;}#function help end#函式說明:#解析參數.#回傳結果:#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.#$reuslt["error"],執行不正常結束的錯訊息陣列.#$result["function"],當前執行的函式名稱.#$result["content"],解析好的參數陣列.#$result["content"][$key][$i],參數 $key 的 $i+1 個參數數值內容.#$result["program"],字串,執行的程式名稱.#必填參數:#無#可省略參數:#$conf["helpFunc"],如果解析的參數不成對,則要執行的函式名稱.$conf["helpFunc"]="help";#備註:#僅能在命令列底下執行.#建議:#以後可將參數 --a--b 的名稱與後面的數值 $value 存成 $result["a"]["b"][$i]=$value .$parseArgu=cmd::parseArgu($conf);unset($conf);#如果解析參數失敗if($parseArgu["status"]==="false"){#印出結果var_dump($parseArgu);#結束執行exit;}#if end#檢查參數#函式說明:#檢查必填與可省略參數,可省略參數可指定預設要給與什麼數值內容。#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.#$reuslt["error"],執行不正常結束的錯訊息陣列.#$result["function"],當前執行的函式名稱.#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過#$result[$shouldBeCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在#$result[$shouldBeCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確#$result[$shouldBeCheckedVarName]["error"],每個參數設定的錯誤訊息#$result["shouldNotBeEmpty"],不應該為空字串或控陣列的變數.#$result["argu"],字串陣列,目前輸入的參數名稱陣列.#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.#$result["notNeedVar"],字串陣列,多餘的參數名稱.#必填寫的參數:#$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。$conf["varInput"]=&$parseArgu["content"];#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.$conf["referenceVarKey"]="variableCheck::checkArguments";#可以省略的參數:#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,元素數量需要跟"mustBeFilledVariableType"參數的元素數量一致,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");$conf["mustBeFilledVariableName"]=array("config","acctVarName","passVarName","dbVarName","dbAddrVarName","dbPortVarName","backupAddr");#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列,元素數量需要跟"mustBeFilledVariableName"參數的元素數量一致,例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double","resource","object"); , null代表不指定變數形態.$conf["mustBeFilledVariableType"]=array("array","array","array","array","array","array","array");#$conf["canBeEmptyString"],字串,必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true",可以為空字串.$conf["canBeEmptyString"]="false";#$conf["canNotBeEmpty"],字串陣列,哪些必填參數的內容不得為空字串或空陣列,僅當$conf["canBeEmptyString"]為"true"時會生效.#$conf["canNotBeEmpty"]=array();#$conf["canBeEmpty"],字串陣列,哪些必填參數的內容可為空字串或空陣列,僅當$conf["canBeEmptyString"]為"false"時會生效.#$conf["canBeEmpty"]=array();#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.#$conf["skipableVariableCanNotBeEmpty"]=array("backTime");#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");$conf["skipableVariableName"]=array("backTime","times","backupFilesToKeep");#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");$conf["skipableVariableType"]=array("array","array","array");#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".$conf["skipableVarDefaultValue"]=array(null,null,array(7));#$conf["disallowAllSkipableVarIsEmpty"],字串,是否允許每個可省略參數都為空字串,預設為"true"允許,反之為"false".#$conf["variableCheck::checkArguments"]["disallowAllSkipableVarIsEmpty"]="false";#$conf["disallowAllSkipableVarIsEmptyArray"],字串,是否允許每個可省略參數都為空陣列,預設為"true"允許,反之為"false".#$conf["disallowAllSkipableVarIsEmptyArray"]="";#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.#$conf["arrayCountEqualCheck"][]=array();#參考資料來源:#array_keys=>http://php.net/manual/en/function.array-keys.php$checkArguments=variableCheck::checkArguments($conf);unset($conf);#若執行失敗if($checkArguments["status"]==="false"){#印出結果var_dump($checkArguments);#結束執行exit;}#if end#若檢查不通過if($checkArguments["passed"]==="false"){#印出結果var_dump($checkArguments);#提示用法help();#結束執行exit;}#if end#取得至少要保留幾份資料,超過數目會依照產生的日期,從舊的開始移除,直到剩下符合上限的數目.$backupFilesToKeep=$parseArgu["content"]["backupFilesToKeep"][0];#取得各項參數$config=$parseArgu["content"]["config"][0];$acctVarName=$parseArgu["content"]["acctVarName"][0];$passVarName=$parseArgu["content"]["passVarName"][0];$dbVarName=$parseArgu["content"]["dbVarName"][0];$dbAddrVarName=$parseArgu["content"]["dbAddrVarName"][0];$dbPortVarName=$parseArgu["content"]["dbPortVarName"][0];$backupFilesToKeep=$parseArgu["content"]["backupFilesToKeep"][0];$backupAddr=$parseArgu["content"]["backupAddr"][0];#如果有設定 backTimeif(isset($parseArgu["content"]["backTime"])){#設置 $backTime$backTime=$parseArgu["content"]["backTime"][0];}#if end#如果有設定 timesif(isset($parseArgu["content"]["times"])){#設置 $times$times=$parseArgu["content"]["times"][0];}#if end#反之若沒有指定 backTimeelse if(!isset($backTime)){#指定只跑一次$times=1;}#else end#如果有設置 backTimeif(isset($backTime)){#函式說明:#將固定格式的字串分開,並回傳分開的結果。#回傳結果:#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.#$result["error"],錯誤訊息陣列#$result["function"],當前執行的函數名稱.#$result["argu"],使用的參數.#$result["oriStr"],要分割的原始字串內容#$result["dataArray"],爲分割好字串的陣列內容,$result["dataArray"][$i]爲第($i+1)段的內容。#$result["dataCounts"],爲總共分成幾段#$result["found"],是否有在$conf["stringIn"]找到$conf["spiltSymbol"],"true"代表有找到,"false"代表沒有找到.#必填參數:#$conf["stringIn"],字串,要處理的字串.$conf["stringIn"]=$backTime;#$conf["spiltSymbol"],字串,爲以哪個符號作爲分割.$conf["spiltSymbol"]=":";#可省略參數:#$conf["allowEmptyStr"],是否允許分割出來空字串,預設為"false"不允許;"true"代表允許.$conf["allowEmptyStr"]="false";#備註:#無.$spiltString=stringProcess::spiltString($conf);unset($conf);#如果取得小時跟分鐘失敗if($spiltString["status"]==="false"){#印出結果var_dump($spiltString);#結束執行exit;}#if end#如果取得小時跟分鐘失敗if($spiltString["found"]==="false"){#印出結果var_dump($spiltString);#結束執行exit;}#if end#取得要執行的小時$runHour=$spiltString["dataArray"][0];#取得要執行的分鐘$runMin=$spiltString["dataArray"][1];}#if end#取得至少要保留幾份資料,超過數目會依照產生的日期,從舊的開始移除,直到剩下符合上限的數目.$backupFilesToKeep=$parseArgu["content"]["backupFilesToKeep"][0];#初始化上次執行的時間點$lastRunTime=0;#初始化要繼續執行$continue=true;#初始化執行的次數$runTimes=0;#無窮迴圈while($continue){#休息一秒sleep(1);#取得當前小時$currentHour=gmdate("H");#取得當前分鐘$currentMin=gmdate("i");#如果有指定執行的時間if(isset($runHour) && isset($runMin)){#如果不是要執行的時間if($currentHour!==$runHour || $currentMin!==$runMin){#下一輪continue;}#if end}#if end#如果距離上次執行沒有過了1分鐘if( time()-$lastRunTime<60 ){#下一輪continue;}#if end#取得設定檔內容#函式說明:#解析PHP檔案裡面的變數.#回傳結果:#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.#$result["error"],錯誤訊息.#$result["function"],當前執行的函數名稱.#$result["argu"],所使用的參數.#$result["content"],找到的變數內容陣列.#$result["content"]["value"],依找到變數順序的數值.#$result["content"]["struc"],依找到變數順序的階層結構.#$result["content"]["direct"],變數名稱對應的數值內容.#必填參數:#$conf["file"],字串,檔案的路徑與名稱.$conf["file"]=$config;#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑$conf["fileArgu"]=__FILE__;#$conf["varName"],字串陣列,要搜尋的變數名稱,例如要搜尋變數$email則輸入"email".$conf["varName"]=array($acctVarName,$passVarName,$dbVarName,$dbAddrVarName,$dbPortVarName);#可省略參數:#$conf["web"],是要取得網路上的檔案則為"true";反之則為"false",預設為"false".$conf["web"]="false";#參考資料:#https://www.php.net/manual/en/function.parse-str.php#備註:#無.$parseVaraiableInPHPfile=fileAccess::parseVaraiableInPHPfile($conf);unset($conf);#如果失敗if($parseVaraiableInPHPfile["status"]==="false"){#印出結果var_dump($parseVaraiableInPHPfile);#結束執行exit;}#if end#取得資料庫帳號$dbAcct=$parseVaraiableInPHPfile["content"][$acctVarName];#取得資料庫密碼$dbPass=$parseVaraiableInPHPfile["content"][$passVarName];#取得資料庫名稱$dbName=$parseVaraiableInPHPfile["content"][$dbVarName];#取得資料位置$dbAddr=$parseVaraiableInPHPfile["content"][$dbAddrVarName];#取得資料庫port$dbPort=$parseVaraiableInPHPfile["content"][$dbPortVarName];#判斷目標目錄是否可以存取#函式說明:#開啟特定目錄,取得底下的檔案路徑清單.#回傳結果:#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.#$result["error"],錯誤訊息.#$result["function"],當前執行的函數名稱.#$result["argu"],所使用的參數.#$result["content"],讀取到的內容陣列.#必填參數:#$conf["path"],字串,要取得檔案資訊的所屬路徑.$conf["path"]=$backupAddr;#可省略參數:#無.#參考資料#無.#備註:#無.$listInfo=fileAccess::listInfo($conf);unset($conf);#如果失敗if($listInfo["status"]==="false"){#印出結果var_dump($listInfo);#結束執行exit;}#if end#產生uuid#涵式說明:#呼叫shell執行系統命令,並取得回傳的內容.#回傳的結果:#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.#$result["error"],錯誤訊息陣列.#$result["function"],當前執行的函數名稱.#$result["argu"],使用的參數.#$result["cmd"],執行的指令內容.#$result["fullCmd"],如果參數 $conf["inBackGround"] 為 "true" 則會回傳該值.#$result["output"],爲執行完二元碼後的輸出陣列,若 $conf["inBackGround"] 為 "true",則為當下的輸出.#$result["tmpFileOutput"],儲存輸出的暫存檔案名稱,若 $conf["inBackGround"] 為 "true" 則會回傳該值.#$result["running"],是否還在執行.#$result["pid"],pid.#$result["statusCode"],執行結束後的代碼.#必填的參數#$conf["command"],字串,要執行的指令與.$conf["external::callShell"]["command"]="uuid";#$conf["fileArgu"],字串,變數__FILE__的內容.$conf["external::callShell"]["fileArgu"]=__FILE__;#可省略參數:#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.#$conf["argu"]=array("");#$conf["arguIsAddr"],陣列字串,指令搭配的哪些參數為路徑,為路徑的參數會進行轉換以便符合呼叫當前函數的位置,預設不指定,若有3個參數,其中第3個參數為路徑,則表示為array("false","false","true").#$conf["arguIsAddr"]=array();#$conf["pre"],陣列,要在本指令前執行的每個指令與參數.#$conf["pre"][$i]["cmd"],字串,要在本指令前執行的第$i+1個指令.#$conf["pre"][$i]["param"],陣列字串,要在本指令前執行的第$i+1個指令的參數.#$conf["enablePrintDescription"],字串,是否要印出$conf["printDescription"]的內容,"true"代表要,"false"代表不要,預設為"false".#$conf["enablePrintDescription"]="true";#$conf["printDescription"],字串,執行該外部程式前要印出來的的文字,預設為$conf["command"]的內容加上使用的$conf["argu"]參數.#$conf["printDescription"]="";#$conf["escapeshellarg"],字串,是否要啟用過濾參數,用了比較安全,但可能會出錯,"true"為啟用,"false"為不啟用,預設為"false".$conf["external::callShell"]["escapeshellarg"]="true";#$conf["username"],字串,要用什麼使用者來執行,預設為執行php的使用者,該參數不適用於apache環境.#$conf["username"]="";#$conf["password"],字串,root的使用者密碼,預設不使用密碼,該參數不適用於apache環境.#$conf["password"]="";#$conf["useScript"],字串,是否要啟用Linux的script指令來記錄輸出,"true"代表要,Fedora的selinux會擋住該操作;"false"代表不要,預設為"false".#$conf["useScript"]="";#$conf["logFilePath"],字串,當 $conf["useScript"] 為 "true" 時,輸出的內容要暫存到哪裡,預設為 "/tmp/.qbpwcf_tmp/external/callShell/".#$conf["logFilePath"]=".qbpwcf_tmp/external/callShell/";#$conf["inBackGround"],字串,是否要在背景執行,且不會等待程式執行結束再執行下一個指令,"true"代表是,"false"代表不要,預設為"false",如果$conf["command"]有用「;」區隔的多個指令將會出錯.#$conf["inBackGround"]="";#$conf["getErr"],字串,"true"代表將錯誤輸出變成標準輸出,反之"false"為不變動.#$conf["getErr"]="false";#備註:#不是所有指令都能用apache的身份執行,目前已知java,javac指令無法執行,使用root身份可能會被selinux阻擋.#參考資料:#exec=>http://php.net/manual/en/function.exec.php#escapeshellcmd=>http://php.net/manual/en/function.escapeshellcmd.php#escapeshellarg=>http://php.net/manual/en/function.escapeshellarg.php$callShell=external::callShell($conf["external::callShell"]);unset($conf["external::callShell"]);#如果執行失敗if($callShell["status"]==="false"){#設置執行不正常$result ["status"]="false";#設置執行錯誤$result["error"]=$callShell;#回傳結果return $result;}#if end#取得uuid$uuid=$callShell["output"][0];#函式說明:#將 ~ 轉換為家目錄路徑#回傳結果:#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.#$result["error"],錯誤訊息.#$result["function"],當前執行的函數名稱.#$result["content"],轉換好的內容.#必填參數:#$conf["fileArgu"],字串,變數__FILE__的內容.$conf["fileAccess::tildeToPath"]["fileArgu"]=__FILE__;#可省略參數:#無.#參考資料:#https://www.businessweekly.com.tw/careers/Blog/14307$tildeToPath=fileAccess::tildeToPath($conf["fileAccess::tildeToPath"]);unset($conf["fileAccess::tildeToPath"]);#如果執行失敗if($tildeToPath["status"]==="false"){#設置執行不正常$result ["status"]="false";#設置執行錯誤$result["error"]=$tildeToPath;#回傳結果return $result;}#if end#取得家目錄路徑$homePath=$tildeToPath["content"];#函式說明:#備份資料庫#回傳結果::#$result["status"],"true"代表執行正常,"false"代表執行有誤.#$result["error"],錯誤訊息陣列.#$result["sqlAddress"],sql檔案的位置.#$result["function"],當前執行的函數名稱#必填參數:#$conf["backedDatabaseName"],字串.$conf["backedDatabaseName"]=$dbName;#爲要備份的資料庫名稱.#$conf["dbAddress"],字串,資料庫的位置.$conf["dbAddress"]=$dbAddr;#$conf["dbAccount"],字串,爲用於連入mysql-Server時要使用的帳號.$conf["dbAccount"]=$dbAcct;#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑$conf["fileArgu"]=__FILE__;#可省略參數:#$conf["dbPassword"],字串,爲連線到mysql-Server時要使用的密碼,可省略,若省略則代表不使用密碼$conf["dbPassword"]=$dbPass;#$conf["storePlace"],字串,要將輸出檔案儲存到哪邊,預設爲當前目錄.#$conf["storePlace"]=$backupAddr;$conf["storePlace"]=$homePath."/";#$conf["exportFileName"],字串,要輸出的檔案名稱,預設爲其export$sqlFileName=$conf["exportFileName"]=gmdate("Y-m-d_H:i:s")."_".$uuid;#如果資料庫不是位於 localhostif($conf["dbAddress"]!=="localhost"){#$conf["dbPort"],字串,爲連線到mysql-Server時要使用的port,預設為3306.$conf["dbPort"]=$dbPort;}#if end#備註:#無.$backupDatabase=db::backupDatabase($conf);unset($conf);#如果執行失敗if($backupDatabase["status"]==="false"){#印出結果var_dump($backupDatabase);#結束執行exit;}#if end#函式說明:#將資料夾打包成.zst檔案#回傳的結果:#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.#$result["error"],錯誤訊息.#$result["function"],當前執行的函式名稱.#$result["storePlace"],檔案輸出後的位置與名稱.#必填參數:#$conf["commentsArray"],字串陣列,提示輸入的文字描述,$conf["commentsArray"][$i]代表第($+1)行的描述.$conf["commentsArray"]=array("正透過zstd壓縮");#$conf["target"],字串,要打包的檔案.$conf["target"]=$sqlFileName.".sql";#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑.$conf["fileArgu"]=__FILE__;#可省略參數:#$conf["newLineBreak"],字串,是否$conf["commentsArray"]的每個元素後面都要斷行,"false"代表不要,預設為"true"要斷行.#$conf["newLineBreak"]="false";#$conf["path"],字串,要打包的檔案的路徑,預設為當前路徑.$conf["path"]=$homePath;#$conf["delAfterSuc"],字串,是否壓縮後將原始檔案移除,預設為"false"代表不要;反之為"true"代表要.$conf["delAfterSuc"]="true";$makeZstFile=cmd::makeZstFile($conf);unset($conf);#如果執行失敗if($makeZstFile["status"]==="false"){#印出結果var_dump($makeZstFile);#結束執行exit;}#if end#移動檔案到指定的位置#涵式說明:#呼叫shell執行系統命令,並取得回傳的內容.#回傳的結果:#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.#$result["error"],錯誤訊息陣列.#$result["function"],當前執行的函數名稱.#$result["argu"],使用的參數.#$result["cmd"],執行的指令內容.#$result["fullCmd"],如果參數 $conf["inBackGround"] 為 "true" 則會回傳該值.#$result["output"],爲執行完二元碼後的輸出陣列,若 $conf["inBackGround"] 為 "true",則為當下的輸出.#$result["tmpFileOutput"],儲存輸出的暫存檔案名稱,若 $conf["inBackGround"] 為 "true" 則會回傳該值.#$result["running"],是否還在執行.#$result["pid"],pid.#$result["statusCode"],執行結束後的代碼.#必填的參數#$conf["command"],字串,要執行的指令與.$conf["external::callShell"]["command"]="mv";#$conf["fileArgu"],字串,變數__FILE__的內容.$conf["external::callShell"]["fileArgu"]=__FILE__;#可省略參數:#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.$conf["external::callShell"]["argu"][]=$makeZstFile["storePlace"];$conf["external::callShell"]["argu"][]=$backupAddr."/";#$conf["arguIsAddr"],陣列字串,指令搭配的哪些參數為路徑,為路徑的參數會進行轉換以便符合呼叫當前函數的位置,預設不指定,若有3個參數,其中第3個參數為路徑,則表示為array("false","false","true").#$conf["arguIsAddr"]=array();#$conf["pre"],陣列,要在本指令前執行的每個指令與參數.#$conf["pre"][$i]["cmd"],字串,要在本指令前執行的第$i+1個指令.#$conf["pre"][$i]["param"],陣列字串,要在本指令前執行的第$i+1個指令的參數.#$conf["enablePrintDescription"],字串,是否要印出$conf["printDescription"]的內容,"true"代表要,"false"代表不要,預設為"false".#$conf["enablePrintDescription"]="true";#$conf["printDescription"],字串,執行該外部程式前要印出來的的文字,預設為$conf["command"]的內容加上使用的$conf["argu"]參數.#$conf["printDescription"]="";#$conf["escapeshellarg"],字串,是否要啟用過濾參數,用了比較安全,但可能會出錯,"true"為啟用,"false"為不啟用,預設為"false".$conf["external::callShell"]["escapeshellarg"]="true";#$conf["username"],字串,要用什麼使用者來執行,預設為執行php的使用者,該參數不適用於apache環境.#$conf["username"]="";#$conf["password"],字串,root的使用者密碼,預設不使用密碼,該參數不適用於apache環境.#$conf["password"]="";#$conf["useScript"],字串,是否要啟用Linux的script指令來記錄輸出,"true"代表要,Fedora的selinux會擋住該操作;"false"代表不要,預設為"false".#$conf["useScript"]="";#$conf["logFilePath"],字串,當 $conf["useScript"] 為 "true" 時,輸出的內容要暫存到哪裡,預設為 "/tmp/.qbpwcf_tmp/external/callShell/".#$conf["logFilePath"]=".qbpwcf_tmp/external/callShell/";#$conf["inBackGround"],字串,是否要在背景執行,且不會等待程式執行結束再執行下一個指令,"true"代表是,"false"代表不要,預設為"false",如果$conf["command"]有用「;」區隔的多個指令將會出錯.#$conf["inBackGround"]="";#$conf["getErr"],字串,"true"代表將錯誤輸出變成標準輸出,反之"false"為不變動.#$conf["getErr"]="false";#備註:#不是所有指令都能用apache的身份執行,目前已知java,javac指令無法執行,使用root身份可能會被selinux阻擋.#參考資料:#exec=>http://php.net/manual/en/function.exec.php#escapeshellcmd=>http://php.net/manual/en/function.escapeshellcmd.php#escapeshellarg=>http://php.net/manual/en/function.escapeshellarg.php$callShell=external::callShell($conf["external::callShell"]);unset($conf["external::callShell"]);#如果執行失敗if($callShell["status"]==="false"){#印出結果var_dump($callShell);#結束執行exit;}#if end#更新最後執行的時間點$lastRunTime=time();#增加已經執行的次數$runTimes++;#如果有指定次數if(isset($times)){#如果已經達到次數的限制if($runTimes>=$times){#設定停止執行$continue=false;}#if end}#if end#函式說明:#取得目錄底下的詳細資訊.#回傳結果:#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.#$result["error"],錯誤訊息.#$result["function"],當前執行的函數名稱.#$result["size"],該清單的大小,單位為bytes.#$result["dataCount"],該清單的長度.#$result["content"],指定目錄底下的所有檔案資訊.#$result["content"][$i]["nType&permission"],第$i個節點類型、權限.#$result["content"][$i]["nType"],第$i個節點類型.#$result["content"][$i]["permission"],第$i個節點權限.#$result["content"][$i]["ownByUser"],第$i個節點擁有者賬號.#$result["content"][$i]["ownByGroup"],第$i個節點擁有者群組.#$result["content"][$i]["bytes"],第$i個節點大小.#$result["content"][$i]["date"],第$i個節點最後變更日期.#$result["content"][$i]["time"],第$i個節點最後異動時間.#$result["content"][$i]["timeDetail"],第$i個節點最後異動詳細時間.#$result["content"][$i]["timezone"],第$i個節點的時區.#$result["content"][$i]["name"],第$i個節點的名稱.#必填參數:#$conf["path"],字串,要檢視的路徑.$conf["path"]=$backupAddr;#可省略參數:#無.#參考資料:#https://www.businessweekly.com.tw/careers/Blog/14307#備註:#無.$ls=fileAccess::ls($conf);unset($conf);#如果執行失敗if($ls["status"]==="false"){#印出結果var_dump($ls);#結束執行exit;}#if end#初始化儲存存在的備份檔案清單$backupFiles=array();#針對每個節點foreach($ls["content"] as $fileInfo){#如果是檔案if($fileInfo["nType"]==="-"){#記錄起來$backupFiles[]=$backupAddr."/".$fileInfo["name"];}#if end}#foreach end#如果節點數量大於 $backupFilesToKeepif(count($backupFiles)>$backupFilesToKeep){#有多少超過數量的節點就執行幾次for($i=$backupFilesToKeep;$i<count($backupFiles);$i++){#設置要移除的檔案$file2remove=$backupFiles[$i];#移除超過限制的舊資料#函式說明:#移除檔案#回傳結果:#$result["status"],"true"代表移除成功,"false"代表移除失敗.#$result["error"],錯誤訊息陣列.#$result["warning"],警告訊息陣列.#$result["function"],當前執行的函數名稱.#$result["argu"],當前函式使用的參數.#必填參數:#$conf["fileAddress"],字串,要移除檔案的位置.$conf["fileAddress"]=$file2remove;#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑.$conf["fileArgu"]=__FILE__;#可省略參數:#$conf["commentsArray"],字串陣列,提示的文字描述,$conf["commentsArray"][$i]代表第($+1)行的描述.$conf["commentsArray"]=array("已移除 ".$file2remove);#$conf["allowDelSymlink"],字串,預設為"false",不移除軟連結;"true"代表要移除軟連結.#$conf["allowDelSymlink"]="true";#$conf["allowDelFolder"],字串,預設為"false",不移除目錄;"true"代表要移除目錄.#$conf["allowDelFolder"]="true";#參考資料:#無.#備註:#無.$delFile=fileAccess::delFile($conf);unset($conf);#如果執行失敗if($delFile["status"]==="false"){#印出結果var_dump($ls);#結束執行exit;}#if end}#for end}#if end}#while end?>