我正在编写bash备份脚本,到目前为止它运行得很好。问题是它很快就把我的硬盘搞乱了。
备份每周在星期日进行。
我想:
现在我该如何做到这一点?我想我可以想出如何“检查文件是否存在”,但我很难理解如何删除正确的备份。
备份3个月,到下个星期将是3个月和1个星期,因此将被删除。有什么好的简单的方法来解决这个问题我可能忽略了吗..?
提前谢谢你,
发布于 2015-10-29 17:51:51
如果您给备份文件一个很好的命名方案,比如: 10.29.15-BACKUP.zip,那么您总是可以很容易地做到这一点。最简单的地方,你可以只有两个单独的文件夹,一个用于每日备份和一个档案。
所以在bash脚本中:
#BACKUP PROCESS HAPPENS HERE, PLACES BACKUP NAMED 10.29.15-BACKUP.zip in /home/User/DailyBackups FOLDER, WHICH WE WILL CALL $CurrentBackup
#Get Date from 3 months ago
ChkDate=`date --date="-3 months" +%m.%d.%y`
#See if this file exists
ls $ChkDate-BACKUP.zip /home/User/BackupArchive/
#If it does exist then copy current backup to BackupArchive Folder and Remove any backups older than 367 days from the BackupArchive Folder
if [[ $? == 0 ]]; then
cp /home/User/DailyBackups/$CurrentBackup /home/User/BackupArchive/$CurrentBackup
find /home/User/BackupArchive/*-BACKUP.zip -mtime +367 -exec rm {} \
fi
#Remove all but the most recent 4 Backups
for i in `ls -t /home/User/DailyBackups/*-BACKUP.zip | tail -n +5`; do
rm "$i"
done我用367来计算366天闰年,以防你的一年备份有点过了,比如366天1分钟。
发布于 2015-10-29 15:16:20
到目前为止,我有一个类似的任务要删除文件,我必须做的是:
1. generate an interval date from todays date (like 3 months ago)
[this post has a good writeup about getting specific dates
http://stackoverflow.com/questions/11144408/convert-string-to-date-in-bash]
2. loop over all the files in the location and get their time\date stamp with
date -r <filename> +%Y
date -r <filename> +%m
date -r <filename> +%d
3. Compare file date to interval date from todays date and keep if it matches or delete if not.希望这能帮助你实现这个概念。
发布于 2015-10-29 15:22:40
假设您根据日期命名了备份:
% date +%Y-%m-%d
2015-10-29然后,您可以像这样计算一年前的日期:
% date +%Y-%m-%d -d "-1 year"
2014-10-295周前的那个日子是这样的:
% date +%Y-%m-%d -d "-5 weeks"
2015-09-24因此,您可以每3个月和每个星期日运行一次cron作业,并删除一年前和5个星期前发生的备份,如下所示:
# Every 3 months, run the backup script
1 2 * */3 * /path/to/backup/script.sh > /path/to/backupdir/$(date +%Y-%m-%d-Q)
# Every 3 months, delete the backup made on that date the previous year
1 2 * */3 * /bin/rm /path/to/backupdir/$(date +%Y-%m-%d-Q -d "-1 year")
# Every Sunday, if backup does not exist, run backup script
1 3 * * 7 if [ ! -f /path/to/backupdir/$(date +%Y-%m-%d-Q) ]; then /path/to/backup/script.sh > /path/to/backupdir/$(date +%Y-%m-%d) fi
# Every Sunday, delete backup 5 weeks old
1 3 * * 7 /bin/rm /path/to/backupdir/$(date +%Y-%m-%d -d "-5 weeks")请注意,
Q:
% date +%Y-%m-%d-Q 2015-10-29-Q
以便删除每周备份的命令,
/bin/rm /path/to/backupdir/$(日期+%Y-%m-%d -d "-5周“)
不会删除季度备份。https://stackoverflow.com/questions/33417386
复制相似问题