编辑代码


mkdir -p /home/code/databack/data
touch /home/code/databack/data/20240205.zip
touch /home/code/databack/data/20240203.zip
touch /home/code/databack/data/20240206.zip
touch /home/code/databack/data/20240207.zip
touch /home/code/databack/data/20240208.zip
touch /home/code/databack/data/20240608.zip
touch /home/code/databack/data/20240708.zip
touch /home/code/databack/data/20240908.zip
mkdir -p /home/code/databack/data/20240216
mkdir -p /home/code/databack/data/20240218
mkdir -p /home/code/databack/data/20240219
mkdir -p /home/code/databack/data/20240619
mkdir -p /home/code/databack/data/20240719

mkdir -p /home/code/databack/data_his
touch /home/code/databack/data_his/20240205.zip
touch /home/code/databack/data_his/20240203.zip
touch /home/code/databack/data_his/20240206.zip
touch /home/code/databack/data_his/20240207.zip
touch /home/code/databack/data_his/20240208.zip
touch /home/code/databack/data_his/20240608.zip
touch /home/code/databack/data_his/20240708.zip
touch /home/code/databack/data_his/20240908.zip
mkdir -p /home/code/databack/data_his/20240216
mkdir -p /home/code/databack/data_his/20240218
mkdir -p /home/code/databack/data_his/20240219
mkdir -p /home/code/databack/data_his/20240619
mkdir -p /home/code/databack/data_his/20240719
mkdir -p /home/code/databack/data_agzx

agzx_fi=nb1
fi_ip=232
bdate=`date -d "15 days ago" +"%Y%m%d"`

clean_logs="/home/code/a.log"
# 备份动作已完成,切换至共享盘目录

clean_func(){
    path=$1
	# 列出所有目录名,筛选出非zip格式的20开头的目录名,循环读取每一个目录名
	ls -1|grep -v zip|grep ^20|awk '{print $1}'|while read folder_name 
	do
	# 判断目录名是否小于$bdate日期
	if [ "$folder_name" -le "$bdate" ];then
		# echo $folder_name
		# 对文件进行改名
		mv ${folder_name}  ${folder_name}_${agzx_fi}_${fi_ip}
		echo "于`date +"%Y-%m-%d %H:%M:%S"`:将${folder_name}文件名称修改为${folder_name}_${agzx_fi}_${fi_ip}。" >> ${clean_logs}
		# 对文件进行压缩
		echo "于`date +"%Y-%m-%d %H:%M:%S"`:${folder_name}_${agzx_fi}_${fi_ip}文件开始压缩..."
		zip -r -1 ${folder_name}_${agzx_fi}_${fi_ip}.zip ${folder_name}_${agzx_fi}_${fi_ip}
		echo "于`date +"%Y-%m-%d %H:%M:%S"`:${folder_name}_${agzx_fi}_${fi_ip}文件完成压缩,名称为:${folder_name}_${agzx_fi}_${fi_ip}.zip" >> ${clean_logs}
		# 对文件进行授权
		chmod 777 ${folder_name}_${agzx_fi}_${fi_ip}.zip
		# 删除改名后的文件
		rm -r ${path}${folder_name}_${agzx_fi}_${fi_ip}
		echo "于`date +"%Y-%m-%d %H:%M:%S"`:将${path}${folder_name}_${agzx_fi}_${fi_ip}文件删除。" >> ${clean_logs}
		# 对文件进行转移
		mv ${folder_name}_${agzx_fi}_${fi_ip}.zip /home/code/databack/data_agzx
		echo "于`date +"%Y-%m-%d %H:%M:%S"`:将${folder_name}_${agzx_fi}_${fi_ip}.zip文件转移至/home/code/databack/data_agzx目录" >> ${clean_logs}
    fi
    done
}


# local_path="/home/code/databack/data"

# if [ -z "$inpath" ];then
#     cd $local_path
#     echo "参数为空,切换至默认目录$local_path" >> ${clean_logs}
#     clean_func $local_path
# else
#     cd $inpath
#     echo "参数不为空,切换至传入目录$in_path" >> ${clean_logs}
#     clean_func $inpath
# fi


# cat /home/code/a.log
# echo '备份后的data_his路径-----------------------------------------------------'
# ls -1 /home/code/databack/data_his/
# echo '备份后的data_agzx路径-----------------------------------------------------'
# ls -1 /home/code/databack/data_agzx