-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup.sh
executable file
·197 lines (159 loc) · 5.65 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#!/bin/bash
#+--------------------------------------------------------------------------------------+
# Author: Aysad Kozanoglu
# email : [email protected]
# Version: 1.0
#
# (Türkiye / Germany)
#
# =============================
# Full automated Backup script
# daily weekly monthly backups
# with backupSet Credentials
# =============================
#
# crontab credendials
# (hold the steps below first monthly than Weekly and at last Daily)
#
# 0 23 28-31 * * [ $(date -d +1day +%d) -eq 1 ] && /source/automated-Full-Backup-Solution/backup.sh monthly
# 20 23 * * 7 /source/automated-Full-Backup-Solution/backup.sh weekly
# 30 23 * * * /source/automated-Full-Backup-Solution/backup.sh daily
#
#
#--------------------------
# Monthly backup will do:
# -------------------------
# will run onyly between 28-31 days of Month and
# if the next day is the 1. day of next month, Monthly backup will run
# ->> see crontab example before^
#
# if monthly backup run success with the cron credentials
# (cron will execute only between 28-31 of the monthday)
# ->>then weekly backup will get symlink from current success running monthly backup
# ->>then daily backup will get symlink from current success running monthly backup
# ->>daily and weekly backup dont run if current monthly backup were success
#
# checks rotation credential and delete the oldest monthly backup Set
#
#--------------------------
# Weekly backup will do:
#--------------------------
# if weekly backup run success
# than daily backup will get symlink from current success running weekly backup
# ->>daily backup dont run if current weekly backup backup were success
#
# checksrotation credential and delete the oldest weekly backup set
#
#--------------------------
# Daily backup will do:
#--------------------------
# if weekly and monthly backup dont run current day
# ->>than daily will run success
#
# checks rotation credential and delete the oldest daily backup set
#
#+--------------------------------------------------------------------------------------+
#sources to backup
src="/etc /usr/local/nginx/html"
# backup destination
dest=/backup
# log file
backuplog=/var/log/backup.log
# rotation credentials
# the oldest set of every backup will be deleted
daily=14
weekly=3
monthly=2
#const
jahrTag=$(date +%j) # 244
kalWoche=$(date +%V) # 45
heuteTag=$(date +%A) # Sonntag
monatsTag=$(date +%d) # 14
monat=$(date +%B) # Nov
destSubPath="notSet"
sumOfDayBackups=0
sumOfWeekBackups=0
sumOfMonthBackups=0
# script debug
#echo $kalTag " " $kalWoche " " $heuteTag " " $monatsTag " " $monat
#echo $src " " $dest
initCheckBackup(){
if [ ! -d "${dest}/monatBackup" ]; then
mkdir -p ${dest}/monatBackup
fi
if [ ! -d "${dest}/tagBackup" ]; then
mkdir -p ${dest}/tagBackup
fi
if [ ! -d "${dest}/wocheBackup" ]; then
mkdir -p ${dest}/wocheBackup
fi
sumOfDayBackups=$(ls -l ${dest}/tagBackup/ | grep -v total | wc -l)
sumOfWeekBackups=$(ls -l ${dest}/wocheBackup/ | grep -v total | wc -l)
sumOfMonthBackups=$(ls -l ${dest}/monatBackup/ | grep -v total | wc -l)
}
executeBackup(){
mysqldump -u USER -pPASSWORD DATABASENAME > $dest"/"$destSubPath/$(date +%d%m).sql
# ionice -c2 -n7 rsync --bwlimit=5000 -avzh -e ssh --progress --ignore-existing --out-format="%t %f %b"
ionice -c2 -n7 rsync --out-format=" %t %f %b " -avl --delete --stats --progress --log-file="$backuplog" $src $dest"/"$destSubPath/ >> $backuplog 2>&1
}
tagesBackupSets(){
destSubPath="tagBackup/"$heuteTag"-T"$monatsTag"-KW"$kalWoche
if [ -d $dest"/wocheBackup/"$heuteTag"-KW"$kalWoche ]; then
echo "=== wochen backup ist für == diesen Tag == vorhanden" >> $backuplog
exit
fi
if (( $sumOfDayBackups < $daily || $sumOfDayBackups == $daily ))
then
mkdir $dest"/"$destSubPath >> $backuplog 2>&1
executeBackup
else
echo "=== delete === oldest DailySet" >> $backuplog
cd ${dest}/tagBackup/ && rm -rf $(ls -lht ${dest}/tagBackup/ | tail -n1 | awk '{print $9}')
fi
}
wocheBackupSets(){
destSubPath="wocheBackup/"$heuteTag"-KW"$kalWoche
if [ -d $dest"/monatBackup/"$monat"-KW"$kalWoche"-"$heuteTag ]; then
echo "=== monats backup ist für == diese woche == vorhanden" >> $backuplog
exit
fi
if (( $sumOfWeekBackups < $weekly || $sumOfWeekBackups == $weekly ))
then
mkdir $dest"/"$destSubPath >> $backuplog 2>&1
ln -s $dest"/"$destSubPath $dest"/tagBackup/"$heuteTag"-T"$monatsTag"-KW"$kalWoche >> $backuplog 2>&1
executeBackup
else
echo "=== delete === Oldest WeekSet" >> $backuplog
cd ${dest}/wocheBackup/ && rm -rf $(ls -lht ${dest}/wocheBackup/ | tail -n1 | awk '{print $9}')
fi
}
monatBackupSets(){
destSubPath="monatBackup/"$monat"-KW"$kalWoche"-"$heuteTag
if (( $sumOfMonthBackups < $monthly || $sumOfMonthBackups == $monthly ))
then
mkdir $dest"/"$destSubPath >> $backuplog 2>&1
ln -s $dest"/"$destSubPath $dest"/wocheBackup/"$heuteTag"-KW"$kalWoche >> $backuplog 2>&1
ln -s $dest"/"$destSubPath $dest"/tagBackup/"$heuteTag"-T"$monatsTag"-KW"$kalWoche >> $backuplog 2>&1
executeBackup
else
echo "=== delete === Oldest MonthSet" >> $backuplog
cd ${dest}/monatBackup/ && rm -rf $(ls -lht ${dest}/monatBackup/ | tail -n1 | awk '{print $9}')
fi
}
initCheckBackup
if [[ "$1" == "monthly" ]]
then
monatBackupSets
else if [[ "$1" == "weekly" ]]
then
wocheBackupSets
else if [[ "$1" == "daily" ]]
then
tagesBackupSets
else
echo "falsche parameter daily weekly oder monthly"
exit 1
fi
fi
fi
#rsync -e 'ssh -p 30000' -avl --delete --stats --progress [email protected]:/home/demo $dest/