-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackup.sh
executable file
·149 lines (128 loc) · 4.34 KB
/
backup.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/bin/bash
################# PARAMETERS ################
# WARNING! This script has not been fully tested, so keep an eye on it.
# source and destination usually is something like remote:path instead of the below local paths
tsource="$1"
dest="$2"
date_for_backup="$3" #two digit number ex. 01 for the first in each month to run the script
del_after="$4" # Will will delete everything older than x days in bckp ex. 30
keep_mnt="$5" # Keep these backup months in the old_dir ex. 01 or 01,04,07,10(comma seperated)
del_all_after="$6" # Will delete everything older than x days in old_dir ex. 365
job_name="$7"
option="$8" # optinal rclone hooks ex. --dry-run
email="$9" # To send allerts
################# SET VARS ##############
# Be carefull to double check the whole script if you change the below variables
bckp="month_backup" # dir to stor monthly backups
old_dir="old_backups" # dir where to retain the old backups
path="$PWD"
log_file="${path}/all_backup_logs.log"
log_option="--log-file=$log_file" # rclone log to log_file
################# FUNCTIONS #################
send_to_log()
{
msg="$1"
# set log - send msg to log
echo "$msg" >> "$log_file" #log msg to log_file
#printf "$msg" | systemd-cat -t RCLONE_JOBBER -p info #log msg to systemd journal
}
send_mail()
{
if [[ ! -z "$email" ]];then
msg="$1"
/usr/sbin/sendmail -i -- $email <<EOF
Subject: Backup Urgency - $job_name
$msg
EOF
fi
}
print_message()
{
urgency="$1"
msg="$2"
message="${urgency}: $job_name $msg"
echo "$message"
send_to_log "$(date +%F_%T) $message"
send_mail "$msg"
}
conf_logging()
{
exit_code="$1"
if [ "$exit_code" -eq 0 ]; then #if no errors
confirmation="$(date +%F_%T) completed $bckp"
echo "$confirmation"
send_to_log "$confirmation"
send_to_log ""
else
print_message "ERROR" "failed. rclone exit_code=$exit_code"
send_to_log ""
exit 1
fi
}
delete_dir() {
dir="$1"
archive="$2"
if [ $archive == "old" ];then
cmd_delete="rclone purge $dest/$old_dir/$dir $log_option $option" # you might want to dry-run this.
else
cmd_delete="rclone purge $dest/$bckp/$dir $log_option $option" # you might want to dry-run this.
fi
echo "Removing old archive backups $dir $job_name"
echo "$cmd_delete"
eval $cmd_delete
exit_code=$?
if ! [ $exit_code == 3 ]; then # We don't want any alerts on 3 (no directories found)
conf_logging "$exit_code"
fi
}
################# SCRIPT ################
log_option="--log-file=$log_file"
ifStart=`date '+%d'`
month=`date '+%m'`
timestamp="$(date +%F_%H%M%S)"
# Split the comma-separated list into an array
# IFS=',' read -ra backup_dates <<< "$date_for_backup"
# Iterate over the array and compare each value with ifStart
#for backup_date in "${backup_dates[@]}"; do
# if [[ $ifStart == $backup_date ]]; then
# if [ $ifStart == $date_for_backup ]; then
if echo "$date_for_backup" | grep -q "$ifStart"; then
if echo "$keep_mnt" | grep -q "$month"; then
cmd="rclone copy $tsource $dest/$old_dir/$timestamp $log_option $option"
echo "$cmd"
eval $cmd
exit_code=$?
conf_logging "$exit_code"
CMD_LSD="rclone lsd --max-depth 1 $dest/$old_dir/"
mapfile -t dir_array < <(eval $CMD_LSD)
DATE=$(date -d "$now - $del_all_after days" +'%Y-%m-%d')
for i in "${!dir_array[@]}"; do
dir_path="${dir_array[i]}"
dir_date=$(echo ${dir_path##* })
dir_date2=$(echo ${dir_date%_*})
conv_date=$(date -d "$dir_date2" +'%Y-%m-%d')
if [[ $conv_date < $DATE ]];then
delete_dir "$dir_date" "old"
fi
done
else
cmd="rclone copy $tsource $dest/$bckp/$timestamp $log_option $option"
echo "$cmd"
eval $cmd
exit_code=$?
conf_logging "$exit_code"
CMD_LSD="rclone lsd --max-depth 1 $dest/$bckp/"
mapfile -t dir_array < <(eval $CMD_LSD)
DATE=$(date -d "$now - $del_after days" +'%Y-%m-%d')
for i in "${!dir_array[@]}"; do
dir_path="${dir_array[i]}"
dir_date=$(echo ${dir_path##* })
dir_date2=$(echo ${dir_date%_*})
conv_date=$(date -d "$dir_date2" +'%Y-%m-%d')
if [[ $conv_date < $DATE ]];then
delete_dir "$dir_date" "mnt"
fi
done
fi
fi
exit 0