Skip to content

Commit af8d11c

Browse files
committedJun 15, 2024
chore: Add option to keep a specified number of backups
1 parent 9e4044d commit af8d11c

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed
 

‎README.md

+2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ user:~$ ./backup.sh -h
2929
-b Backup directory
3030
-v Verify backup after creation
3131
-l [PATH/NAME] Log file to write the output (default: backup.log)
32+
-k [NUMBER] Number of backups to keep (latest backups)
3233

3334
Restore options:
3435
-b Backup directory
@@ -38,6 +39,7 @@ user:~$ ./backup.sh -h
3839
Examples:
3940
./backup.sh -c # Create backup in the default directory
4041
./backup.sh -c -v # Verify backup after creation
42+
./backup.sh -c -k 3 -v # Keep the latest 3 backups and verify
4143
./backup.sh -c -s /path/to/source # Create backup of a specific directory
4244
./backup.sh -c -b /path/to/backup # Create backup in a specific directory
4345
./backup.sh -c -s /path/to/source -b /path/to/backup

‎backup.sh

+37-2
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ compare_partition_space() {
103103
# Help function
104104
help() {
105105
cat << EOF
106-
$0 - Backup script to create and restore backups [version: 1.0.0]
106+
$0 - Backup script to create and restore backups [version: 1.1.0]
107107
108108
$0 [MODE][OPTIONS]
109109
@@ -118,6 +118,7 @@ $0 [MODE][OPTIONS]
118118
-b Backup directory
119119
-v Verify backup after creation
120120
-l [PATH/NAME] Log file to write the output (default: backup.log)
121+
-k [NUMBER] Number of backups to keep (latest backups)
121122
122123
Restore options:
123124
-b Backup directory
@@ -127,6 +128,7 @@ $0 [MODE][OPTIONS]
127128
Examples:
128129
$0 -c # Create backup in the default directory
129130
$0 -c -v # Verify backup after creation
131+
$0 -c -k 3 -v # Keep the latest 3 backups and verify
130132
$0 -c -s /path/to/source # Create backup of a specific directory
131133
$0 -c -b /path/to/backup # Create backup in a specific directory
132134
$0 -c -s /path/to/source -b /path/to/backup
@@ -191,6 +193,31 @@ verify_backup() {
191193
fi
192194
}
193195

196+
# Function to remove old backup archives
197+
remove_old_backups() {
198+
# Number of backups to keep
199+
local keep=$1
200+
201+
if [ ! -d "$BACKUP_DIR" ]; then
202+
logger "ERROR: Backup directory $BACKUP_DIR does not exist." >&2
203+
exit 1
204+
fi
205+
206+
# Get a list of all backup files, sorted by date
207+
mapfile -t backups < <(find "$BACKUP_DIR" -name "${BACKUP_PREFIX}*.tar.gz" -exec basename {} \; | sort -r)
208+
209+
if [ ${#backups[@]} -le "$keep" ]; then
210+
logger "INFO: No old backup files to remove." >&2
211+
return 0
212+
fi
213+
214+
# Remove all backup files that are older than the specified number
215+
for (( i=keep; i<${#backups[@]}; i++ )); do
216+
rm "$BACKUP_DIR/${backups[$i]}"
217+
logger "INFO: Removed old backup ${backups[$i]}"
218+
done
219+
}
220+
194221
###############################################################################
195222
# RESTORE BACKUP FUNCTIONS #
196223
###############################################################################
@@ -272,8 +299,9 @@ main() {
272299
;;
273300
-c|--create)
274301
VERIFY=false
302+
KEEP=0
275303
shift
276-
while getopts ":s:b:vl:" opt; do
304+
while getopts ":s:b:vl:k:" opt; do
277305
case ${opt} in
278306
s )
279307
SOURCE_DIR=$OPTARG
@@ -287,6 +315,9 @@ main() {
287315
l )
288316
LOG_FILE=$OPTARG
289317
;;
318+
k )
319+
KEEP=$OPTARG
320+
;;
290321
\? )
291322
error "Invalid option: -$OPTARG"
292323
;;
@@ -303,6 +334,10 @@ main() {
303334
if [ "$VERIFY" = true ]; then
304335
verify_backup
305336
fi
337+
# If the keep option was specified, remove old backups
338+
if [ "$KEEP" -gt 0 ]; then
339+
remove_old_backups "$KEEP"
340+
fi
306341
logger "Done!"
307342
exit 0
308343
;;

0 commit comments

Comments
 (0)