forked from Daggenthal/Warshade
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.py
More file actions
771 lines (450 loc) · 25.6 KB
/
functions.py
File metadata and controls
771 lines (450 loc) · 25.6 KB
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
import getpass, os
from subprocess import run
from sys import exit
from time import sleep
from programs import *
def fullBackup():
while True:
# Cleans up the /tmp/Backup/ directory incase the script was executed without fully completing.
run(['cd /tmp/ && sudo rm -rf Backup/'], shell=True, check=True)
# Create a temporary directory that will be used throughout the script.
run(['mkdir /tmp/Backup/ && mkdir /tmp/Backup/etc/ && mkdir /tmp/Backup/usr'], shell=True, check=True)
run(['clear'], shell=True)
print('\t Grabbing pv to show pipe progress during compression and decompression...\n')
if distro[0 or 1] in OS:
run([dpv], shell=True, check=True)
elif distro[2 or 3] in OS:
run([fpv], shell=True, check=True)
elif distro[4] in OS:
run([apv], shell=True, check=True)
elif distro[5] in OS:
run([opv], shell=True, check=True)
elif distro[6] in OS:
run([bpv], shell=True, check=True)
print('\t PV has been installed, continuing backup...')
sleep(3)
run(['clear'])
userName = input('\n\t Please input your MariaDB Username: ')
passWord = getpass.getpass('\n\t Please input your password: ')
run(['clear'], shell=True, check=True)
# Start the backup process of the mariaDB database.
print('\n\t Backup initiated, this may take a bit and is active, please wait...\n')
run(['cd /tmp/Backup && mysqldump --user=' + userName + ' --password=' + passWord + ' --lock-tables --all-databases > server_db_backup.sql'], shell=True, check=True)
print('\t Database has been backed up! Backing up /etc/...\n')
# Starts the backup process of my.cnf, NGINX, apache(HTTPD), and postfix for the mail system / SendGrid settings, then moves them in the tmp directory.
run(['cp /etc/my.cnf /tmp/Backup/etc/'], shell=True, check=True)
run(['cp /etc/php.ini /tmp/Backup/etc/'], shell=True, check=True)
run(['sudo cp -r /etc/nginx/ /tmp/Backup/etc/'], shell=True, check=True)
run(['sudo cp -r /etc/postfix/ /tmp/Backup/etc/'], shell=True, check=True)
# Starts the backup process of the letsencrypt certs for the website's SSL
run(['sudo cp -r /etc/letsencrypt/ /tmp/Backup/etc/'], shell=True, check=True)
print('\t /etc/ has been backed up! Backing up the website...\n')
# Starts the backup process of the website, and its included files. This may take long depending on what's in there.
run(['sudo cp -r /usr/share/nginx/ /tmp/Backup/usr/'], shell=True, check=True)
print('\t The website has been backed up! Compressing files...\n\t')
# Dates and compresses the /tmp/Backup/ folder for RSYNC later on, then removes the temporary /tmp/Backup/ folder.
run(["""cd /tmp/ && sudo tar -zcvf "$(date '+%Y-%m-%d').tar.gz" /tmp/Backup/"""], shell=True, check=True)
run(['cd /tmp/ && sudo rm -rf Backup/'], shell=True, check=True)
print('\n\t Backup has been completed, would you like to return to the main menu?\n')
print('\t 1: Yes')
print('\t 2: No\n')
response = input('\t Please input your selection: ')
if response == '1':
break
elif response == '2':
run(['clear'], shell=True)
exit()
def dbBackup():
while True:
# Clear the terminal for our next output
run(['clear'], shell=True)
# Print on the screen that the backup is being initiated
print('\n\t MariaDB backup initiated...')
# Ask for the Username and Password, storing those into 2 variables we've aptly created.
userName = input('\n\t Please input your MariaDB Username: ')
passWord = getpass.getpass('\n\t Please input your password: ')
# Print out that we are initializing the backup, and tell the user where it will be located.
print('\n\t The DB backup will be stored in /tmp/\n\t Please wait, as depending on the size of the DB, this may seem inactive...')
sleep(1.25)
# Use the information we've gathered earlier, and CD into the specific user directory that this is being ran inside of. If we did this in /tmp/, we'd have to make a folder there and constantly check for it.
# Instead, we're just storing it in their /Documents/ folder, as that is >typically< created upon user account creation.
# We finalzie this process by tagging the Year, Month, and Day of DB creation so we can select which one to recover from later.
run(["cd /tmp/ && mysqldump --user=" + userName + " --password=" + passWord + " --lock-tables --all-databases > $(date '+%Y-%m-%d').sql"], shell=True, check=True)
# Clear the screen upon completion, and tell the user that it has finished successfully.
run(["clear"], shell=True)
print('\n\t The Database was successfully backed up. The default location is in /home/$USER/Documents/')
exit()
def dbRestore():
while True:
# Clear the terminal.
run(['clear'], shell=True)
print('Scanning the /tmp/ directory for any databases...')
sleep(1)
run(['clear'], shell=True)
# Now we will search the specified location for .sql files, and if none are found, exit. If they are found, allow the user to select them and restore the database with it.
while True:
try:
# Get the list of files inside the /home/$USER/Documents/ directory
files = os.listdir('/tmp/')
# Print out a numbered list of files
for index, file in enumerate(files):
if file.endswith(".sql"):
print(f"{index+1}. {file}\n")
# Get the user's input
selection = int(input('Enter the number of the file you wish to select: '))
# Check if the selection is valid
if selection > 0 and selection <= len(files):
# Get the file name
output_file = files[selection-1]
else:
print("Invalid selection")
# Grab the DB Username we will use to force the DB to be restored.
userName = input('\n\t Please input your MariaDB Username: ')
# Force the restoration by inputting the username, asking a password, and pushing through the file we've selected.
run(['sudo mysql --user ' + userName + ' --password --force < ' + output_file], shell=True, check=True)
# Clear the shell and print out that it was successful.
run(['clear'], shell=True)
print('\n\t The Database was properly restored! Exiting now...')
sleep(1.5)
exit()
except:
# Clear the terminal.
run(['clear'], shell=True)
# Tell the user that nothing was found.
print('\n\t No files with the .sql extension were -\n\t - found in /tmp/, or the program was exited.\n')
exit()
def webBackup():
while True:
# Clears the terminal, creates a directory for where we'll store our website backup, then we'll tar the directories and move them into /home/$USER/Documents while being timestamped.
run(['clear'], shell=True)
# Cleans up the /tmp/Backup/ directory incase the script was executed without fully completing.
run(['cd /tmp/ && sudo rm -rf Backup/'], shell=True, check=True)
# Create a temporary directory that will be used throughout the script.
run(['mkdir /tmp/Backup/ && mkdir /tmp/Backup/etc/ && mkdir /tmp/Backup/usr'], shell=True, check=True)
# Starts the backup process of my.cnf, NGINX, apache(HTTPD), and postfix for the mail system / SendGrid settings, then moves them in the tmp directory.
run(['cp /etc/my.cnf /tmp/Backup/etc/'], shell=True, check=True)
run(['cp /etc/php.ini /tmp/Backup/etc/'], shell=True, check=True)
run(['sudo cp -r /etc/nginx/ /tmp/Backup/etc/'], shell=True, check=True)
run(['sudo cp -r /etc/postfix/ /tmp/Backup/etc/'], shell=True, check=True)
# Starts the backup process of the letsencrypt certs for the website's SSL
run(['sudo cp -r /etc/letsencrypt/ /tmp/Backup/etc/'], shell=True, check=True)
print('\t /etc/ has been backed up! Backing up the website...\n')
# Starts the backup process of the website, and its included files. This may take long depending on what's in there.
run(['sudo cp -r /usr/share/nginx/ /tmp/Backup/usr/'], shell=True, check=True)
print('\t The website has been backed up! Compressing files...\n\t')
# Dates and compresses the /tmp/Backup/ folder for RSYNC later on, then removes the temporary /tmp/Backup/ folder.
run(["""cd /tmp/ && sudo tar -zcvf "$(date '+%Y-%m-%d')_webBackup.tar.gz" /tmp/Backup/"""], shell=True, check=True)
# Moves the folder we've created to the /tmp/ directory to be cleared whenever the server is rebooted, or when /tmp/ is usually cleared.
run(['cd /tmp/ && sudo rm -rf Backup/'])
# Clear the terminal and tell the user that the operation finished.
run(['clear'], shell=True)
print('\n\t The Website and its subdirectories have been fully backed up, and are located inside /home/$USER/Documents')
print('\n\t Exiting program...')
sleep(1.5)
exit()
def webRestore():
while True:
# This function lists the users Website backups that are located in /home/$USER/Documents/ and allows them to restore NGINX, the website, and all required files to be fully operational.
run(['clear'], shell=True)
print('\n\t Scanning the /tmp/ directory for files...')
sleep(1)
run(['clear'], shell=True)
try:
# Get the list of files inside the /tmp/ directory
files = os.listdir('/tmp/')
# Print out a numbered list of files
for index, file in enumerate(files):
if file.endswith("_webBackup.tar.gz"):
print(f"{index+1}. {file}\n")
# Get the user's input
selection = int(input('Enter the number of the file you wish to select: '))
# Check if the selection is valid
if selection > 0 and selection <= len(files):
# Get the file name
output_file = files[selection-1]
else:
print("Invalid selection")
# Clear the screen and tell the user that a file was selected, and that the operation will begin.
run(['clear'], shell=True)
print('\n\t Website Backup file has been selected, attempting restoration...')
# Here we're going to disable the services while we attempt to restore them.
print('\t Disabling services momentarily...\n\t')
sleep(1.25)
if distro[0 or 1 or 2 or 3 or 4 or 5] in OS:
run(['sudo systemctl stop nginx postfix mariadb memcached.service'], shell=True, check=True)
# Turns out that in FreeBSD, services aren't auto-started once installed. Here, we enable to start on boot, but for now they'll stay turned off.
# Later on we'll actually start these.
elif distro[6] in OS:
run(['sudo sysrc nginx_enable=YES'], shell=True, check=True)
run(['sudo sysrc postfix_enable=YES'], shell=True, check=True)
run(['sudo sysrc mysql_enable=YES'], shell=True, check=True)
run(['sudo sysrc memcached_enable=YES'], shell=True, check=True)
run(['clear'], shell=True, check=True)
print('\t Services have successfully been disabled. Attempting restoration, please wait...\n\t')
sleep(1.25)
# Here we're beginning to decompress the file we created, and moved, earlier. This contains everything we need to properly setup the new server.
print('\t Attempting to decompress the file, please wait...\n\t')
sleep(2)
run(['cd /tmp/ && sudo pv ' + output_file + ' | tar -xz'], shell=True, check=True)
sleep(3)
run(['clear'], shell=True)
print('\t The file has successfully been decompressed! Attempting restore...\n\t')
sleep(1.25)
# Here we're going to move the files to the proper directory that they came from.
if distro[0 or 1 or 2 or 3 or 4 or 5] in OS:
run(['cd /tmp/tmp/Backup/etc && sudo rsync -avr * /etc/'], shell=True, check=True)
elif distro[6] in OS:
run(['cd /tmp/tmp/Backup/etc/ && sudo cp my.cnf /usr/local/etc/mysql/'], shell=True, check=True)
run(['cd /tmp/tmp/Backup/etc/ && sudo cp php.ini /usr/local/etc/'], shell=True, check=True)
# You may ask yourself why we're moving the directories out. Errors are thrown that the directories already exist, so we're moving them!
# Sure, we can use -f to force the mv into the directory, but moving them to /tmp/ is cleaner, as later on we enable /tmp/ to be autocleared upon reboot.
run(['cd /usr/local/etc/ && sudo mv nginx/ /tmp/'], shell=True, check=True)
run(['cd /usr/local/etc/ && sudo mv postfix /tmp/'], shell=True, check=True)
# Now we'll finally move the directories that we want, back into their proper homes!
run(['cd /tmp/tmp/Backup/etc && sudo rsync -avr nginx/ postfix/ localhost:/usr/local/etc/'], shell=True, check=True)
print('\t /etc/ folders have successfully been restored! Attempting website restore...\n\t')
sleep(1.25)
# Now we're going to move the website and mail certs back to their origin.
# We move the folder that's created by nginx, to the /tmp/ directory, so we can install our own copy.
if distro[0 or 1 or 2 or 3 or 4 or 5] in OS:
run(['cd /tmp/tmp/Backup/usr && sudo rsync -avr nginx/ /usr/share/'])
elif distro[6] in OS:
run(['sudo mkdir /tmp/holding/'], shell=True, check=True)
run(['cd /usr/local/www/ && sudo rm -rf nginx/'], shell=True, check=True)
run(['cd /tmp/tmp/Backup/usr/ && sudo mv nginx/ /usr/local/www/'], shell=True, check=True)
print('\t Website has successfully been restored! Attempting API key restore...\n\t')
sleep(1.25)
# Now we're going to setup postfix so we can use the same API keys
if distro[0 or 1 or 2 or 3 or 4 or 5] in OS:
run(['sudo postmap /etc/postfix/sasl_passwd'], shell=True, check=True)
elif distro[6] in OS:
# We need to create the group postdrop, and then give them postfix permissions.
run(['sudo pw groupadd postdrop && sudo postfix set-permissions'], shell=True, check=True)
# Now we can properly set the main.cf file for our usage.
run(['sudo postmap /usr/local/etc/postfix/sasl_passwd'], shell=True, check=True)
print('\t API keys have been successfully restored! Attempting SSL certs restore...\n')
sleep(1.25)
# Now we're going to restore the letsencrypt SSL certificates for the website.
if distro[0 or 1 or 2 or 3 or 4 or 5] in OS:
sleep(0.25)
elif distro[6] in OS:
run(['cd /usr/local/etc/ && sudo mv letsencrypt/ /tmp/holding/'], shell=True, check=True)
run(['cd /tmp/tmp/Backup/etc && sudo mv letsencrypt/ /usr/local/etc/'], shell=True, check=True)
print('\t SSL certs have successfully been restored!\n\t')
sleep(1.25)
# Now we'll start the services again, and enable them to persist upon reboot.
print('\n\t Starting services, and enabling them for future reboots, please wait...\n')
if distro[0 or 1 or 2 or 3 or 4 or 5] in OS:
run(['sudo systemctl start nginx postfix mariadb memcached.service && sudo systemctl enable nginx postfix mariadb memcached.service'], shell=True, check=True)
elif distro[6] in OS:
run(['sudo service nginx postfix mysql-server memcached start'], shell=True, check=True)
# This line right here adds 'weekly_certbot_enable="YES"' to a file that we create, so certbot is checked weekly.
# We're also disabling sendmail, eventhough it's not present, due to enabling postfix prior in this script.
run(["""sudo touch /etc/periodic.conf && sudo echo 'weekly_certbot_enable="YES"' >> /etc/periodic.conf """], shell=True, check=True)
run(["""sudo echo 'sendmail_enable="NONE"' >> /etc/periodic.conf """], shell=True, check=True)
run(["""sudo echo 'clear_tmp_enable="YES" >> /etc/rc.conf"""], shell=True, check=True)
except:
run(['clear'], shell=True)
print('\n\t No _website.tar.gz files were located in /tmp/, or the user exited the program, aborting...')
sleep(5)
exit()
def transferBackup():
while True:
# This clears the terminal, and attempts to RSYNC the previously .tar.gz file we created earlier, and attempts to send it to the new one.
run(['clear'], shell=True)
# Here we're storing the target userName, and ipAddress, for the server where our file will be rsync'd to.
# These will be called later on.
userName = input('\n\t Please input the target username: ')
ipAddress = input('\n\t Please input the target IP Address: ')
run(['clear'], shell=True)
print('\n\t Are these correct?')
print('\n\t Username: ', userName)
print('\t IP Address: ', ipAddress, '\n')
print('\t 1: Yes')
print('\t 2: No\n')
response = input('\t Please input your selection: ')
if response == '1':
run(['clear'], shell=True)
# Get the list of files inside the /tmp/ directory.
files = os.listdir('/tmp/')
# Print out a numbered list of files that we scan for.
try:
for index, file in enumerate(files):
if file.endswith('.tar.gz'):
print(f"{index+1}. {file}\n")
except:
print("\n\tNo files with the extension of '.tar.gz' exists in /tmp/\n Please check it or move files there. Exiting now...\n")
sleep(2.5)
exit()
# Get the user's input
selection = int(input('Enter the number of the file you wish to select: '))
# Check if the selection is valid
if selection > 0 and selection <= len(files):
# Get the file name
output_file = files[selection-1]
else:
print("Invalid selection")
# Now we're going to RSYNC our selected file to the destination that we've chosen earlier.
run(['cd /tmp/ && sudo rsync -av -P ' + output_file + ' ' + userName + '@' + ipAddress + ':/tmp/'], shell=True, check=True)
print('\t Rsync was successful! Would you like to return to the main menu?\n')
print('\t 1: Yes')
print('\t 2: No\n')
response = input('\t Please input your selection: ')
if response == '1':
break
elif response == '2':
run(['clear'], shell=True)
exit()
elif response == '2':
run(['clear'], shell=True)
print('\n\t Returning to main menu...')
sleep(1.25)
break
def serverSetup():
while True:
run(['clear'], shell=True)
# Here we're installing the necessary programs that we'll be using for later on. These are *required* for the restoreBackup() function to properly work.
# What we're doing now is checking the users Linux distribution with 'cat /etc/os-release', and reading the output while scanning for key words, then executing what's needed.
if distro[0 or 1] in OS:
run([debian], shell=True, check=True)
elif distro[2] in OS:
run([fedora], shell=True, check=True)
elif distro[3] in OS:
run([rocky], shell=True, check=True)
elif distro[4] in OS:
run([arch], shell=True, check=True)
elif distro[5] in OS:
run([opensuse], shell=True, check=True)
elif distro[6] in OS:
run([freebsd], shell=True, check=True)
run(['clear'], shell=True)
print('\n\t The prerequisites have been installed! Would you like to return to the main menu?\n')
print('\t 1: Yes')
print('\t 2: No\n')
response = input('\t Please input your selection: ')
if response == '1':
break
elif response == '2':
run(['clear'], shell=True)
exit()
def fullRestore():
while True:
# This CD's into /tmp/ and untar's our .tar.gz that we created earlier.
# Please, make sure that you're in the proper folder in order for this to work.
run(['clear'], shell=True)
# Here we're going to disable the services while we attempt to restore them.
print('\t Disabling services momentarily...\n\t')
sleep(1.25)
if distro[0 or 1 or 2 or 3 or 4 or 5] in OS:
run(['sudo systemctl stop nginx postfix mariadb memcached.service'], shell=True, check=True)
# Turns out that in FreeBSD, services aren't auto-started once installed. Here, we enable to start on boot, but for now they'll stay turned off.
# Later on we'll actually start these.
elif distro[6] in OS:
run(['sudo sysrc nginx_enable=YES'], shell=True, check=True)
run(['sudo sysrc postfix_enable=YES'], shell=True, check=True)
run(['sudo sysrc mysql_enable=YES'], shell=True, check=True)
run(['sudo sysrc memcached_enable=YES'], shell=True, check=True)
run(['clear'], shell=True, check=True)
print('\t Services have successfully been disabled. Attempting restoration, please wait...\n\t')
sleep(1.25)
# Get the list of files inside the /tmp/ directory
files = os.listdir('/tmp/')
# Print out a numbered list of files
for index, file in enumerate(files):
if file.endswith(".tar.gz"):
print(f"{index+1}. {file}\n")
# Get the user's input
selection = int(input('Enter the number of the file you wish to select: '))
# Check if the selection is valid
if selection > 0 and selection <= len(files):
# Get the file name
output_file = files[selection-1]
else:
print("Invalid selection")
# Here we're beginning to decompress the file we created, and moved, earlier. This contains everything we need to properly setup the new server.
print('\t Attempting to decompress the file, please wait...\n\t')
sleep(2)
run(['cd /tmp/ && sudo pv ' + output_file + ' | tar -xz'], shell=True, check=True)
sleep(3)
run(['clear'], shell=True)
print('\t The file has successfully been decompressed! Attempting restore...\n\t')
sleep(1.25)
# Here we're going to move the files to the proper directory that they came from.
if distro[0 or 1 or 2 or 3 or 4 or 5] in OS:
run(['cd /tmp/tmp/Backup/etc && sudo rsync -avr * /etc/'], shell=True, check=True)
elif distro[6] in OS:
run(['cd /tmp/tmp/Backup/etc/ && sudo cp my.cnf /usr/local/etc/mysql/'], shell=True, check=True)
run(['cd /tmp/tmp/Backup/etc/ && sudo cp php.ini /usr/local/etc/'], shell=True, check=True)
# You may ask yourself why we're moving the directories out. Errors are thrown that the directories already exist, so we're moving them!
# Sure, we can use -f to force the mv into the directory, but moving them to /tmp/ is cleaner, as later on we enable /tmp/ to be autocleared upon reboot.
run(['cd /usr/local/etc/ && sudo mv nginx/ /tmp/'], shell=True, check=True)
run(['cd /usr/local/etc/ && sudo mv postfix /tmp/'], shell=True, check=True)
# Now we'll finally move the directories that we want, back into their proper homes!
run(['cd /tmp/tmp/Backup/etc && sudo rsync -avr nginx/ postfix/ localhost:/usr/local/etc'])
print('\t /etc/ folders have successfully been restored! Attempting website restore...\n\t')
sleep(1.25)
# Now we're going to move the website and mail certs back to their origin.
if distro[0 or 1 or 2 or 3 or 4 or 5] in OS:
run(['cd /tmp/tmp/Backup/usr && sudo rsync -avr nginx/ /usr/share'], shell=True, check=True)
elif distro[6] in OS:
run(['sudo mkdir /tmp/holding/'], shell=True, check=True)
run(['cd /tmp/tmp/Backup/usr/ && sudo rsync -avr nginx/ /usr/local/www/'], shell=True, check=True)
print('\t Website has successfully been restored! Attempting API key restore...\n\t')
sleep(1.25)
# Now we're going to setup postfix so we can use the same API keys
if distro[0 or 1 or 2 or 3 or 4 or 5] in OS:
run(['sudo postmap /etc/postfix/sasl_passwd'], shell=True, check=True)
elif distro[6] in OS:
# We need to create the group postdrop, and then give them postfix permissions.
run(['sudo pw groupadd postdrop && sudo postfix set-permissions'], shell=True, check=True)
# Now we can properly set the main.cf file for our usage.
run(['sudo postmap /usr/local/etc/postfix/sasl_passwd'], shell=True, check=True)
print('\t API keys have been successfully restored! Attempting SSL certs restore...\n')
sleep(1.25)
# Now we're going to restore the letsencrypt SSL certificates for the website.
if distro[0 or 1 or 2 or 3 or 4 or 5] in OS:
sleep(0.25)
elif distro[6] in OS:
run(['cd /tmp/tmp/Backup/etc && sudo rsync -avr letsencrypt/ /usr/local/etc/'], shell=True, check=True)
print('\t SSL certs have successfully been restored!\n\t')
sleep(1.25)
# Now we'll start the services again, and enable them to persist upon reboot.
print('\n\t Starting services, and enabling them for future reboots, please wait...\n')
if distro[0 or 1 or 2 or 3 or 4 or 5] in OS:
run(['sudo systemctl start nginx postfix mariadb memcached.service && sudo systemctl enable nginx postfix mariadb memcached.service'], shell=True, check=True)
elif distro[6] in OS:
run(['sudo service nginx postfix mysql-server memcached start'], shell=True, check=True)
# This line right here adds 'weekly_certbot_enable="YES"' to a file that we create, so certbot is checked weekly.
# We're also disabling sendmail, eventhough it's not present, due to enabling postfix prior in this script.
run(["""sudo touch /etc/periodic.conf && sudo echo 'weekly_certbot_enable="YES"' >> /etc/periodic.conf """], shell=True, check=True)
run(["""sudo echo 'sendmail_enable="NONE"' >> /etc/periodic.conf """], shell=True, check=True)
run(["""sudo echo 'clear_tmp_enable="YES" >> /etc/rc.conf"""], shell=True, check=True)
print('\n\t Services have successfully been enabled! Configuring the database...\n')
print('\n\t Have you already setup MariaDB?\n')
print('\t 1: Yes')
print('\t 2: No\n')
response = input('\t Please input your selection: ')
if response == '1':
userName = input('\n\t Please enter your mariaDB username: ')
print('\n\t Attempting mariaDB / mySQL Database restoration, please wait... ')
run(['sudo mysql --user ' + userName + ' --password --force < /tmp/tmp/Backup/server_db_backup.sql'], shell=True, check=True)
print('\n\t Database restoration was successful! Completing restoration, please wait... ')
sleep(1.25)
elif response == '2':
print('\n\t Would you like to go ahead and setup MariaDB?\n')
print('\t 1: Yes')
print('\t 2: No\n')
response = input('\t Please input your selection: ')
if response == '1':
run(['sudo mariadb-secure-installation'], shell=True, check=True)
userName = input('\n\t Please enter your mariaDB username: ')
print('\n\t Attempting mariaDB / mySQL Database restoration, please wait... ')
run(['sudo mysql --user ' + userName + ' --password --force < /tmp/tmp/Backup/server_db_backup.sql'], shell=True, check=True)
print('\n\t Database restoration was successful! Going back to main menu...')
sleep(4)
break
elif response == '2':
print('\n\t Please note, that you may need to manually\n\t setup the DB for it to properly function.')
print('\n\n\t Restoration complete! Going back to main menu...')
sleep(4)
break