forked from fishsponge/daily-backup-snapshot-script
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmakeSnapshot.sh
executable file
·234 lines (188 loc) · 8.63 KB
/
makeSnapshot.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#!/bin/bash
# Daily, Weekly, Monthly & Yearly Snapshots
# Version 1.8 - "echo" commands put in so there's blank lines in between certain outputs.
# Version 1.7 - Variables and if statements added so these things can be chosen: processIsRunning file directory, protocol: rsync or just directory, rsync hostname, username & passwordfile for rsync protocol and checksum yes or no.
# Version 1.6 - Modified "overlap time" so it just uses the same "duration" function and modified "duration" function, so it doesn't say "0 hours, 0 minutes, 12 seconds" - it now removes the zeros and says "12 seconds".
# Version 1.5 - Added "-n" to some of the "echo" commands, so the "done" appears on the same line.
# Version 1.4 - moved "customizable variables" back to the top & echo'ing the duration of each rm, mv, copy and the whole rsync process with start_time & end_time variables.
# Version 1.3 - echo comments modified, start date added to beginning, added overlap time being echo'd and comments being echo'd describing snapshots being deleted, moved and created.
# Version 1.2 - "processIsRunning" file created when running and while loop created so script doesn't overlap.
# Version 1.1 - rsync protocol added.
# Version 1.0 - Original.
# Written by Richard Hobbs (fishsponge)
# http://www.rhobbs.co.uk/
################################
# CUSTOMIZABLE VARIABLES BELOW #
################################
# The directory in which snapshots are stored
# NOTE: This will contain 1 complete copy of
# the original data at *least*.
snapdir="/snapshots"
rsyncsnapdir="snapshots"
# rsync protocol OR directory -> directory
protocol="rsync"
#protocol="directory"
# rsync login details
rsynchostname="nas"
rsyncusername="username"
rsyncpasswordfile="/root/password"
# rsync checksums? "yes" or "no"
checksums="no"
# directory for "makeSnapshotProcessIsRunning" file
pirfiledir="/root/scripts/makeSnapshots"
# Source directories to put into the snapshots
# NOTE: Do *NOT* include the snapshot directory.
dirs[0]='/root/'
dirs[1]='/etc/'
dirs[2]='/home/'
# NOTE: If you reduce the maximum number of snapshots
# you may have to delete the higher numbered snapshots
# yourself.
# Maximum number of daily snapshots
maxdaily=7
# Maximum number of weekly snapshots
maxweekly=5
# Maximum number of monthly snapshots
maxmonthly=12
# Maximum number of yearly snapshots
maxyearly=10
# Day of week to run weekly, monthly & yearly snapshots
# 1=Mon, 2=Tue, 3=Wed, 4=Thu, 5=Fri, 6=Sat, 7=Sun
# NOTE: All weekly, monthly & yearly snapshots
# will be based on the day before.
daytorun=2
################################
# DO NOT EDIT BELOW THIS LINE #
################################
echo -n "Start: "; date
echo
duration ()
{
hours=`echo $(( (($end_time - $start_time) / 60) / 60 ))`
minutes=`echo $(( (($end_time - $start_time) / 60) - ($hours * 60) ))`
seconds=`echo $(( ($end_time - $start_time) - ($minutes * 60) - ($hours * 60 * 60) ))`
if [ $hours != 0 ]
then
echo "$hours hours, $minutes minutes, $seconds seconds"
else
if [ $minutes != 0 ]
then
echo "$minutes minutes, $seconds seconds"
else
echo "$seconds seconds"
fi
fi
}
start_time=`date +%s`
while [ 1 ]
do
if [ -f "${pirfiledir}/makeSnapshotProcessIsRunning" ]
then
echo "Another \"makeSnapshot\" process is currently running (`date`) so waiting for it to finish..."; sleep 257
else
echo "makeSnapshot has finished (or is not running), so starting the process..."; break
fi
done
end_time=`date +%s`
echo "Overlap took `duration`."
echo
echo -n "Snapshot creation starts: "; date
echo
touch ${pirfiledir}/makeSnapshotProcessIsRunning
if [ ! -d ${snapdir} ]; then echo "${snapdir} doesn't exist.\nPlease check \"\$snapdir=\" in the script or run \"mkdir ${snapdir}\"" >&2; exit 1; fi
if [ `date +%u` == "${daytorun}" ]; then doweekly="yes"; fi
if [ `date +%u` == "${daytorun}" ]; then if [ `date +%d` -le "7" ]; then domonthly="yes"; fi; fi
if [ `date +%u` == "${daytorun}" ]; then if [ `date +%j` -le "7" ]; then doyearly="yes"; fi; fi
if [ ! -d ${snapdir}/daily.0 ]; then echo "\"${snapdir}/daily.0/\" directory doesn't exist, so creating it now."; echo; mkdir ${snapdir}/daily.0 || exit 1; fi
# DAILY
minus1=`expr ${maxdaily} - 1`
if [ -d ${snapdir}/daily.${minus1} ]; then echo -n "Removing \"daily.${minus1}\"..."; start_time=`date +%s`; rm -rf ${snapdir}/daily.${minus1}; end_time=`date +%s`; echo " done (`duration`)"; echo; fi
for ((ss=${minus1}; ss >= 2; ss--))
do
ssminus1=`expr ${ss} - 1`
if [ -d ${snapdir}/daily.${ssminus1} ]; then echo -n "Moving \"daily.${ssminus1}\" to \"daily.${ss}\"..."; start_time=`date +%s`; mv ${snapdir}/daily.${ssminus1} ${snapdir}/daily.${ss}; end_time=`date +%s`; echo " done (`duration`)"; fi
done
echo
if [ -d ${snapdir}/daily.0 ]; then echo -n "Synchronizing \"daily.0\" with \"daily.1\"..."; start_time=`date +%s`; cp -al ${snapdir}/daily.0 ${snapdir}/daily.1; end_time=`date +%s`; echo " done (`duration`)"; echo; fi
# WEEKLY
if [ "${doweekly}" == "yes" ]
then
minus1=`expr ${maxweekly} - 1`
if [ -d ${snapdir}/weekly.${minus1} ]; then echo -n "Removing \"weekly.${minus1}\"..."; start_time=`date +%s`; rm -rf ${snapdir}/weekly.${minus1}; end_time=`date +%s`; echo " done (`duration`)"; echo; fi
for ((ss=${minus1}; ss >= 1; ss--))
do
ssminus1=`expr ${ss} - 1`
if [ -d ${snapdir}/weekly.${ssminus1} ]; then echo -n "Moving \"weekly.${ssminus1}\" to \"weekly.${ss}\"..."; start_time=`date +%s`; mv ${snapdir}/weekly.${ssminus1} ${snapdir}/weekly.${ss}; end_time=`date +%s`; echo " done (`duration`)"; fi
done
echo
if [ -d ${snapdir}/daily.0 ]; then echo -n "Synchronizing \"daily.0\" with \"weekly.0\"..."; start_time=`date +%s`; cp -al ${snapdir}/daily.0 ${snapdir}/weekly.0; end_time=`date +%s`; echo " done (`duration`)"; echo; fi
fi
# MONTHLY
if [ "${domonthly}" == "yes" ]
then
minus1=`expr ${maxmonthly} - 1`
if [ -d ${snapdir}/monthly.${minus1} ]; then echo -n "Removing \"monthly.${minus1}\"..."; start_time=`date +%s`; rm -rf ${snapdir}/monthly.${minus1}; end_time=`date +%s`; echo " done (`duration`)"; echo; fi
for ((ss=${minus1}; ss >= 1; ss--))
do
ssminus1=`expr ${ss} - 1`
if [ -d ${snapdir}/monthly.${ssminus1} ]; then echo -n "Moving \"monthly.${ssminus1}\" to \"monthly.${ss}\"..."; start_time=`date +%s`; mv ${snapdir}/monthly.${ssminus1} ${snapdir}/monthly.${ss}; end_time=`date +%s`; echo " done (`duration`)"; fi
done
echo
if [ -d ${snapdir}/daily.0 ]; then echo -n "Synchronizing \"daily.0\" with \"monthly.0\"..."; start_time=`date +%s`; cp -al ${snapdir}/daily.0 ${snapdir}/monthly.0; end_time=`date +%s`; echo " done (`duration`)"; echo; fi
fi
# YEARLY
if [ "${doyearly}" == "yes" ]
then
minus1=`expr ${maxyearly} - 1`
if [ -d ${snapdir}/yearly.${minus1} ]; then echo -n "Removing \"yearly.${minus1}\"..."; start_time=`date +%s`; rm -rf ${snapdir}/yearly.${minus1}; end_time=`date +%s`; echo " done (`duration`)"; echo; fi
for ((ss=${minus1}; ss >= 1; ss--))
do
ssminus1=`expr ${ss} - 1`
if [ -d ${snapdir}/yearly.${ssminus1} ]; then echo -n "Moving \"yearly.${ssminus1}\" to \"yearly.${ss}\"..."; start_time=`date +%s`; mv ${snapdir}/yearly.${ssminus1} ${snapdir}/yearly.${ss}; end_time=`date +%s`; echo " done (`duration`)"; fi
done
echo
if [ -d ${snapdir}/daily.0 ]; then echo -n "Synchronizing \"daily.0\" with \"yearly.0\"..."; start_time=`date +%s`; cp -al ${snapdir}/daily.0 ${snapdir}/yearly.0; end_time=`date +%s`; echo " done (`duration`)"; echo; fi
fi
# LIVE DATA
echo "Starting rsync process..."
echo
start_time=`date +%s`
for dir in "${dirs[@]}"
do
echo "Rsyncing \"${dir}\"..."
if [ ${protocol} = "rsync" ]
then
if [ ${checksums} = "yes" ]
then
rsync -avSc --delete --password-file=${rsyncpasswordfile} ${dir}/ ${rsyncusername}@${rsynchostname}::${rsyncsnapdir}/daily.0/${dir}/
else
rsync -avS --delete --password-file=${rsyncpasswordfile} ${dir}/ ${rsyncusername}@${rsynchostname}::${rsyncsnapdir}/daily.0/${dir}/
fi
else
if [ ${protocol} = "directory" ]
then
if [ ${checksums} = "yes" ]
then
rsync -avSc --delete ${dir}/ ${snapdir}/daily.0/${dir}/
else
rsync -avS --delete ${dir}/ ${snapdir}/daily.0/${dir}/
fi
else
echo "No protocol specified, so dropping rsync protocol and using directory..."
if [ ${checksums} = "yes" ]
then
rsync -avSc --delete ${dir}/ ${snapdir}/daily.0/${dir}/
else
rsync -avS --delete ${dir}/ ${snapdir}/daily.0/${dir}/
fi
fi
fi
echo
done
echo
end_time=`date +%s`
echo "Entire rsync process done (`duration`)"
echo
touch ${snapdir}/daily.0
rm ${pirfiledir}/makeSnapshotProcessIsRunning
echo -n "End: "; date