Skip to content

Commit c4a8e1b

Browse files
committed
added backup and rotation scripts
1 parent 44fd95a commit c4a8e1b

1 file changed

Lines changed: 59 additions & 0 deletions

File tree

backup.sh

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#!/bin/bash
2+
3+
4+
<<readme
5+
This is a script for backup with 5 days rotation
6+
7+
8+
Usage:
9+
./backup.sh <path to your source> <path to backup folder>
10+
readme
11+
12+
13+
function display_usage {
14+
echo "./backup.sh <path to your source> <path to backup folder>"
15+
16+
}
17+
18+
if [$# -eq 0]; then
19+
display_usage
20+
21+
fi
22+
23+
source_dir=$1
24+
timestamp=$('+%Y-%m-%d-%H-%M-%S')
25+
backup_dir=$2
26+
27+
function create_backup {
28+
29+
zip -r "${backup_dir}/backup_${timestamp}.zip" "${source_dir}" > /dev/null
30+
31+
if [ $? -eq 0]; then
32+
echo "backup generated successfully for ${timestamp}"
33+
fi
34+
}
35+
36+
function perform_rotation {
37+
backups=($(ls -t "${backup_dir}/backup_"*.zip 2>/dev/null))
38+
# echo "${backups[@]}"
39+
40+
if ["${#backups[@]}" -gt 5]; then
41+
echo "Performing rotation for 5 days"
42+
43+
backups_to_remove=("${backups[@]:5}")
44+
45+
# echo "${backups_to_remove[@]}"
46+
47+
for backup in "${backups_to_remove[@]}";
48+
do
49+
rm -f ${backup}
50+
done
51+
52+
fi
53+
}
54+
55+
create_backup
56+
perform_rotation
57+
58+
59+

0 commit comments

Comments
 (0)