-
Notifications
You must be signed in to change notification settings - Fork 1
/
remove_old_backups.sh
134 lines (113 loc) · 3.12 KB
/
remove_old_backups.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#! /bin/bash
#
# Removes old / dupliacte backups from remote.
IFS=', ' read -r -a backups <<< "$(findBackups $efi_backup_dir)"; unset IFS
current_time=$(date +%s)
backups_age=()
backups_md5=()
for backup in "${backups[@]}"; do
eval $(stat -s "$backup")
age=$(expr $current_time - $st_ctime)
md5=$(echo ${backup##*_}| cut -d"." -f1)
backups_age+=("$age")
backups_md5+=("$md5")
done
getBackupAge() {
for ((i=0;i<"${#backups[@]}";i++)); do
if [[ "$1" == "${backups[$i]}" ]]; then
echo "${backups_age[$i]}"
return
fi
done
}
getBackupMD5() {
for ((i=0;i<"${#backups[@]}";i++)); do
if [[ "$1" == "${backups[$i]}" ]]; then
echo "${backups_md5[$i]}"
return
fi
done
}
# Filter
b_this_hour=()
b_today=()
b_yesterday=()
b_this_week=()
b_this_month=()
b_last_month=()
b_this_year=()
b_older=()
for ((i=0;i<"${#backups[@]}";i++)); do
backup="${backups[$i]}"
md5="${backups_md5[$i]}"
age="${backups_age[$i]}"
# Remove duplicate files with same MD5
if ! elementIn "$md5" "${__md5_set[@]}"; then
__md5_set+=("$md5")
else
backups_to_remove+=("${backups[$i]}")
continue
fi
# Age
if [[ "$age" -gt "31556736" ]]; then
b_older+=("$backup")
elif [[ "$age" -gt "5259456" ]]; then
b_this_year+=("$backup")
elif [[ "$age" -gt "2629728" ]]; then
b_last_month+=("$backup")
elif [[ "$age" -gt "604000" ]]; then
b_this_month+=("$backup")
elif [[ "$age" -gt "172800" ]]; then
b_this_week+=("$backup")
elif [[ "$age" -gt "86400" ]]; then
b_yesterday+=("$backup")
elif [[ "$age" -gt "3600" ]]; then
b_today+=("$backup")
else
b_this_hour+=("$backup")
fi
done
# echo "TH: ${b_this_hour[@]}"
# echo "T: ${b_today[@]}"
# echo "Y: ${b_yesterday[@]}"
# echo "TW: ${b_this_week[@]}"
# echo "TM: ${b_this_month[@]}"
# echo "LM: ${b_last_month[@]}"
# echo "TY: ${b_this_year[@]}"
# echo "O: ${b_older[@]}"
# Choose which backups to remove
keep_count="$(( ${#b_this_hour[@]} + ${#b_today[@]} ))" # Keeps all backups made this hour and day
for array_name in b_yesterday b_this_week b_this_month; do # Keeps at least one backup made yesterday, this week and this month
IFS=', ' read -r -a array <<< "$(eval "echo \${$array_name[@]}")"; unset IFS
keep_count="$(( $keep_count + $(minimum ${#array[@]} 1) ))"
done
for array_name in b_yesterday b_this_week b_this_month; do
IFS=', ' read -r -a array <<< "$(eval "echo \${$array_name[@]}")"; unset IFS
for backup in "${array[@]:1}"; do
if [[ "$keep_count" -lt "$KEEP_OLD_BACKUPS" ]]; then
keep_count="$(( $keep_count + 1 ))"
else
backups_to_remove+=("$backup")
fi
done
done
for array_name in b_last_month b_this_year b_older; do
IFS=', ' read -r -a array <<< "$(eval "echo \${$array_name[@]}")"; unset IFS
keep_count="$(( $keep_count + $(minimum ${#array[@]} 1) ))"
for backup in "${array[@]}"; do
if [[ "$keep_count" -lt "$KEEP_OLD_BACKUPS" ]]; then
keep_count="$(( $keep_count + 1 ))"
else
backups_to_remove+=("$backup")
fi
done
done
if [[ "$DRY_RUN" != true ]]; then
# Delete Backups
for backup in "${backups_to_remove[@]}"; do
rm "$backup"
done
echo "Did delete backups: ${backups_to_remove[@]}"
else
echo "Would delete backups: ${backups_to_remove[@]}"
fi