-
Notifications
You must be signed in to change notification settings - Fork 1
/
Backup.sh-20201112
69 lines (56 loc) · 2.01 KB
/
Backup.sh-20201112
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
#!/bin/bash
# Daily backup of selected directories
# Variables
LOG_LOC="/var/log/mybackup.log"
## Functions start
function check_dir_local {
# Check for dir list file
if [ ! -s "/backup_dirs.conf" ]
then
echo "Please create a list of directories to backup by creating a backup_dir.conf file in the root directory"
exit 1
fi
}
function check_backup_local {
if [ ! -s "/backup_local.conf" ]
then
echo "Please specify the full path of where to send the backup to, by creating the backup_local.conf file in the root directory"
exit 1
fi
}
function check_schedule {
# Check to see if the script exists in daily cron directory
if [ ! -s "/etc/cron.daily/make_backup" ]
then
# Copy script to cron.daily dir
sudo cp make_backup.sh /etc/cron.daily/make_backup #Why sudo? The Cron directory is owned by root.
echo "The backup has been set to run daily"
echo "The exact run time is in the /etc/crontab file."
exit 1
fi
}
function perform_backup {
# Get backup location
backup_path=$(cat /backup_local.conf)
echo "starting backup. . ." > $LOG_LOC
# For each dir, archive and compress to backup location
while read -r dir_path
do
# Get backup dir name
dir_name=$(basename "$dir_path")
# Create filename for compressed backup
filename=$backup_path$dir_name.tar.gz
# Archive dirs and compress archive
tar -zcf "$filename" "$dir_path" | openssl enc -e -aes256 -out backup_secured.tar.gz 2>> $LOG_LOC
# Change ownership of backup files
chown root:root "$filename"
echo "Backing up of $dir_name completed." >> $LOG_LOC
done < /backup_dirs.conf
echo "Backup complete at:" >> $LOG_LOC
date >> $LOG_LOC
}
## End of functions
check_dir_loc
.check_backup_loc
check_schedule
perform_backup