歸檔數據文件,在看正式的腳本以前首先分析,歸檔腳本所須要的東西,從什麼地方獲取歸檔的信息。如何遍歷歸檔列表。將歸檔信息保存在何處。歸檔信息不存在該如何
[root@localhost gawk]# cat test5.sh #在看正事歸檔腳本前先看下這個小腳本,經過exec 獲取標準輸入,data1 是一個當前在當前目錄的文件, #!/bin/bash exec 0< data1 read line #經過read 命令獲取標準輸入的第一行信息,並將這行信息賦值給變量 line count=0 while [ $? -eq 0 ];do #經過 [ $? -eq 0 ] 比較運算判斷上一行 read line 是否獲取了信息 echo "line: $line" count=$[ $count + 1 ]
echo $count
read line #執行完前面的操做後再從新讀取文件下一行信息,而且read line 是循環中最後一行,不然會無限循環 done [root@localhost gawk]#
#總體的數據保存結構,本身能夠靈活修改
[root@localhost /]# tree archive/ archive/ ├── 04 │ └── 23 │ ├── archive2019-04-23-16:29:34.tar.gz │ ├── archive2019-04-23-16-32-04.tar.gz │ └── usr │ └── local │ └── src ├── archive190423.tar.gz ├── archive2019-04-23 ├── archive2019-04-23:16:15:47.tar.gz ├── archive2019-04-23-16:16:07.tar.gz ├── Archive.sh └── File_To_Backup 5 directories, 8 files [root@localhost /]#
[root@localhost archive]# cat Archive.sh #!/bin/bash # # #Archive - Archive designated files & directories ######################################################## # # # Gather current data 收集當前日期 # #DATE=$(date +%y%m%d) DATE=$(date +%Y-%m-%d\-%H-%M-%S) # # #設置存檔文件名稱 # FILE=archive$DATE.tar.gz # #設置配置文件和存檔文件 # CONFIG_FILE=/archive/File_To_Backup #DESTINATION=/archive/$FILE ################################################################################# #每次執行腳本建立一個新的目錄來存放歸檔文件 BASEDEST=/archive #獲取當前時間 DAY=$(date +%d) MONTH=$(date +%m) TIME=$(date +%k%M) # #建立目錄 mkdir -p $BASEDEST/$MONTH/$DAY DESTINATION=$BASEDEST/$MONTH/$DAY/$FILE # ########## Main Script ################## # #檢查存檔配置文件是否存在 # if [ -f $CONFIG_FILE ];then echo else echo echo "$CONFIG_FILE does not exist." echo "Backup not completed due to missing configuration file" echo exit #若是不存在退出腳本執行 fi # # # 構建要備份的文件名稱 # FILE_NO=1 #備份文件計數,從1 開始 exec < $CONFIG_FILE #經過exec 方式讀取要備份的文件目錄 # read FILE_NAME #讀取第一行目錄信息,並賦值給變量FILE_NAME # while [ $? -eq 0 ];do #$? 獲取上個命令執行結果,若是read 可以獲取值爲成功,不然失敗 if [ -f $FILE_NAME -o -d $FILE_NAME ];then #判斷file_name 是否存在,-f 文件,-d 目錄 ,-o 或 #若是文件存在,將文件名稱添加到文件列表 FILE_LIST="$FILE_LIST $FILE_NAME" else echo echo echo "$FILE_NAME doesn't exist." echo echo fi FILE_NO=$[$FILE_NO + 1] #編號增長一行 read FILE_NAME #讀取下一行數據 done # ##################################################################### # #備份壓縮文件 # echo echo "Starting archive....." echo # tar -czf $DESTINATION $FILE_LIST 2> /dev/null # echo "Archive completed" echo "Resulting archive file is: $DESTINATION" echo # exit
刪除帳戶在管理帳戶工做中比較複雜。在刪除帳戶時,至少須要4個步驟: (1) 得到正確的待刪除用戶帳戶名; (2) 殺死正在系統上運行的屬於該帳戶的進程; (3) 確認系統中屬於該帳戶的全部文件; (4) 刪除該用戶帳戶。
#!/bin/bash # #Delete_User - Automates the 4 steps to remove an account # 在這個腳本中輸入就調用get_answer 函數,判斷就調用process_answer 函數,很精巧 ############################################################### # Define Functions # ##################################################### function get_answer { #調用get_answer 函數,用戶輸入和確認刪除輸入都調用這個函數,很重要 # unset ANSWER #首先unset ANSWER 變量 ASK_COUNT=0 #查詢計數歸0 # while [ -z "$ANSWER" ] #While no answer is given, keep asking. 第一次執行的時候。ANSWER 爲空,ASK_COUNT 計數+1 do ASK_COUNT=$[ $ASK_COUNT + 1 ] #第一次執行的時候。ANSWER 爲空,ASK_COUNT 計數+1,所以在case 判斷中不會執行,沒有1 的匹配條件 # case $ASK_COUNT in #If user gives no answer in time allotted #當用戶一直不進行輸入時每60s ASK_COUNT 就會加1,會提示兩次,第四次退出腳本執行 2) echo echo "Please answer the question." echo ;; 3) echo echo "One last try...please answer the question." echo ;; 4) echo echo "Since you refuse to answer the question..." echo "exiting program." echo # exit ;; esac # echo # if [ -n "$LINE2" ] #輸出提示符 then #Print 2 lines echo $LINE1 echo -e $LINE2" \c" else #Print 1 line echo -e $LINE1" \c" fi # # Allow 60 seconds to answer before time-out read -t 60 ANSWER #讀取命令行寫入的字段 done # Do a little variable clean-up unset LINE1 unset LINE2 # } #End of get_answer function # ##################################################### function process_answer { #ANSWER 判斷 # case $ANSWER in #判斷從系統刪除,若是是跳出判斷,若是不是輸出提示,unset 變量, y|Y|YES|yes|Yes|yEs|yeS|YEs|yES ) # If user answers "yes", do nothing. ;; *) # If user answers anything but "yes", exit script echo echo $EXIT_LINE1 echo $EXIT_LINE2 echo exit ;; esac # # Do a little variable clean-up # unset EXIT_LINE1 unset EXIT_LINE2 # } #End of process_answer function # ############################################## # End of Function Definitions # ############# Main Script #################### # Get name of User Account to check # 確認要刪除的用戶名稱 echo "Step #1 - Determine User Account name to Delete " echo " 確認要刪除的用戶名稱" echo LINE1="Please enter the username of the user " #輸出提示信息 LINE2="account you wish to delete from system:" #輸出提示信息,在get_answer 函數中read 讀取字符輸入 get_answer #調用函數,隨着鏡頭咱們將視線轉向get_answer 函數 USER_ACCOUNT=$ANSWER #獲取用戶輸入的用戶名 # # Double check with script user that this is the correct User Account # LINE1="Is $USER_ACCOUNT the user account " # LINE2="you wish to delete from the system? [y/n]" get_answer #調用get_answer 函數,用戶確認信息輸入,只有不爲空,就能夠這裏不判斷輸入內容 # # Call process_answer funtion: # if user answers anything but "yes", exit script # EXIT_LINE1="Because the account, $USER_ACCOUNT, is not " #定義兩個提示語句 EXIT_LINE2="the one you wish to delete, we are leaving the script..." process_answer #調用process 函數 # ################################################################ ################################################################ # Check that USER_ACCOUNT is really an account on the system #檢查user_account 是否是系統上的帳戶 # USER_ACCOUNT_RECORD=$(cat /etc/passwd | grep -w $USER_ACCOUNT) #過濾 # if [ $? -eq 1 ] # If the account is not found, exit script 判斷上一個命令是不成功推出 then echo echo "Account, $USER_ACCOUNT, not found. " echo "Leaving the script..." echo exit fi # echo echo "I found this record:" echo $USER_ACCOUNT_RECORD # LINE1="Is this the correct User Account? [y/n]" get_answer #函數提示用戶輸入確認值 # # # Call process_answer function: # if user answers anything but "yes", exit script # EXIT_LINE1="Because the account, $USER_ACCOUNT, is not " EXIT_LINE2="the one you wish to delete, we are leaving the script..." process_answer #process_answer 函數判斷響應的值是yes 仍是其餘 # ################################################################## # Search for any running processes that belong to the User Account # echo echo "Step #2 - Find process on system belonging to user account" echo # ps -u $USER_ACCOUNT >/dev/null #Are user processes running? # case $? in 1) # No processes running for this User Account # echo "There are no processes for this account currently running." echo ;; 0) # Processes running for this User Account. 若是當前系統有指定用戶的進程 # Ask Script User if wants us to kill the processes. # echo "$USER_ACCOUNT has the following processes running: " echo ps -u $USER_ACCOUNT # LINE1="Would you like me to kill the process(es)? [y/n]" get_answer #使用get_answer 函數獲取值 # case $ANSWER in #case 語句判斷值 y|Y|YES|yes|Yes|yEs|yeS|YEs|yES ) # If user answers "yes", # kill User Account processes. # echo echo "Killing off process(es)..." # # List user processes running code in variable, COMMAND_1 COMMAND_1="ps -u $USER_ACCOUNT --no-heading" # # Create command to kill proccess in variable, COMMAND_3 COMMAND_3="xargs -d \\n /usr/bin/sudo /bin/kill -9" # ############################################################## :<<! xargs命令被保存在變量COMMAND_3中。選項-d指明使用什麼樣的分隔符。換句話說,既然 xargs命令接收多個項做爲輸入,那麼各個項之間要怎麼區分呢?在這裏,\n(換行符)被做爲 各項的分隔符。當每一個PID發送給xargs時,它將PID做爲單個項來處理。又由於xargs命令被賦 給了一個變量,因此\n中的反斜槓(\)必須再加上另外一個反斜槓(\)進行轉義。 注意,在處理PID時,xargs命令須要使用命令的完整路徑名。sudo命令和kill命令 (用於殺死用戶帳戶的運行進程。另外還注意到kill命令使用了信號-9。 這三條命令經過管道串聯在了一塊兒。ps命令生成了處於運行狀態的用戶進程列表,其中包括 每一個進程的PID。gawk命令將ps命令的標準輸出(STDOUT)做爲本身的STDIN,而後從中只提 取出PID(參見第15章)。xargs命令將gawk命令生成的每一個PID做爲STDIN,建立並執行kill 命令,殺死用戶全部的運行進程。這個命令管道以下。 ! ################################################################### # Kill processes via piping commands together $COMMAND_1 | gawk '{print $1}' | $COMMAND_3 #ps 用戶進程,獲取PID ,kill # echo echo "Process(es) killed." ;; *) # If user answers anything but "yes", do not kill. echo echo "Will not kill the process(es)" echo ;; esac ;; esac ################################################################# # Create a report of all files owned by User Account # echo echo "Step #3 - Find files on system belonging to user account" echo echo "Creating a report of all files owned by $USER_ACCOUNT." echo echo "It is recommended that you backup/archive these files," echo "and then do one of two things:" echo " 1) Delete the files" echo " 2) Change the files' ownership to a current user account." echo echo "Please wait. This may take a while..." # REPORT_DATE=$(date +%y%m%d) REPORT_FILE=$USER_ACCOUNT"_Files_"$REPORT_DATE".rpt" # find / -user $USER_ACCOUNT > $REPORT_FILE 2>/dev/null # echo echo "Report is complete." echo "Name of report: $REPORT_FILE" echo "Location of report: $(pwd)" echo #################################### # Remove User Account 刪除用戶 echo echo "Step #4 - Remove user account" echo # LINE1="Remove $USER_ACCOUNT's account from system? [y/n]" get_answer # # Call process_answer function: # if user answers anything but "yes", exit script # EXIT_LINE1="Since you do not wish to remove the user account," EXIT_LINE2="$USER_ACCOUNT at this time, exiting the script..." process_answer #process_answer 函數判斷回答是否是yes ,是就繼續進行不是就退出腳本執行 # userdel $USER_ACCOUNT #delete user account echo echo "User account, $USER_ACCOUNT, has been removed" echo # exit
du命令用來查看目錄或文件所佔用磁盤空間的大小。經常使用選項組合爲:du -sh 1、du的功能:`du` reports the amount of disk space used by the specified files and for each subdirectory (of directory arguments). with no arguments,`du` reports the disk space for the current directory。 很明顯,與df不一樣,它用來查看文件或目錄所佔用的磁盤空間的大小。 2、du經常使用的選項: -h:以人類可讀的方式顯示 -a:顯示目錄佔用的磁盤空間大小,還要顯示其下目錄和文件佔用磁盤空間的大小 -s:顯示目錄佔用的磁盤空間大小,不要顯示其下子目錄和文件佔用的磁盤空間大小 -c:顯示幾個目錄或文件佔用的磁盤空間大小,還要統計它們的總和 --apparent-size:顯示目錄或文件自身的大小 -l :統計硬連接佔用磁盤空間的大小 -L:統計符號連接所指向的文件佔用的磁盤空間大小 1、du -h:這個就很少說了。 2、du -a:使用此選項時,顯示目錄和目錄下子目錄和文件佔用磁盤空間的大小。
#!/bin/bash # # Big_Users - Find big disk space users in various directories ############################################################### # Parameters for Script # CHECK_DIRECTORIES=" /var/log /home" #Directories to check # ############## Main Script ################################# # DATE=$(date '+%m%d%y') #Date for report file # exec > disk_space_$DATE.rpt #Make report file STDOUT # echo "Top Ten Disk Space Usage" #Report header echo "for $CHECK_DIRECTORIES Directories" # for DIR_CHECK in $CHECK_DIRECTORIES #Loop to du directories do echo "" echo "The $DIR_CHECK Directory:" #Directory header # # Create a listing of top ten disk space users in this dir du -S $DIR_CHECK 2>/dev/null | sort -rn | sed '{11,$D; =}' | sed 'N; s/\n/ /' | gawk '{printf $1 ":" "\t" $2 "\t" $3 "\n"}' # done #End of loop # exit