我有一个脚本,可以按当前日期在备份文件夹中创建文件夹。这个脚本每天运行一次,每天通过cron运行。
有没有办法通过文件夹名称删除超过3天的文件夹?
之类的东西日期-3?
有效的脚本:谢谢Jo So.此脚本按日期创建文件夹。压缩文件进行备份,将其粘贴到备份目录中并清除超过3天的备份: - )
#!/bin/bash
cd /home/backups
mkdir $(date +%Y-%m-%d)
cd /opt/
tar -pczf /home/backups/$(date +%Y-%m-%d)/opt.tar.gz code
cd /var/
tar -pczf /home/backups/$(date +%Y-%m-%d)/var.tar.gz work
cd /home/backups/
threedaysago=`date -d "3 days ago" +%Y%m%d`
for backup in [0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]
do
backupdate=`echo "$backup" | tr -d -` # remove dashes
if test "$backupdate" -lt "$threedaysago"
then
rm -rf "$backup"
fi
done
答案 0 :(得分:3)
threedaysago=`date -d "3 days ago" +%Y%m%d`
for backup in [0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]
do
backupdate=`echo "$backup" | tr -d -` # remove dashes
if test "$backupdate" -lt "$threedaysago"
then
rm -rf "$backup"
fi
done
独立于mtime工作,我可以告诉你,它不会在特别奇怪的角落情况下破坏; - )
答案 1 :(得分:0)
删除超过3天的每日备份(“常规文件”类型):
rm -f `find $YOUR_BACKUP_DIR -maxdepth 1 -type f -mtime +3`
来自find
手册页:
-mtime n File's data was last modified n*24 hours ago. See the comments for -atime to understand how rounding affects the interpretation of file modification times.