归档
归档数据文件,在看正式的脚本之前首先分析,归档脚本所需要的东西,从什么地方获取归档的信息。如何遍历归档列表。将归档信息保存在何处。归档信息不存在该如何
[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命令接收多个项作为输入,那么各个项之间要怎么区分呢?在这里,
(换行符)被作为
各项的分隔符。当每个PID发送给xargs时,它将PID作为单个项来处理。又因为xargs命令被赋
给了一个变量,所以
中的反斜杠()必须再加上另一个反斜杠()进行转义。
注意,在处理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
一、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不同,它用来查看文件或目录所占用的磁盘空间的大小。
二、du常用的选项:
-h:以人类可读的方式显示
-a:显示目录占用的磁盘空间大小,还要显示其下目录和文件占用磁盘空间的大小
-s:显示目录占用的磁盘空间大小,不要显示其下子目录和文件占用的磁盘空间大小
-c:显示几个目录或文件占用的磁盘空间大小,还要统计它们的总和
--apparent-size:显示目录或文件自身的大小
-l :统计硬链接占用磁盘空间的大小
-L:统计符号链接所指向的文件占用的磁盘空间大小
一、du -h:这个就不多说了。
二、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/
/ /' |
gawk '{printf $1 ":" " " $2 " " $3 "
"}'
#
done #End of loop
#
exit