2
2
import os
3
3
import os .path
4
4
import sys
5
+
5
6
sys .path .append ('/usr/local/CyberCP' )
6
7
os .environ .setdefault ("DJANGO_SETTINGS_MODULE" , "CyberCP.settings" )
7
8
8
9
import django
10
+
9
11
try :
10
12
django .setup ()
11
13
except :
25
27
from plogical .dnsUtilities import DNS
26
28
from mailServer .models import Domains as eDomains
27
29
from random import randint
30
+ import json
31
+ from django .shortcuts import HttpResponse
32
+
28
33
try :
29
34
from plogical .virtualHostUtilities import virtualHostUtilities
30
35
from plogical .mailUtilities import mailUtilities
31
36
except :
32
37
pass
33
38
39
+
34
40
class IncJobs (multi .Thread ):
35
41
36
42
def __init__ (self , function , extraArgs ):
@@ -51,29 +57,97 @@ def run(self):
51
57
self .createBackup ()
52
58
elif self .function == 'restorePoint' :
53
59
self .restorePoint ()
60
+ elif self .function == 'remoteRestore' :
61
+ self .restorePoint ()
62
+
63
+ def getRemoteBackups (self ):
64
+ if self .jobid .destination == 'local' :
65
+ path = '/home/%s/incbackup' % (self .website )
66
+ command = 'export RESTIC_PASSWORD=%s && restic -r %s snapshots' % (self .passwordFile , path )
67
+ return ProcessUtilities .outputExecutioner (command ).split ('\n ' )
68
+ elif self .jobid .destination [:4 ] == 'sftp' :
69
+ path = '/home/backup/%s' % (self .website )
70
+ command = 'export RESTIC_PASSWORD=%s PATH=${PATH}:/usr/bin && restic -r %s:%s snapshots' % (
71
+ self .passwordFile , self .backupDestinations , path )
72
+ return ProcessUtilities .outputExecutioner (command ).split ('\n ' )
73
+ else :
74
+ path = '/home/%s/incbackup' % (self .website )
75
+ command = 'export RESTIC_PASSWORD=%s && restic -r %s snapshots' % (self .passwordFile , path )
76
+ return ProcessUtilities .outputExecutioner (command ).split ('\n ' )
77
+
78
+ def fetchCurrentBackups (self ):
79
+ try :
80
+ self .website = self .extraArgs ['website' ]
81
+ self .backupDestinations = self .extraArgs ['backupDestinations' ]
82
+ self .passwordFile = self .extraArgs ['password' ]
83
+
84
+ remotePath = '/home/backup/%s' % (self .website )
85
+
86
+ command = 'export RESTIC_PASSWORD=%s PATH=${PATH}:/usr/bin && restic -r %s:%s snapshots' % (
87
+ self .passwordFile , self .backupDestinations , remotePath )
88
+ result = ProcessUtilities .outputExecutioner (command ).split ('\n ' )
89
+
90
+ activator = 0
91
+ json_data = "["
92
+ checker = 0
93
+
94
+ for items in result :
95
+ if items .find ('---------------' ) > - 1 :
96
+ if activator == 0 :
97
+ activator = 1
98
+ continue
99
+ else :
100
+ activator = 0
101
+
102
+ if activator :
103
+ entry = items .split (' ' )
104
+ logging .writeToFile (str (entry ))
105
+
106
+ dic = {'id' : entry [0 ],
107
+ 'date' : "%s %s" % (entry [2 ], entry [3 ]),
108
+ 'host' : entry [5 ],
109
+ 'path' : entry [- 1 ]
110
+ }
111
+
112
+ if checker == 0 :
113
+ json_data = json_data + json .dumps (dic )
114
+ checker = 1
115
+ else :
116
+ json_data = json_data + ',' + json .dumps (dic )
117
+
118
+ json_data = json_data + ']'
119
+ final_json = json .dumps ({'status' : 1 , 'error_message' : "None" , "data" : json_data })
120
+ return HttpResponse (final_json )
121
+
122
+ except BaseException , msg :
123
+ logging .writeToFile (str (msg ))
124
+
125
+ ####
54
126
55
127
def getAWSData (self ):
56
128
key = self .backupDestinations .split ('/' )[- 1 ]
57
129
path = '/home/cyberpanel/aws/%s' % (key )
58
130
secret = open (path , 'r' ).read ()
59
131
return key , secret
60
132
61
- def awsFunction (self , fType , backupPath = None , snapshotID = None , bType = None ):
133
+ def awsFunction (self , fType , backupPath = None , snapshotID = None , bType = None ):
62
134
try :
63
135
if fType == 'backup' :
64
136
key , secret = self .getAWSData ()
65
137
command = 'export AWS_ACCESS_KEY_ID=%s AWS_SECRET_ACCESS_KEY=%s && restic -r s3:s3.amazonaws.com/%s backup %s --password-file %s' % (
66
- key , secret , self .website .domain , backupPath , self .passwordFile )
138
+ key , secret , self .website .domain , backupPath , self .passwordFile )
67
139
result = ProcessUtilities .outputExecutioner (command )
68
140
logging .statusWriter (self .statusPath , result , 1 )
69
141
snapShotid = result .split (' ' )[- 2 ]
70
142
if bType == 'database' :
71
- newSnapshot = JobSnapshots (job = self .jobid , type = '%s:%s' % (bType , backupPath .split ('/' )[- 1 ].strip ('.sql' )),
143
+ newSnapshot = JobSnapshots (job = self .jobid ,
144
+ type = '%s:%s' % (bType , backupPath .split ('/' )[- 1 ].strip ('.sql' )),
72
145
snapshotid = snapShotid ,
73
146
destination = self .backupDestinations )
74
147
else :
75
- newSnapshot = JobSnapshots (job = self .jobid , type = '%s:%s' % (bType , backupPath ), snapshotid = snapShotid ,
76
- destination = self .backupDestinations )
148
+ newSnapshot = JobSnapshots (job = self .jobid , type = '%s:%s' % (bType , backupPath ),
149
+ snapshotid = snapShotid ,
150
+ destination = self .backupDestinations )
77
151
newSnapshot .save ()
78
152
else :
79
153
self .backupDestinations = self .jobid .destination
@@ -88,9 +162,9 @@ def awsFunction(self, fType, backupPath = None, snapshotID = None, bType = None)
88
162
logging .statusWriter (self .statusPath , "%s [88][5009]" % (str (msg )), 1 )
89
163
return 0
90
164
91
- def localFunction (self , backupPath , type , restore = None ):
165
+ def localFunction (self , backupPath , type , restore = None ):
92
166
if restore == None :
93
- command = 'restic -r %s backup %s --password-file %s' % (self .repoPath , backupPath , self .passwordFile )
167
+ command = 'restic -r %s backup %s --password-file %s --exclude %s ' % (self .repoPath , backupPath , self .passwordFile , self . repoPath )
94
168
result = ProcessUtilities .outputExecutioner (command )
95
169
logging .statusWriter (self .statusPath , result , 1 )
96
170
snapShotid = result .split (' ' )[- 2 ]
@@ -107,11 +181,11 @@ def localFunction(self, backupPath, type, restore = None):
107
181
else :
108
182
repoLocation = '/home/%s/incbackup' % (self .website )
109
183
command = 'restic -r %s restore %s --target / --password-file %s' % (
110
- repoLocation , self .jobid .snapshotid , self .passwordFile )
184
+ repoLocation , self .jobid .snapshotid , self .passwordFile )
111
185
result = ProcessUtilities .outputExecutioner (command )
112
186
logging .statusWriter (self .statusPath , result , 1 )
113
187
114
- def sftpFunction (self , backupPath , type , restore = None ):
188
+ def sftpFunction (self , backupPath , type , restore = None ):
115
189
if restore == None :
116
190
remotePath = '/home/backup/%s' % (self .website .domain )
117
191
command = 'export PATH=${PATH}:/usr/bin && restic -r %s:%s backup %s --password-file %s --exclude %s' % (
@@ -132,7 +206,7 @@ def sftpFunction(self, backupPath, type, restore = None):
132
206
else :
133
207
repoLocation = '/home/backup/%s' % (self .website )
134
208
command = 'export PATH=${PATH}:/usr/bin && restic -r %s:%s restore %s --target / --password-file %s' % (
135
- self .jobid .destination , repoLocation , self .jobid .snapshotid , self .passwordFile )
209
+ self .jobid .destination , repoLocation , self .jobid .snapshotid , self .passwordFile )
136
210
result = ProcessUtilities .outputExecutioner (command )
137
211
logging .statusWriter (self .statusPath , result , 1 )
138
212
@@ -160,7 +234,8 @@ def restoreDatabase(self):
160
234
else :
161
235
self .awsFunction ('restore' , '' , self .jobid .snapshotid )
162
236
163
- if mysqlUtilities .mysqlUtilities .restoreDatabaseBackup (self .jobid .type .split (':' )[1 ].rstrip ('.sql' ), '/home/cyberpanel' , 'dummy' , 'dummy' ) == 0 :
237
+ if mysqlUtilities .mysqlUtilities .restoreDatabaseBackup (self .jobid .type .split (':' )[1 ].rstrip ('.sql' ),
238
+ '/home/cyberpanel' , 'dummy' , 'dummy' ) == 0 :
164
239
raise BaseException
165
240
166
241
try :
@@ -189,7 +264,6 @@ def restoreEmail(self):
189
264
def reconstructWithMeta (self ):
190
265
try :
191
266
192
-
193
267
if self .jobid .destination == 'local' :
194
268
self .localFunction ('none' , 'none' , 1 )
195
269
elif self .jobid .destination [:4 ] == 'sftp' :
@@ -219,7 +293,6 @@ def restorePoint(self):
219
293
logging .statusWriter (self .statusPath , message , 1 )
220
294
self .passwordFile = '/home/%s/%s' % (self .website , self .website )
221
295
222
-
223
296
##
224
297
225
298
if self .jobid .type [:8 ] == 'database' :
@@ -433,10 +506,11 @@ def backupData(self):
433
506
else :
434
507
self .awsFunction ('backup' , backupPath , '' , 'data' )
435
508
436
- logging .statusWriter (self .statusPath , 'Data for %s backed to %s.' % (self .website .domain , self .backupDestinations ), 1 )
509
+ logging .statusWriter (self .statusPath ,
510
+ 'Data for %s backed to %s.' % (self .website .domain , self .backupDestinations ), 1 )
437
511
return 1
438
512
except BaseException , msg :
439
- logging .statusWriter (self .statusPath ,'%s. [IncJobs.backupData.223][5009]' % str (msg ), 1 )
513
+ logging .statusWriter (self .statusPath , '%s. [IncJobs.backupData.223][5009]' % str (msg ), 1 )
440
514
return 0
441
515
442
516
def backupDatabases (self ):
@@ -461,10 +535,11 @@ def backupDatabases(self):
461
535
try :
462
536
os .remove ('/home/cyberpanel/%s.sql' % (items .dbName ))
463
537
except BaseException , msg :
464
- logging .statusWriter (self .statusPath ,'Failed to delete database: %s. [IncJobs.backupDatabases.456]' % str (msg ), 1 )
538
+ logging .statusWriter (self .statusPath ,
539
+ 'Failed to delete database: %s. [IncJobs.backupDatabases.456]' % str (msg ), 1 )
465
540
return 1
466
541
except BaseException , msg :
467
- logging .statusWriter (self .statusPath ,'%s. [IncJobs.backupDatabases.269][5009]' % str (msg ), 1 )
542
+ logging .statusWriter (self .statusPath , '%s. [IncJobs.backupDatabases.269][5009]' % str (msg ), 1 )
468
543
return 0
469
544
470
545
def emailBackup (self ):
@@ -480,11 +555,11 @@ def emailBackup(self):
480
555
else :
481
556
self .awsFunction ('backup' , backupPath , '' , 'email' )
482
557
483
-
484
- logging . statusWriter ( self . statusPath , 'Emails for %s backed to %s.' % (self .website .domain , self .backupDestinations ), 1 )
558
+ logging . statusWriter ( self . statusPath ,
559
+ 'Emails for %s backed to %s.' % (self .website .domain , self .backupDestinations ), 1 )
485
560
return 1
486
561
except BaseException , msg :
487
- logging .statusWriter (self .statusPath ,'%s. [IncJobs.emailBackup.269][5009]' % str (msg ), 1 )
562
+ logging .statusWriter (self .statusPath , '%s. [IncJobs.emailBackup.269][5009]' % str (msg ), 1 )
488
563
return 0
489
564
490
565
def metaBackup (self ):
@@ -500,11 +575,11 @@ def metaBackup(self):
500
575
else :
501
576
self .awsFunction ('backup' , backupPath , '' , 'meta' )
502
577
503
-
504
- logging . statusWriter ( self . statusPath , 'Meta for %s backed to %s.' % (self .website .domain , self .backupDestinations ), 1 )
578
+ logging . statusWriter ( self . statusPath ,
579
+ 'Meta for %s backed to %s.' % (self .website .domain , self .backupDestinations ), 1 )
505
580
return 1
506
581
except BaseException , msg :
507
- logging .statusWriter (self .statusPath ,'%s. [IncJobs.metaBackup.269][5009]' % str (msg ), 1 )
582
+ logging .statusWriter (self .statusPath , '%s. [IncJobs.metaBackup.269][5009]' % str (msg ), 1 )
508
583
return 0
509
584
510
585
def initiateRepo (self ):
@@ -518,20 +593,23 @@ def initiateRepo(self):
518
593
519
594
elif self .backupDestinations [:4 ] == 'sftp' :
520
595
remotePath = '/home/backup/%s' % (self .website .domain )
521
- command = 'export PATH=${PATH}:/usr/bin && restic init --repo %s:%s --password-file %s' % (self .backupDestinations , remotePath , self .passwordFile )
596
+ command = 'export PATH=${PATH}:/usr/bin && restic init --repo %s:%s --password-file %s' % (
597
+ self .backupDestinations , remotePath , self .passwordFile )
522
598
result = ProcessUtilities .outputExecutioner (command )
523
599
logging .statusWriter (self .statusPath , result , 1 )
524
600
else :
525
- key ,secret = self .getAWSData ()
526
- command = 'export AWS_ACCESS_KEY_ID=%s AWS_SECRET_ACCESS_KEY=%s && restic -r s3:s3.amazonaws.com/%s init --password-file %s' % (key , secret , self .website .domain , self .passwordFile )
601
+ key , secret = self .getAWSData ()
602
+ command = 'export AWS_ACCESS_KEY_ID=%s AWS_SECRET_ACCESS_KEY=%s && restic -r s3:s3.amazonaws.com/%s init --password-file %s' % (
603
+ key , secret , self .website .domain , self .passwordFile )
527
604
result = ProcessUtilities .outputExecutioner (command )
528
605
logging .statusWriter (self .statusPath , result , 1 )
529
606
return 1
530
607
531
- logging .statusWriter (self .statusPath , 'Repo %s initiated for %s.' % (self .backupDestinations , self .website .domain ), 1 )
608
+ logging .statusWriter (self .statusPath ,
609
+ 'Repo %s initiated for %s.' % (self .backupDestinations , self .website .domain ), 1 )
532
610
return 1
533
611
except BaseException , msg :
534
- logging .statusWriter (self .statusPath ,'%s. [IncJobs.initiateRepo.47][5009]' % str (msg ), 1 )
612
+ logging .statusWriter (self .statusPath , '%s. [IncJobs.initiateRepo.47][5009]' % str (msg ), 1 )
535
613
return 0
536
614
537
615
def createBackup (self ):
@@ -562,7 +640,6 @@ def createBackup(self):
562
640
command = 'chmod 600 %s' % (self .passwordFile )
563
641
ProcessUtilities .executioner (command )
564
642
565
-
566
643
if self .initiateRepo () == 0 :
567
644
return
568
645
@@ -589,6 +666,7 @@ def createBackup(self):
589
666
command = 'rm -f %s' % (metaPathNew )
590
667
ProcessUtilities .executioner (command )
591
668
except BaseException , msg :
592
- logging .statusWriter (self .statusPath ,'Failed to delete meta file: %s. [IncJobs.createBackup.591]' % str (msg ), 1 )
669
+ logging .statusWriter (self .statusPath ,
670
+ 'Failed to delete meta file: %s. [IncJobs.createBackup.591]' % str (msg ), 1 )
593
671
594
- logging .statusWriter (self .statusPath , 'Completed' , 1 )
672
+ logging .statusWriter (self .statusPath , 'Completed' , 1 )
0 commit comments