-
Notifications
You must be signed in to change notification settings - Fork 0
/
rotate.sh
161 lines (124 loc) · 2.69 KB
/
rotate.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
157
158
159
160
161
#!/bin/sh
max_backups=5
zip_backup=0
create_backup() {
if ! [ -f "$file" ]; then
printf "File '%s' not found.\n" "$file" >&2
exit 1
fi
if ! [ -s "$file" ]; then
exit 0
fi
if [ -f "$file.1" ] || [ -f "$file.1.gz" ]; then
rotate_backups
fi
if [ "$zip_backup" = 1 ]; then
gzip -c "$file" > "$file.1.gz"
touch -r "$file" "$file.1.gz"
else
cp "$file" "$file.1"
touch -r "$file" "$file.1"
fi
}
rotate_backups() {
local current_backup=$max_backups
rm "$file.$current_backup" "$file.$current_backup.gz" 2> /dev/null
while [ "$current_backup" -gt 1 ]; do
local last_backup=$(( current_backup - 1 ))
if [ -f "$file.$last_backup" ]; then
mv "$file.$last_backup" "$file.$current_backup"
fi
if [ -f "$file.$last_backup.gz" ]; then
mv "$file.$last_backup.gz" "$file.$current_backup.gz"
fi
current_backup=$(( current_backup - 1 ))
done
}
delete_backups() {
if [ -f "$file" ]; then
rm "$file".[0-9]* "$file".[0-9]*.gz 2> /dev/null
fi
}
print_help() {
help_message="Usage: rotate [OPTIONS] <FILENAME>
-b N | Set the number of backups to keep. MIN=1 DEF=5 MAX=9
-d | Delete all backups for <FILENAME>.
-h | Displays this help message.
-l | Lists <FILENAME> and all backups for <FILENAME>.
-z | GZIPs the backup of <FILENAME>."
printf "%s\n" "$help_message"
}
# Beginning of script that's always run.
option_set="def"
while getopts "b:dhlz" option; do
case $option in
b)
if printf "%s" "$option_set" | grep -Eq "^[dhl]$"; then
printf "Option -%s can't be used together with -b.\n" "$option_set" >&2
exit 1
fi
option_set="b"
if printf "%d" "$OPTARG" | grep -Eq "^[1-9]$"; then
max_backups=$OPTARG
else
printf "Invalid number %d. N must be between 1-9.\n" "$OPTARG" >&2
exit 1
fi
;;
d)
if [ $option_set != "def" ]; then
printf "Option -%s can't be used together with -d.\n" "$option_set" >&2
exit 1
fi
option_set="d"
;;
h)
if [ $option_set != "def" ]; then
printf "Option -%s can't be used together with -h.\n" "$option_set" >&2
exit 1
fi
option_set="h"
;;
l)
if [ $option_set != "def" ]; then
printf "Option -%s can't be used together with -l.\n" "$option_set" >&2
exit 1
fi
option_set="l"
;;
z)
if printf "%s" "$option_set" | grep -Eq "^[dhl]$"; then
printf "Option -%s can't be used together with -z.\n" "$option_set" >&2
exit 1
fi
option_set="z"
zip_backup=1
;;
\?)
print_help
exit 1
;;
esac
done
shift "$(( OPTIND - 1 ))"
file=$1
case $option_set in
b)
;;
d)
delete_backups
exit 0
;;
h)
print_help
exit 0
;;
l)
ls -l "$file" "$file".* 2> /dev/null
exit 0
;;
z)
;;
esac
create_backup
exit 0