这是我备份的结构:
cron_hourly_backup
ddmmyyyy
格式命名的目录。db1_000000.zip .... db5_000000.zip
最高db1_230000.zip ... db5_230000.zip
)现在我想以编程方式删除超过1天的所有备份文件(或者,保留今天和昨天的所有备份),但每天保留一个最新的数据库(所有5个dbs)。我怎样才能做到这一点?
目前我正在这样做:
find . -type f \( -name "*_00*" \
-o -name "*_01*"-o -name "*_02*" \
-o -name "*_03*" -o -name "*_04*" \
-o -name "*_05*" -o -name "*_06*" \
-o -name "*_07*" -o -name "*_08*" \
-o -name "*_09*" -o -name "*_10*" \
-o -name "*_11*" -o -name "*_12*" \
-o -name "*_13*" -o -name "*_14*" \
-o -name "*_14*" -o -name "*_15*" \
-o -name "*_16*" -o -name "*_17*" \
-o -name "*_18*" -o -name "*_19*" \
-o -name "*_20*" -o -name "*_21*" \
-o -name "*_22*" \) -delete
这很有效,问题是
非常感谢有关如何解决上述2个问题的任何建议。
答案 0 :(得分:0)
不确定" 但每天保留一个最新的db(所有5 dbs)"手段。如果它意味着" 每天只保留最后一个(按字典顺序排列)文件",如果你有coreutils
date
实用程序,像这样的bash脚本可以工作(未经测试):
#/usr/bin/env bash
declare -a l # array of backup files
bd=cron_hourly_backup # backup dir
td=$( date +"%d%m%Y" ) # today
yd=$( date -d yesterday +"%d%m%Y" ) # yesterday
for n in "$bd"/*; do
if [ ! -d "$n" ]; then
continue # skip if not a directory
fi
if [[ "$n" == "$bd/$td" || "$n" == "$bd/$yd" ]]; then
continue # skip if today or yesterday
fi
l=( $( ls "$n" ) ) # populate array
# loop over all backup files except the last one
for (( i = 0; i < ${#l[@]} - 1; i += 1 )); do
echo "rm -f $n/${l[i]}" # comment when OK
# rm -f "$n/${l[i]}" # uncomment when OK
done
done
如果您希望将每个dbN_*
的最后一个保留为1<=N<=6
,那么您可以再使用一个循环级别(未经过测试):
#/usr/bin/env bash
declare -a l # array of backup files
bd=cron_hourly_backup # backup dir
td=$( date +"%d%m%Y" ) # today
yd=$( date -d yesterday +"%d%m%Y" ) # yesterday
for n in "$bd"/*; do
if [ ! -d "$n" ]; then
continue # skip if not a directory
fi
if [[ "$n" == "$bd/$td" || "$n" == "$bd/$yd" ]]; then
continue # skip if today or yesterday
fi
for (( j = 1; j <= 6; j += 1 )); do
l=( $( ls "$n/db${j}_"* ) ) # populate array
# loop over all backup files except the last one
for (( i = 0; i < ${#l[@]} - 1; i += 1 )); do
echo "rm -f ${l[i]}" # comment when OK
# rm -f "${l[i]}" # uncomment when OK
done
done
done