-
Notifications
You must be signed in to change notification settings - Fork 0
/
backupScript.sh
66 lines (59 loc) · 1.69 KB
/
backupScript.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
#!/usr/bin/sh
backup_dir="/home/kali/Desktop/scripting"
backup_file="backup_$(date +"%Y-%m-%d_%H-%M-%S").tar.gz"
files_to_backup=""
compression="yes"
retain_days=7
# user manual
usage() {
echo "Usage: $0 [OPTIONS] [FILES...]"
echo "./backup_script.sh -d /path/to/backup -n my_backup.tar.gz /path/to/directory1 /path/to/directory2"
echo "Options:"
echo " -d, --directory DIR Specify backup directory (default: /path/to/backup)"
echo " -n, --name NAME Specify backup file name (default: backup_YYYY-MM-DD_HH-MM-SS.tar.gz)"
echo " -c, --compress Enable compression (default: yes)"
echo " -r, --retain DAYS Number of days to retain backups (default: 7)"
echo " -h, --help Display this help message"
exit 1
}
while [ $# -gt 0 ]; do
case $1 in
-d | --directory)
backup_dir="$2"
shift 2
;;
-n | --name)
backup_file="$2"
shift 2
;;
-c | --compress)
compression="yes"
shift
;;
-r | --retain)
retain_days="$2"
shift 2
;;
-h | --help)
usage
;;
*)
files_to_backup="$files_to_backup $1"
shift
;;
esac
done
# CHECK THE DIRECTORY, IF NOT AVAILABLE, CREATE ONE
if [ ! -d "$backup_dir" ]
then
mkdir -p $backup_dir
fi
# Backup files/directories
echo "Backing up: $files_to_backup"
if [ "$compression" == "yes" ]; then
tar -czf "$backup_dir/$backup_file" $files_to_backup
else
tar -cf "$backup_dir/$backup_file" $files_to_backup
fi
echo "Backup saved to: $backup_dir/$backup_file"
exit 0