[root@localhost ~]# cat /data/sh/auto_backup_file.sh
#!/bin/bash
# 2019-12-30 10:01:09
# by author HsLM_Z
# auto backup file and directory
########################
# BackUp File
# 30 23 * * * /bin/bash /data/sh/auto_backup_file.sh /data/lwjwlkj_jar > /tmp/log/backup_log 2>&1
# 30 23 * * * /bin/bash /data/sh/auto_backup_file.sh /data/html > /tmp/log/backup_log 2>&1
BackUp_Dest="/data/backup"
BackUp_Log="/tmp/log/backup_log"
BackUp_Time=`date +%Y%m%d%H%M`
# Delete first 7 Days file or directory.
function Delete_BackUp_File(){
echo;echo -e " 33[33m#### Delete Files Name.#### 33[0m"
find ${BackUp_Dest}/${BaseName} -mtime +7 -name "$BaseName*"
find ${BackUp_Dest}/${BaseName} -mtime +7 -name "$BaseName*"|xargs rm -rf
echo -e -e " 33[33m---------------------$BackUp_Time--------------------- 33[0m"
}
# Archive file to backup directory.
function Archive(){
[ ! -d ${BackUp_Dest}/${BaseName} ];mkdir -p ${BackUp_Dest}/${BaseName}
if [ -d ${DirName}/${BaseName} ];then
cd $DirName
tar -czPf ${BackUp_Dest}/${BaseName}/${BaseName}_${BackUp_Time}_tar.gz $BaseName
echo -e " 33[32mls -lh ${BackUp_Dest}/${BaseName}/${BaseName}_${BackUp_Time}_tar.gz 33[0m"
ls -lh ${BackUp_Dest}/${BaseName}/${BaseName}_${BackUp_Time}_tar.gz
Delete_BackUp_File
else
echo "${DirName}/${BaseName} : No such file or directory";echo;exit
fi
}
function BackUP_Menu(){
if [ $# -eq 0 -o "$1" == "help" ];then
echo "Usage: { /bin/bash $0 BackUp_Source. }";echo
else
DirName=`dirname $1`
BaseName=`basename $1`
Archive
fi
}
BackUP_Menu $*
执行结果
[root@localhost ~]# /bin/bash /data/sh/auto_backup_file.sh /usr/local/nginx/
ls -lh /data/backup/nginx/nginx_202003161311_tar.gz
-rw-r--r-- 1 root root 34M Mar 16 13:11 /data/backup/nginx/nginx_202003161311_tar.gz
#### Delete Files Name.####
---------------------202003161311---------------------