归档文件的脚本
需求注意的点代码需求
首先要对进行文件归档的目录,判断是否存在。如果存在,则进行文件归档,如果不存在,则标明该目录无法进行文件归档,并指出。
注意的点
1)对于tar命令,-f参数选项要放到最后,否则会如下错误
代码
存储需要进行文件归档的目录的配置文件:
cat conf_file /root/tools/root/software/root/shell-ch# 目录并不存在/root/Dir_not_exist
脚本实现如下:
#!/bin/bashDATE=$(date +%y%m%d)FILE=archive$DATE.tar.gzDESTINATION=/root/$FILE[ ! -f conf_file ] && {echo "there is no conf_file"echo "Backup not completed due to missing Configuration File"echo exit 1}FILE_NO=1confs=`cat conf_file | grep -v "^#"`for single in $confsdo#echo "$single"if [ -f $single -o -d $single ];thenFILE_LIST="$FILE_LIST $single"elseechoecho "$single,do not exist............"echo "It is on line $FILE_NO of the conf_file"echofiFILE_NO=$[$FILE_NO + 1]doneecho "FILE_LIST : $FILE_LIST"echo "Starting archive............"echotar -czf $DESTINATION $FILE_LIST 2> /dev/nullecho "Archive completed........."echo "The archive file is: $DESTINATION "echo