forked from aruhier/easy-incremental-backups
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdelete-old-backups.sh
executable file
·156 lines (136 loc) · 4.55 KB
/
delete-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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/bin/bash
#
# Copyright (c) 2014 RUHIER Anthony
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice,
# * this list of conditions and the following disclaimer. Redistributions in
# * binary form must reproduce the above copyright notice, this list of
# * conditions and the following disclaimer in the documentation and/or other
# * materials provided with the distribution. Neither the name of the
# * copyright holder nor the names of its contributors may be used to endorse
# * or promote products derived from this software without specific prior
# * written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# Print the backups to delete
# One backup by day (except for today) for the last 30 days, before it's one
# backup by month.
# By Anthony25
SCRIPT_DIR="$( cd "$( dirname "$0" )" && pwd )"
# Change this line if the configuration file is not in the same directory that
# this script
CONFIGURATION_FILE="$SCRIPT_DIR"/easy-incremental-backups.conf
# Use the configuration file
source "$CONFIGURATION_FILE"
TARGET_ROOT="$BACKUPS_BASE_DIR"
get_day()
{
backup="$1"
day=${backup:0:2}
echo $day
}
list_months()
{
year=$1
months=$(ls "$TARGET_ROOT/$year" | grep -E ^[0-9]{1\,2}$ 2>/dev/null)
echo $months
}
list_years()
{
echo $(ls "$TARGET_ROOT" | grep -E ^[0-9]{4}$ 2>/dev/null)
}
# Not more than one by day for the month sent in parameter
nmto_by_day()
{
year=$1
month=$2
backups=$(ls "$TARGET_ROOT/$year/$month")
temp_day=""
temp_backup=""
for backup in $backups
do
day="$(get_day "$backup")"
if [ "$temp_day" != "" -a "$day" == "$temp_day" -a \
"$day" != "$(date +%d)" ]
then echo "$TARGET_ROOT/$year/$month/$temp_backup"
fi
temp_day="$day"
temp_backup="$backup"
done
}
# One backup in the month, keep only the last one
clean_all_except_last_one()
{
year=$1
month=$2
backups=$(ls -d "$TARGET_ROOT/$year/$month/"* 2>/dev/null)
backup_2_delete=$(echo "$backups" | head -n -1)
echo "$backup_2_delete"
}
# Last 30 days in the middle of the month : Last 30 days rarely are in an
# entire month. Delete all backups of the month until the one 30 days ago.
last_30_days_imo_month()
{
year=$1
month=$2
day=$3
[[ $day ]] && if (( $day > 9 ))
then last_month=`expr ${day:0:1} - 1`
echo "$(ls -d "$TARGET_ROOT/$year/$month/"[0-${last_month}][0-9]* \
2>/dev/null)"
echo "$(ls -d "$TARGET_ROOT/$year/$month/"${day:0:1}[0-${day:1:2}]* \
2>/dev/null | head -n -1)"
else
echo "$(ls -d "$TARGET_ROOT/$year/$month/"0[0-${day:1:2}]* \
2>/dev/null | head -n -1)"
fi
}
dirs_to_delete()
{
y_30da="$(date --date="30 days ago" +%Y)"
m_30da="$(date --date="30 days ago" +%m)"
d_30da="$(date --date="30 days ago" +%d)"
for year in $(list_years)
do
months=$(list_months $year)
for month in $months
do
if [ "$month" -lt "$m_30da" -o "$year" -lt "$y_30da" ]
then echo "$(clean_all_except_last_one $year $month)"
elif [ "$month" -eq "$m_30da" ]
then echo "$(last_30_days_imo_month $year $month $d_30da)"
echo "$(nmto_by_day $year $month)"
else
echo "$(nmto_by_day $year $month)"
fi
done
done
}
# Check if the configuration file exists
if [ ! -e "$CONFIGURATION_FILE" -o ! -r "$CONFIGURATION_FILE" ]
then echo -e "ERROR: Configuration file not found\n"
exit 1
fi
dirs_to_delete=$(dirs_to_delete)
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
for dir in $dirs_to_delete
do
rm -Rf "$dir"
done
IFS=$SAVEIFS