-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackup.sh
More file actions
59 lines (36 loc) · 995 Bytes
/
backup.sh
File metadata and controls
59 lines (36 loc) · 995 Bytes
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
#!/bin/bash
<<readme
This is a script for backup with 5 days rotation
Usage:
./backup.sh <path to your source> <path to backup folder>
readme
function display_usage {
echo "./backup.sh <path to your source> <path to backup folder>"
}
if [$# -eq 0]; then
display_usage
fi
source_dir=$1
timestamp=$('+%Y-%m-%d-%H-%M-%S')
backup_dir=$2
function create_backup {
zip -r "${backup_dir}/backup_${timestamp}.zip" "${source_dir}" > /dev/null
if [ $? -eq 0]; then
echo "backup generated successfully for ${timestamp}"
fi
}
function perform_rotation {
backups=($(ls -t "${backup_dir}/backup_"*.zip 2>/dev/null))
# echo "${backups[@]}"
if ["${#backups[@]}" -gt 5]; then
echo "Performing rotation for 5 days"
backups_to_remove=("${backups[@]:5}")
# echo "${backups_to_remove[@]}"
for backup in "${backups_to_remove[@]}";
do
rm -f ${backup}
done
fi
}
create_backup
perform_rotation