-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathBackup-encryption-Email-Ubuntu.sh.txt
332 lines (154 loc) · 6.49 KB
/
Backup-encryption-Email-Ubuntu.sh.txt
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#!/bin/bash
#Author:Muhamamd Asim
#Date:10-june-2017
#Purpose:Back up on a remote location, with compression & auto updation of backup.
#Defining the source to backup
#SOURCE
LOCATION="/home/"
SRC="source"
#Creation of Source directory at /home, in which we place all our data, which we want to backup.
if [ ! -d $LOCATION$SRC ]
then
echo -e "\nCreation of $SRC directory at $LOCATION\n"
mkdir $LOCATION$SRC
fi
SRCNAME="`date +%F`.tar"
#DESTINATION
DST="/home/destination"
if [ ! -d $DST ]
then
echo -e "\nCreation of $DST directory at $LOCATION\n"
mkdir $DST
fi
#Create A Backup of $SRC
#Note:Very Very Important research tar actually creates an archiving of all absolute paths, which are not required.
#The option -C works; just for clarification I'll post 1 example
#creation of a tarball without the full path: full path /home/testuser/workspace/project/application.war
#and what we want is just project/application.war so:
#tar -cvf output_filename.tar -C /home/testuser/workspace project
#Note: there is a space between workspace and project; tar will replace full path with just project
SRCTAR=`tar -cvf "$SRCNAME" -C "$LOCATION" "$SRC"`
echo $SRCTAR &> /dev/null
if [ "$?" = "0" ]
then
echo -e "\nMoving Source Backup to Destination\n"
mv $SRCNAME $DST &> /dev/null
if [ ! -f $DST/$SRCNAME ]
then
echo -e "\nTar Back file does not exist\n"
elif [ -f $DST/$SRCNAME ]
then
BZ2NAME="`date +%F`.tar.bz2"
#echo $BZ2NAME
#Creation of BZ2 compression.
BZ2=`tar -cjvf "$DST/$BZ2NAME" -C "$DST" "$SRCNAME"`
echo "$BZ2" &> /dev/null
fi
#Removal .tar file from destination.
GPGEXTENSION=".gpg"
if [ -f $DST/$BZ2NAME ]
then
echo -e "\nCreating a $BZ2NAME Compression Backup and encrypting this file as $BZ2NAME$GPGEXTENSION\n"
rm -rf $DST/$SRCNAME$* &> /dev/null
fi
#Extracting bz2 compression.
if [ -f $DST/$BZ2NAME ]
then
bunzip2 $DST/$BZ2NAME &> /dev/null
#Updating tar file
if [ "$?" = "0" ]
then
tar - uvf $DST/$SRCNAME &> /dev/null
#Recreation of BZ2 Compression.
BZ2=`tar -cjvf "$DST/$BZ2NAME" -C "$DST" "$SRCNAME"`
echo "$BZ2" &> /dev/null
#Re-removal .tar file from destination.
if [ -f $DST/$BZ2NAME ]
then
#echo -e "\nWe are removing $SRCNAME from $DST & creating a $BZ2NAME\n"
rm -rf $DST/$SRCNAME$* &> /dev/null
fi
fi
fi
#3rd Stage will be BZ2 Image with encryption.
#Define encryption command first
#We will be using gpg -c option for encrytion, which is -c, --symmetric encryption only with symmetric cipher
#symmetric means same key will be used for encryption and decrytion
if [ -f $DST/$BZ2NAME ]
then
#Creation of encryption with KEY variable, please set a KEY, further if you change a KEY, comment out the OLDKEY
GPGTHIS=`ls -lth $DST/ | head -n2 | grep -i bz2 | awk '{print $9}'`
KEY="ami"
GPG=`gpg --yes --batch --passphrase=$KEY -c $DST/$GPGTHIS`
echo $GPG
#Removal of bz2 image
if [ "$?" = "0" ] #Opening of Removal of bz2 images
then
REMOVEBZ2=`ls -ltrh $DST/*.bz2 | head -n1 | awk '{print $9}'`
rm -rf $REMOVEBZ2
fi #Closing of Removal of bz2 Images from /home/destination
#Shifting GPG encrypted file to a remote location
GPGREMOTE=`ls -lth $DST/*.gpg | head -n1 | awk '{print $9}'`
if [ -f $GPGREMOTE ]
then
echo $GPGREMOTE > /dev/null
REMOTE="192.168.1.7"
if [ -f $GPGREMOTE ]
then
ping -c3 $REMOTE &> /dev/null
if [ "$?" = "0" ]
then
echo -e "\nWe are shifting your Data to $REMOTE please wait.....\n"
rsync -auv $GPGREMOTE ssh root@$REMOTE:/root 2> /dev/null
#Creating an E-mail function to know about the space status & health of our SERVER
#GOOGLE => ALLOW LESS SECURE APP ON ===> https://myaccount.google.com/u/1/lesssecureapps?pli=1
function PASS() {
SERVER=smtp.gmail.com:587
SUBJECT="Congratulations Backup successfull"
BODY="Dear Concern,\n
\n
This is to inform you, Backup of $GPGREMOTE to $REMOTE was successfull\n
\n
Regards,\n
\n
Muhammad Asim"
PASS=hackerhacker150pakistan!@#$%
sendEmail -f $FROM -t $TO -u $SUBJECT -m $BODY -s $SERVER -xu $USER -xp $PASS
}
if [ "$?" = "0" ]
then
PASS
fi
else
#GOOGLE => ALLOW LESS SECURE APP ON ===> https://myaccount.google.com/u/1/lesssecureapps?pli=1
function FAIL() {
GPGREMOTE=`ls -lth $DST/*.gpg | head -n1 | awk '{print $9}'`
REMOTE="192.168.1.7"
SERVER=smtp.gmail.com:587
SUBJECT="Sorry Backup Un-Successfull"
BODY="Dear Concern,\n
\n
This is to inform you, Backup of $GPGREMOTE to $REMOTE was unsuccessfull\n
\n
Regards,\n
\n
Muhammad Asim"
PASS=hackerhacker150pakistan!@#$%
sendEmail -f $FROM -t $TO -u $SUBJECT -m $BODY -s $SERVER -xu $USER -xp $PASS
}
FAIL
echo -e "\n$REMOTE Server is down\n"
fi
fi
fi
fi
else
echo -e "\nSomething is wrong in creating $SRC tar backup\n"
fi
#END