2019-12-10 23:04:24 +05:00
#!/usr/local/CyberCP/bin/python
2019-07-03 13:15:26 +05:00
import os . path
import sys
import django
sys . path . append ( ' /usr/local/CyberCP ' )
os . environ . setdefault ( " DJANGO_SETTINGS_MODULE " , " CyberCP.settings " )
django . setup ( )
import argparse
from plogical . processUtilities import ProcessUtilities
from random import randint
from plogical . CyberCPLogFileWriter import CyberCPLogFileWriter as logging
import re
import shutil
from dns . models import Domains , Records
from manageServices . models import PDNSStatus
from loginSystem . models import Administrator
from plogical . dnsUtilities import DNS
import MySQLdb as mysql
import MySQLdb . cursors as cursors
import shlex
import subprocess
from databases . models import Databases
2019-08-21 12:32:16 +05:00
from websiteFunctions . models import Websites , ChildDomains as CDomains
2019-07-03 13:15:26 +05:00
from plogical . vhost import vhost
from plogical . virtualHostUtilities import virtualHostUtilities
from plogical . mailUtilities import mailUtilities
from mailServer . models import EUsers
2019-11-12 13:24:43 +05:00
import time
2019-07-03 13:15:26 +05:00
class ChildDomains :
def __init__ ( self , domain , addon ) :
self . domain = domain
self . addon = addon
class cPanelImporter :
MailDir = 1
MdBox = 0
mainBackupPath = ' /home/backup/ '
def __init__ ( self , backupFile , logFile ) :
self . backupFile = backupFile
2019-07-16 23:23:16 +05:00
self . fileName = backupFile . split ( ' / ' ) [ - 1 ] . replace ( ' .tar.gz ' , ' ' )
2019-07-03 13:15:26 +05:00
self . logFile = logFile
self . PHPVersion = ' '
self . email = ' '
self . mainDomain = ' '
self . homeDir = ' '
self . documentRoot = ' '
self . mailFormat = 1
2019-11-12 13:24:43 +05:00
self . externalApp = ' '
2019-07-03 13:15:26 +05:00
2021-03-16 00:26:36 +05:00
## New
self . MainSite = [ ]
self . OtherDomains = [ ]
self . OtherDomainNames = [ ]
self . InheritPHP = ' '
def LoadDomains ( self ) :
try :
###
CompletPathToExtractedArchive = cPanelImporter . mainBackupPath + self . fileName
### Find Domain Name
import json
UserData = json . loads ( open ( ' %s /userdata/cache.json ' % ( CompletPathToExtractedArchive ) , ' r ' ) . read ( ) )
for key , value in UserData . items ( ) :
if value [ 2 ] == ' main ' :
self . MainSite = value
self . PHPVersion = value [ 9 ]
self . InheritPHP = self . PHPDecider ( )
else :
self . OtherDomainNames . append ( key )
self . OtherDomains . append ( value )
except BaseException as msg :
print ( str ( msg ) )
2019-07-03 13:15:26 +05:00
def PHPDecider ( self ) :
2019-08-03 14:53:31 +05:00
if self . PHPVersion == ' inherit ' :
2021-03-18 15:18:56 +05:00
self . PHPVersion = ' PHP 7.4 '
2019-07-03 13:15:26 +05:00
if self . PHPVersion . find ( ' 53 ' ) > - 1 :
self . PHPVersion = ' PHP 5.3 '
elif self . PHPVersion . find ( ' 54 ' ) > - 1 :
self . PHPVersion = ' PHP 5.4 '
elif self . PHPVersion . find ( ' 55 ' ) > - 1 :
self . PHPVersion = ' PHP 5.5 '
elif self . PHPVersion . find ( ' 56 ' ) > - 1 :
self . PHPVersion = ' PHP 5.6 '
elif self . PHPVersion . find ( ' 70 ' ) > - 1 :
self . PHPVersion = ' PHP 7.0 '
elif self . PHPVersion . find ( ' 71 ' ) > - 1 :
self . PHPVersion = ' PHP 7.1 '
elif self . PHPVersion . find ( ' 72 ' ) > - 1 :
self . PHPVersion = ' PHP 7.2 '
elif self . PHPVersion . find ( ' 73 ' ) > - 1 :
self . PHPVersion = ' PHP 7.3 '
2020-05-22 17:29:31 -04:00
elif self . PHPVersion . find ( ' 74 ' ) > - 1 :
self . PHPVersion = ' PHP 7.4 '
2021-03-16 00:26:36 +05:00
elif self . PHPVersion . find ( ' 80 ' ) > - 1 :
self . PHPVersion = ' PHP 8.0 '
2020-05-22 17:29:31 -04:00
2019-07-16 23:23:16 +05:00
if self . PHPVersion == ' ' :
2021-04-20 21:12:11 +05:00
if self . InheritPHP != ' ' :
self . PHPVersion = self . InheritPHP
else :
self . PHPVersion = ' PHP 7.4 '
2021-03-16 00:26:36 +05:00
return self . PHPVersion
2019-07-16 23:23:16 +05:00
2019-07-03 13:15:26 +05:00
def SetupSSL ( self , path , domain ) :
data = open ( path , ' r ' ) . readlines ( )
Key = [ ]
Cert = [ ]
KeyCheck = 1
CertCheck = 0
for items in data :
if KeyCheck == 1 and items . find ( ' -----END RSA PRIVATE KEY----- ' ) > - 1 :
KeyCheck = 0
CertCheck = 1
Key . append ( items )
continue
else :
Key . append ( items )
if CertCheck == 1 :
Cert . append ( items )
KeyPath = ' /home/cyberpanel/ %s ' % ( str ( randint ( 1000 , 9999 ) ) )
writeToFile = open ( KeyPath , ' w ' )
for items in Key :
writeToFile . writelines ( items )
writeToFile . close ( )
##
CertPath = ' /home/cyberpanel/ %s ' % ( str ( randint ( 1000 , 9999 ) ) )
writeToFile = open ( CertPath , ' w ' )
for items in Cert :
writeToFile . writelines ( items )
writeToFile . close ( )
virtualHostUtilities . saveSSL ( domain , KeyPath , CertPath )
def ExtractBackup ( self ) :
try :
message = ' Extracting main cPanel archive file: %s ' % ( self . backupFile )
logging . statusWriter ( self . logFile , message , 1 )
if not os . path . exists ( cPanelImporter . mainBackupPath ) :
os . mkdir ( cPanelImporter . mainBackupPath )
os . chdir ( cPanelImporter . mainBackupPath )
command = ' tar -xf %s --directory %s ' % ( self . backupFile , cPanelImporter . mainBackupPath )
ProcessUtilities . normalExecutioner ( command )
message = ' %s successfully extracted. ' % ( self . backupFile )
logging . statusWriter ( self . logFile , message , 1 )
return 1
2019-12-10 15:09:10 +05:00
except BaseException as msg :
2019-07-03 13:15:26 +05:00
message = ' Failed to extract backup for file %s , error message: %s . [ExtractBackup] ' % (
self . backupFile , str ( msg ) )
logging . statusWriter ( self . logFile , message , 1 )
return 0
def CreateMainWebsite ( self ) :
try :
message = ' Creating main account from archive file: %s ' % ( self . backupFile )
logging . statusWriter ( self . logFile , message , 1 )
2021-03-16 00:26:36 +05:00
## Paths
DomainName = self . MainSite [ 3 ]
self . mainDomain = DomainName
2019-07-03 13:15:26 +05:00
CompletPathToExtractedArchive = cPanelImporter . mainBackupPath + self . fileName
2021-03-16 00:26:36 +05:00
DomainMeta = ' %s /userdata/ %s ' % ( CompletPathToExtractedArchive , DomainName )
2019-07-03 13:15:26 +05:00
### Find Domain Name
message = ' Detected main domain for this file is: %s . ' % ( DomainName )
logging . statusWriter ( self . logFile , message , 1 )
## Find PHP Version
message = ' Finding PHP version for %s . ' % ( DomainName )
logging . statusWriter ( self . logFile , message , 1 )
2021-03-16 00:26:36 +05:00
self . PHPVersion = self . MainSite [ 9 ]
self . PHPDecider ( )
2019-07-16 23:23:16 +05:00
2019-07-03 13:15:26 +05:00
message = ' PHP version of %s is %s . ' % ( DomainName , self . PHPVersion )
logging . statusWriter ( self . logFile , message , 1 )
## Find Email
message = ' Finding Server Admin email for %s . ' % ( DomainName )
logging . statusWriter ( self . logFile , message , 1 )
data = open ( DomainMeta , ' r ' ) . readlines ( )
for items in data :
if items . find ( ' serveradmin ' ) > - 1 :
2019-07-16 23:23:16 +05:00
self . email = items . split ( ' ' ) [ - 1 ] . replace ( ' \n ' , ' ' )
2019-07-03 13:15:26 +05:00
break
message = ' Server Admin email for %s is %s . ' % ( DomainName , self . email )
logging . statusWriter ( self . logFile , message , 1 )
## Create Site
message = ' Calling core to create %s . ' % ( DomainName )
logging . statusWriter ( self . logFile , message , 1 )
2019-11-12 13:24:43 +05:00
self . externalApp = " " . join ( re . findall ( " [a-zA-Z]+ " , DomainName ) ) [ : 7 ]
2019-07-03 13:15:26 +05:00
2019-09-29 12:55:58 +05:00
try :
counter = 0
2019-11-12 13:24:43 +05:00
while True :
tWeb = Websites . objects . get ( externalApp = self . externalApp )
self . externalApp = ' %s %s ' % ( tWeb . externalApp , str ( counter ) )
2019-09-29 12:55:58 +05:00
counter = counter + 1
2019-12-10 15:09:10 +05:00
print ( self . externalApp )
except BaseException as msg :
2019-11-12 13:24:43 +05:00
logging . statusWriter ( self . logFile , str ( msg ) , 1 )
time . sleep ( 2 )
2019-09-29 12:55:58 +05:00
2021-03-16 00:26:36 +05:00
result = virtualHostUtilities . createVirtualHost ( DomainName , self . email , self . PHPVersion , self . externalApp , 1 , 0 ,
2019-07-03 13:15:26 +05:00
0 , ' admin ' , ' Default ' , 0 )
if result [ 0 ] == 1 :
pass
else :
message = ' Failed to create main site %s from archive file: %s ' % ( DomainName , self . backupFile )
logging . statusWriter ( self . logFile , message , 1 )
return 0
message = ' Successfully created %s from core. ' % ( DomainName )
logging . statusWriter ( self . logFile , message , 1 )
### Let see if there is SSL
message = ' Detecting SSL for %s . ' % ( DomainName )
logging . statusWriter ( self . logFile , message , 1 )
SSLPath = ' %s /apache_tls/ %s ' % ( CompletPathToExtractedArchive , DomainName )
if os . path . exists ( SSLPath ) :
message = ' SSL found for %s , setting up. ' % ( DomainName )
logging . statusWriter ( self . logFile , message , 1 )
self . SetupSSL ( SSLPath , DomainName )
message = ' SSL set up OK for %s . ' % ( DomainName )
logging . statusWriter ( self . logFile , message , 1 )
else :
message = ' SSL not detected for %s , you can later issue SSL from Manage SSL in CyberPanel. ' % ( DomainName )
logging . statusWriter ( self . logFile , message , 1 )
## Document root
message = ' Restoring document root files for %s . ' % ( DomainName )
logging . statusWriter ( self . logFile , message , 1 )
2021-03-16 00:26:36 +05:00
self . homeDir = self . MainSite [ 4 ] . replace ( ' /home/ %s / ' % ( self . MainSite [ 0 ] ) , ' ' )
2019-07-03 13:15:26 +05:00
nowPath = ' /home/ %s /public_html ' % ( DomainName )
if os . path . exists ( nowPath ) :
shutil . rmtree ( nowPath )
movePath = ' %s /homedir/ %s ' % (
2021-03-16 00:26:36 +05:00
CompletPathToExtractedArchive , self . homeDir )
2019-07-03 13:15:26 +05:00
2019-08-20 02:50:35 +05:00
shutil . copytree ( movePath , nowPath , symlinks = True )
2019-07-03 13:15:26 +05:00
message = ' Main site %s created from archive file: %s ' % ( DomainName , self . backupFile )
logging . statusWriter ( self . logFile , message , 1 )
return 1
2019-12-10 15:09:10 +05:00
except BaseException as msg :
2019-07-03 13:15:26 +05:00
message = ' Failed to create main website from backup file %s , error message: %s . ' % (
self . backupFile , str ( msg ) )
logging . statusWriter ( self . logFile , message , 1 )
return 0
def CreateChildDomains ( self ) :
try :
message = ' Creating child domains from archive file: %s ' % ( self . backupFile )
logging . statusWriter ( self . logFile , message , 1 )
CompletPathToExtractedArchive = cPanelImporter . mainBackupPath + self . fileName
### Find Possible Child Domains
message = ' Finding Addon/Subdomains from backup file %s . Account main domain was %s . ' % ( self . backupFile , self . mainDomain )
logging . statusWriter ( self . logFile , message , 1 )
message = ' Following Addon/Subdomains found for backup file %s . Account main domain was %s . ' % (
self . backupFile , self . mainDomain )
logging . statusWriter ( self . logFile , message , 1 )
2021-03-16 00:26:36 +05:00
for items in self . OtherDomainNames :
print ( items )
2019-07-03 13:15:26 +05:00
## Starting Child-domains creation
message = ' Starting Addon/Subdomains creation from backup file %s . Account main domain was %s . ' % (
self . backupFile , self . mainDomain )
logging . statusWriter ( self . logFile , message , 1 )
2021-03-16 00:26:36 +05:00
counter = 0
for items in self . OtherDomainNames :
2019-07-03 13:15:26 +05:00
2019-07-16 23:23:16 +05:00
try :
2019-07-03 13:15:26 +05:00
2021-03-16 00:26:36 +05:00
message = ' Creating %s . ' % ( items )
2019-07-16 23:23:16 +05:00
logging . statusWriter ( self . logFile , message , 1 )
2019-07-03 13:15:26 +05:00
2021-03-16 00:26:36 +05:00
path = ' /home/ ' + self . mainDomain + ' / ' + items
2019-07-03 13:15:26 +05:00
2019-07-16 23:23:16 +05:00
## Find PHP Version
2019-07-03 13:15:26 +05:00
2021-03-16 00:26:36 +05:00
self . PHPVersion = self . OtherDomains [ counter ] [ 9 ]
self . PHPDecider ( )
2019-07-03 13:15:26 +05:00
2021-03-16 00:26:36 +05:00
message = ' Calling core to create %s . ' % ( items )
2019-07-03 13:15:26 +05:00
logging . statusWriter ( self . logFile , message , 1 )
2021-03-16 00:26:36 +05:00
result = virtualHostUtilities . createDomain ( self . mainDomain , items , self . PHPVersion , path , 1 , 0 ,
2019-07-16 23:23:16 +05:00
0 , ' admin ' , 0 )
2019-07-03 13:15:26 +05:00
2019-07-16 23:23:16 +05:00
if result [ 0 ] == 1 :
2021-03-16 00:26:36 +05:00
message = ' Child domain %s created from archive file: %s ' % ( items , self . backupFile )
2019-07-16 23:23:16 +05:00
logging . statusWriter ( self . logFile , message , 1 )
else :
2021-03-16 00:26:36 +05:00
message = ' Failed to create Child domain %s from archive file: %s ' % ( items , self . backupFile )
2019-07-16 23:23:16 +05:00
logging . statusWriter ( self . logFile , message , 1 )
2019-07-03 13:15:26 +05:00
2019-07-16 23:23:16 +05:00
## Setup SSL
2019-07-03 13:15:26 +05:00
2021-03-16 00:26:36 +05:00
message = ' Detecting SSL for %s . ' % ( items )
2019-07-03 13:15:26 +05:00
logging . statusWriter ( self . logFile , message , 1 )
2019-07-16 23:23:16 +05:00
2021-03-16 00:26:36 +05:00
SSLPath = ' %s /apache_tls/ %s ' % ( CompletPathToExtractedArchive , items )
2019-07-16 23:23:16 +05:00
2019-07-03 13:15:26 +05:00
if os . path . exists ( SSLPath ) :
2021-03-16 00:26:36 +05:00
message = ' SSL found for %s , setting up. ' % ( items )
2019-07-03 13:15:26 +05:00
logging . statusWriter ( self . logFile , message , 1 )
2021-03-16 00:26:36 +05:00
self . SetupSSL ( SSLPath , items )
message = ' SSL set up OK for %s . ' % ( items )
2019-07-03 13:15:26 +05:00
logging . statusWriter ( self . logFile , message , 1 )
else :
2021-03-16 00:26:36 +05:00
SSLPath = ' %s /apache_tls/ %s . %s ' % ( CompletPathToExtractedArchive , items , self . mainDomain )
2019-07-16 23:23:16 +05:00
if os . path . exists ( SSLPath ) :
2021-03-16 00:26:36 +05:00
message = ' SSL found for %s , setting up. ' % ( items )
2019-07-16 23:23:16 +05:00
logging . statusWriter ( self . logFile , message , 1 )
2021-03-16 00:26:36 +05:00
self . SetupSSL ( SSLPath , items )
message = ' SSL set up OK for %s . ' % ( items )
2019-07-16 23:23:16 +05:00
logging . statusWriter ( self . logFile , message , 1 )
else :
message = ' SSL not detected for %s , you can later issue SSL from Manage SSL in CyberPanel. ' % (
2021-03-16 00:26:36 +05:00
items )
2019-07-16 23:23:16 +05:00
logging . statusWriter ( self . logFile , message , 1 )
2019-07-03 13:15:26 +05:00
2019-07-16 23:23:16 +05:00
## Creating Document root for childs
2019-07-03 13:15:26 +05:00
2021-03-16 00:26:36 +05:00
message = ' Restoring document root files for %s . ' % ( items )
2019-07-16 23:23:16 +05:00
logging . statusWriter ( self . logFile , message , 1 )
2019-07-03 13:15:26 +05:00
2021-03-16 00:26:36 +05:00
ChildDocRoot = self . OtherDomains [ counter ] [ 4 ] . replace ( ' /home/ %s / ' % ( self . MainSite [ 0 ] ) , ' ' )
2019-07-03 13:15:26 +05:00
2019-07-16 23:23:16 +05:00
if os . path . exists ( path ) :
shutil . rmtree ( path )
2019-07-03 13:15:26 +05:00
2021-03-16 00:26:36 +05:00
movePath = ' %s /homedir/ %s ' % ( CompletPathToExtractedArchive , ChildDocRoot )
logging . statusWriter ( self . logFile , ' Document root in cPanel Backup for %s is %s ' % ( items , movePath ) , 1 )
2019-07-03 13:15:26 +05:00
2019-08-24 15:39:16 +05:00
if os . path . exists ( movePath ) :
2021-03-16 00:26:36 +05:00
shutil . copytree ( movePath , path )
2019-07-03 13:15:26 +05:00
2019-07-16 23:23:16 +05:00
message = ' Successfully created child domain. '
logging . statusWriter ( self . logFile , message , 1 )
2021-03-16 00:26:36 +05:00
counter = counter + 1
2019-12-10 15:09:10 +05:00
except BaseException as msg :
2019-07-16 23:23:16 +05:00
message = ' Failed to create child domain from backup file %s , error message: %s . Moving on.. ' % (
self . backupFile , str ( msg ) )
2021-03-16 00:26:36 +05:00
logging . statusWriter ( self . logFile , message , 1 )
counter = counter + 1
2019-07-03 13:15:26 +05:00
return 1
2019-12-10 15:09:10 +05:00
except BaseException as msg :
2019-07-03 13:15:26 +05:00
message = ' Failed to create child domain from backup file %s , error message: %s . ' % (
self . backupFile , str ( msg ) )
logging . statusWriter ( self . logFile , message , 1 )
return 0
2019-08-21 12:32:16 +05:00
def createDummyChild ( self , childDomain ) :
path = ' /home/ %s /public_html/ %s ' % ( self . mainDomain , childDomain )
virtualHostUtilities . createDomain ( self . mainDomain , childDomain , self . PHPVersion , path , 0 , 0 ,
0 , ' admin ' , 0 )
2019-07-03 13:15:26 +05:00
def CreateDNSRecords ( self ) :
try :
message = ' We are going to create DNS records now, please note we will not create DKIM records. Make sure to create them from CyberPanel interface using our DKIM manager. '
logging . statusWriter ( self . logFile , message , 1 )
ipFile = " /etc/cyberpanel/machineIP "
f = open ( ipFile )
ipData = f . read ( )
ipAddress = ipData . split ( ' \n ' , 1 ) [ 0 ]
2020-01-29 11:05:47 +05:00
admin = Administrator . objects . get ( userName = ' admin ' )
2019-07-03 13:15:26 +05:00
CompletPathToExtractedArchive = cPanelImporter . mainBackupPath + self . fileName
DNSZonesPath = ' %s /dnszones ' % ( CompletPathToExtractedArchive )
for items in os . listdir ( DNSZonesPath ) :
topLevelDomain = items . replace ( ' .db ' , ' ' , 1 )
message = ' Creating DNS records for %s ' % ( topLevelDomain )
logging . statusWriter ( self . logFile , message , 1 )
try :
Domains . objects . get ( name = topLevelDomain ) . delete ( )
except :
pass
try :
pdns = PDNSStatus . objects . get ( pk = 1 )
if pdns . type == ' MASTER ' :
zone = Domains ( admin = admin , name = topLevelDomain , type = " MASTER " )
zone . save ( )
else :
zone = Domains ( admin = admin , name = topLevelDomain , type = " NATIVE " )
zone . save ( )
except :
2021-12-01 13:18:33 +05:00
zone = Domains ( admin = admin , name = topLevelDomain , type = " NATIVE " )
zone . save ( )
2019-07-03 13:15:26 +05:00
pass
content = " ns1. " + topLevelDomain + " hostmaster. " + topLevelDomain + " 1 10800 3600 604800 3600 "
soaRecord = Records ( domainOwner = zone ,
domain_id = zone . id ,
name = topLevelDomain ,
type = " SOA " ,
content = content ,
ttl = 3600 ,
prio = 0 ,
disabled = 0 ,
auth = 1 )
soaRecord . save ( )
CurrentZonePath = ' %s / %s ' % ( DNSZonesPath , items )
data = open ( CurrentZonePath , ' r ' ) . readlines ( )
SOACheck = 0
start = 0
for items in data :
2019-07-24 22:37:37 +05:00
try :
if items . find ( ' SOA ' ) > - 1 :
SOACheck = 1
continue
2019-07-03 13:15:26 +05:00
2019-07-24 22:37:37 +05:00
if SOACheck == 1 and items . find ( ' ) ' ) > - 1 :
SOACheck = 0
start = 1
continue
else :
pass
if start == 1 :
if len ( items ) > 3 :
if items . find ( " DKIM1 " ) > - 1 :
continue
RecordsData = items . split ( ' \t ' )
if RecordsData [ 3 ] == ' A ' :
RecordsData [ 4 ] = ipAddress
if RecordsData [ 0 ] . find ( topLevelDomain ) > - 1 :
2019-08-17 21:14:37 +05:00
if RecordsData [ 3 ] == ' MX ' :
DNS . createDNSRecord ( zone , RecordsData [ 0 ] . rstrip ( ' . ' ) , RecordsData [ 3 ] , RecordsData [ 5 ] . rstrip ( ' . ' ) . rstrip ( ' . \n ' ) , int ( RecordsData [ 4 ] ) , RecordsData [ 1 ] )
else :
DNS . createDNSRecord ( zone , RecordsData [ 0 ] . rstrip ( ' . ' ) , RecordsData [ 3 ] , RecordsData [ 4 ] . rstrip ( ' . ' ) . rstrip ( ' . \n ' ) , 0 , RecordsData [ 1 ] )
2019-07-24 22:37:37 +05:00
else :
2019-08-17 21:14:37 +05:00
if RecordsData [ 3 ] == ' MX ' :
DNS . createDNSRecord ( zone , RecordsData [ 0 ] + ' . ' + topLevelDomain , RecordsData [ 3 ] ,
RecordsData [ 5 ] . rstrip ( ' . ' ) . rstrip ( ' . \n ' ) , RecordsData [ 4 ] ,
RecordsData [ 1 ] )
else :
DNS . createDNSRecord ( zone , RecordsData [ 0 ] + ' . ' + topLevelDomain , RecordsData [ 3 ] , RecordsData [ 4 ] . rstrip ( ' . ' ) . rstrip ( ' . \n ' ) , 0 ,
RecordsData [ 1 ] )
2019-12-10 15:09:10 +05:00
except BaseException as msg :
2019-07-24 22:37:37 +05:00
message = ' Failed while creating DNS entry for %s , error message: %s . ' % ( topLevelDomain , str ( msg ) )
logging . statusWriter ( self . logFile , message , 1 )
2019-07-03 13:15:26 +05:00
message = ' DNS records successfully created for %s . ' % ( topLevelDomain )
logging . statusWriter ( self . logFile , message , 1 )
return 1
2019-12-10 15:09:10 +05:00
except BaseException as msg :
2019-07-03 13:15:26 +05:00
message = ' Failed to create DNS records from file %s , error message: %s . ' % (
self . backupFile , str ( msg ) )
logging . statusWriter ( self . logFile , message , 1 )
return 0
def setupConnection ( self , db = None ) :
try :
passFile = " /etc/cyberpanel/mysqlPassword "
f = open ( passFile )
data = f . read ( )
password = data . split ( ' \n ' , 1 ) [ 0 ]
2019-07-16 23:23:16 +05:00
password = password . replace ( ' \n ' , ' ' ) . replace ( ' \r ' , ' ' )
2019-07-03 13:15:26 +05:00
conn = mysql . connect ( user = ' root ' , passwd = password , cursorclass = cursors . SSCursor )
cursor = conn . cursor ( )
return conn , cursor
2019-12-10 15:09:10 +05:00
except BaseException as msg :
2019-07-03 13:15:26 +05:00
message = ' Failed to connect to database, error message: %s . [ExtractBackup] ' % ( str ( msg ) )
logging . statusWriter ( self . logFile , message , 1 )
return 0 , 0
def RestoreDatabases ( self ) :
try :
message = ' Restoring databases from %s . ' % ( self . backupFile )
logging . statusWriter ( self . logFile , message , 1 )
##
passFile = " /etc/cyberpanel/mysqlPassword "
f = open ( passFile )
data = f . read ( )
password = data . split ( ' \n ' , 1 ) [ 0 ]
##
connection , cursor = self . setupConnection ( )
CompletPathToExtractedArchive = cPanelImporter . mainBackupPath + self . fileName
DatabasesPath = ' %s /mysql ' % ( CompletPathToExtractedArchive )
for items in os . listdir ( DatabasesPath ) :
2019-08-25 17:40:29 +05:00
if items . find ( ' roundcube ' ) > - 1 :
continue
2019-07-03 13:15:26 +05:00
if items . endswith ( ' .sql ' ) :
2019-07-16 23:23:16 +05:00
message = ' Restoring MySQL dump for %s . ' % ( items . replace ( ' .sql ' , ' ' ) )
2019-07-03 13:15:26 +05:00
logging . statusWriter ( self . logFile , message , 1 )
try :
2019-09-08 20:32:20 +05:00
cursor . execute ( " CREATE DATABASE ` %s ` " % ( items . replace ( ' .sql ' , ' ' ) ) )
2019-12-10 15:09:10 +05:00
except BaseException as msg :
2019-07-24 22:37:37 +05:00
message = ' Failed while restoring database %s from backup file %s , error message: %s ' % ( items . replace ( ' .sql ' , ' ' ) , self . backupFile , str ( msg ) )
2019-07-03 13:15:26 +05:00
logging . statusWriter ( self . logFile , message , 1 )
2019-07-16 23:23:16 +05:00
command = ' sudo mysql -u root -p ' + password + ' ' + items . replace ( ' .sql ' , ' ' )
2019-07-03 13:15:26 +05:00
cmd = shlex . split ( command )
DBPath = " %s / %s " % ( DatabasesPath , items )
with open ( DBPath , ' r ' ) as f :
res = subprocess . call ( cmd , stdin = f )
website = Websites . objects . get ( domain = self . mainDomain )
2019-09-08 20:32:20 +05:00
## Trying to figure out dbname
CommandsPath = ' %s /mysql.sql ' % ( CompletPathToExtractedArchive )
data = open ( CommandsPath , ' r ' ) . readlines ( )
for inItems in data :
if inItems . find ( ' GRANT ALL PRIVILEGES ' ) > - 1 and inItems . find ( ' localhost ' ) > - 1 and inItems . find ( ' _test ' ) == - 1 :
cDBName = inItems . split ( ' ` ' ) [ 1 ] . replace ( ' \\ ' , ' ' )
logging . statusWriter ( self . logFile , inItems , 1 )
if cDBName == items . replace ( ' .sql ' , ' ' ) :
2020-06-21 23:29:28 -03:00
cDBUser = inItems . replace ( " ` " , " ' " ) . replace ( " \\ " , " " ) . split ( " ' " ) [ 1 ]
2019-09-08 20:32:20 +05:00
message = ' Database user for %s is %s . ' % ( cDBName , cDBUser )
logging . statusWriter ( self . logFile , message , 1 )
if Databases . objects . filter ( dbUser = cDBUser ) . count ( ) > 0 :
continue
break
db = Databases ( website = website , dbName = items . replace ( ' .sql ' , ' ' ) , dbUser = cDBUser )
2019-07-03 13:15:26 +05:00
db . save ( )
2019-07-16 23:23:16 +05:00
message = ' MySQL dump successfully restored for %s . ' % ( items . replace ( ' .sql ' , ' ' ) )
2019-07-03 13:15:26 +05:00
logging . statusWriter ( self . logFile , message , 1 )
message = ' Creating Database users from backup file %s . ' % ( self . backupFile )
logging . statusWriter ( self . logFile , message , 1 )
CommandsPath = ' %s /mysql.sql ' % ( CompletPathToExtractedArchive )
data = open ( CommandsPath , ' r ' ) . readlines ( )
for items in data :
if items . find ( " -- " ) > - 1 or items . find ( " ' cyberpanel ' @ " ) > - 1 :
continue
try :
cursor . execute ( items )
2019-12-10 15:09:10 +05:00
except BaseException as msg :
2019-07-24 22:37:37 +05:00
message = ' Failed while restoring database %s from backup file %s , error message: %s ' % (
2019-07-16 23:23:16 +05:00
items . replace ( ' .sql ' , ' ' ) , self . backupFile , str ( msg ) )
2019-07-03 13:15:26 +05:00
logging . statusWriter ( self . logFile , message , 1 )
connection . close ( )
message = ' Databases successfully restored. '
logging . statusWriter ( self . logFile , message , 1 )
return 1
2019-12-10 15:09:10 +05:00
except BaseException as msg :
2020-04-04 23:23:59 +02:00
message = ' Failed to restore databases from file %s , error message: %s . ' % (
2019-07-03 13:15:26 +05:00
self . backupFile , str ( msg ) )
logging . statusWriter ( self . logFile , message , 1 )
return 0
def FixPermissions ( self ) :
2020-04-07 12:58:07 +05:00
from filemanager . filemanager import FileManager
fm = FileManager ( None , None )
fm . fixPermissions ( self . mainDomain )
2020-06-18 04:17:49 +05:30
def createCronJobs ( self ) :
try :
message = ' Restoring cron jobs from %s . ' % ( self . backupFile )
logging . statusWriter ( self . logFile , message , 1 )
CompletPathToExtractedArchive = cPanelImporter . mainBackupPath + self . fileName
cronPath = ' %s /cron ' % ( CompletPathToExtractedArchive )
if len ( os . listdir ( cronPath ) ) == 0 :
message = ' No Cron Job file found. '
logging . statusWriter ( self . logFile , message , 1 )
return 1
if ProcessUtilities . decideDistro ( ) == ProcessUtilities . centos or ProcessUtilities . decideDistro ( ) == ProcessUtilities . cent8 :
localCronPath = " /var/spool/cron/ " + self . externalApp
else :
localCronPath = " /var/spool/cron/crontabs/ " + self . externalApp
2020-06-21 05:03:39 +05:30
localCronFile = open ( localCronPath , " a+ " )
2020-06-18 04:17:49 +05:30
commandT = ' touch %s ' % ( localCronPath )
ProcessUtilities . executioner ( commandT , ' root ' )
commandT = ' chown %s : %s %s ' % ( self . externalApp , self . externalApp , localCronPath )
ProcessUtilities . executioner ( commandT , ' root ' )
# There's only single file usually but running for all found
for item in os . listdir ( cronPath ) :
cronFile = open ( ' %s / %s ' % ( cronPath , item ) , ' r ' )
cronJobs = cronFile . readlines ( )
# Filter actual jobs and remove variables and last new line character
for job in cronJobs :
if len ( job . split ( ' ' ) ) > 1 :
# Valid enough, add it to user
localCronFile . write ( job )
message = ' Cron Jobs successfully restored. '
logging . statusWriter ( self . logFile , message , 1 )
return 1
except BaseException as msg :
message = ' Failed to restore Cron Jobs from file %s , error message: %s . ' % (
self . backupFile , str ( msg ) )
logging . statusWriter ( self . logFile , message , 1 )
return 0
2019-07-03 13:15:26 +05:00
def DeleteSite ( self ) :
vhost . deleteVirtualHostConfigurations ( self . mainDomain )
2019-08-21 12:32:16 +05:00
def checkIfExists ( self , virtualHostName ) :
if Websites . objects . filter ( domain = virtualHostName ) . count ( ) > 0 :
return 1
if CDomains . objects . filter ( domain = virtualHostName ) . count ( ) > 0 :
return 1
return 0
2019-07-03 13:15:26 +05:00
def RestoreEmails ( self ) :
try :
message = ' Restoring emails from archive file: %s ' % ( self . backupFile )
logging . statusWriter ( self . logFile , message , 1 )
CompletPathToExtractedArchive = cPanelImporter . mainBackupPath + self . fileName
### Find Mail Format
UserData = ' %s /homedir/mail ' % ( CompletPathToExtractedArchive )
FormatPath = ' %s /mailbox_format.cpanel ' % ( UserData )
message = ' Detecting email format from %s . ' % ( self . backupFile )
logging . statusWriter ( self . logFile , message , 1 )
2019-08-13 16:27:56 +05:00
try :
2019-07-03 13:15:26 +05:00
2019-08-13 16:27:56 +05:00
Format = open ( FormatPath , ' r ' ) . read ( )
if Format . find ( ' mdbox ' ) > - 1 :
self . mailFormat = cPanelImporter . MdBox
message = ' Mdbox format detected from %s . ' % ( self . backupFile )
logging . statusWriter ( self . logFile , message , 1 )
else :
self . mailFormat = cPanelImporter . MailDir
message = ' Maildir format detected from %s . ' % ( self . backupFile )
logging . statusWriter ( self . logFile , message , 1 )
except :
2019-07-03 13:15:26 +05:00
self . mailFormat = cPanelImporter . MailDir
####
for items in os . listdir ( UserData ) :
FinalMailDomainPath = ' %s / %s ' % ( UserData , items )
if os . path . isdir ( FinalMailDomainPath ) :
if items [ 0 ] == ' . ' :
continue
if items . find ( ' . ' ) > - 1 :
for it in os . listdir ( FinalMailDomainPath ) :
2019-08-21 12:32:16 +05:00
try :
if self . checkIfExists ( items ) == 0 :
self . createDummyChild ( items )
2019-07-03 13:15:26 +05:00
2019-08-21 12:32:16 +05:00
mailUtilities . createEmailAccount ( items , it , ' cyberpanel ' )
finalEmailUsername = it + " @ " + items
message = ' Starting restore for %s . ' % ( finalEmailUsername )
logging . statusWriter ( self . logFile , message , 1 )
eUser = EUsers . objects . get ( email = finalEmailUsername )
2019-07-03 13:15:26 +05:00
2019-08-21 12:32:16 +05:00
if self . mailFormat == cPanelImporter . MailDir :
eUser . mail = ' maildir:/home/vmail/ %s / %s /Maildir ' % ( items , it )
2019-08-23 01:18:16 +05:00
MailPath = ' /home/vmail/ %s / %s ' % ( items , it )
2019-07-03 13:15:26 +05:00
2019-08-21 12:32:16 +05:00
command = ' mkdir -p %s ' % ( MailPath )
ProcessUtilities . normalExecutioner ( command )
2019-07-03 13:15:26 +05:00
2019-08-23 01:18:16 +05:00
command = ' rm -rf %s /Maildir ' % ( MailPath )
ProcessUtilities . normalExecutioner ( command )
2019-08-21 12:32:16 +05:00
MailPathInBackup = ' %s / %s ' % ( FinalMailDomainPath , it )
2019-07-03 13:15:26 +05:00
2019-08-23 01:18:16 +05:00
command = ' mv %s %s /Maildir ' % ( MailPathInBackup , MailPath )
2019-08-21 12:32:16 +05:00
subprocess . call ( command , shell = True )
2019-07-03 13:15:26 +05:00
2019-08-21 12:32:16 +05:00
else :
eUser . mail = ' mdbox:/home/vmail/ %s / %s /Mdbox ' % ( items , it )
2019-08-23 01:18:16 +05:00
MailPath = ' /home/vmail/ %s / %s ' % ( items , it )
2019-07-03 13:15:26 +05:00
2019-08-21 12:32:16 +05:00
command = ' mkdir -p %s ' % ( MailPath )
ProcessUtilities . normalExecutioner ( command )
2019-07-03 13:15:26 +05:00
2019-08-23 01:18:16 +05:00
command = ' rm -rf %s /Mdbox ' % ( MailPath )
ProcessUtilities . normalExecutioner ( command )
2019-08-21 12:32:16 +05:00
MailPathInBackup = ' %s / %s ' % ( FinalMailDomainPath , it )
2019-07-03 13:15:26 +05:00
2019-08-23 01:18:16 +05:00
command = ' mv %s %s /Mdbox ' % ( MailPathInBackup , MailPath )
2019-08-21 12:32:16 +05:00
subprocess . call ( command , shell = True )
2019-07-03 13:15:26 +05:00
2019-08-21 12:32:16 +05:00
## Also update password
2019-07-03 13:15:26 +05:00
2019-08-21 12:32:16 +05:00
PasswordPath = ' %s /homedir/etc/ %s /shadow ' % ( CompletPathToExtractedArchive , items )
PasswordData = open ( PasswordPath , ' r ' ) . readlines ( )
2019-07-03 13:15:26 +05:00
2019-08-21 12:32:16 +05:00
for i in PasswordData :
if i . find ( it ) > - 1 :
finalPassword = ' %s %s ' % ( ' {CRYPT} ' , i . split ( ' : ' ) [ 1 ] )
eUser . password = finalPassword
2019-07-03 13:15:26 +05:00
2019-08-21 12:32:16 +05:00
eUser . save ( )
message = ' Restore completed for %s . ' % ( finalEmailUsername )
logging . statusWriter ( self . logFile , message , 1 )
2019-12-10 15:09:10 +05:00
except BaseException as msg :
2019-08-21 12:32:16 +05:00
message = ' Failed to restore emails from archive file %s , For domain: %s . error message: %s . [ExtractBackup] ' % (
self . backupFile , items , str ( msg ) )
logging . statusWriter ( self . logFile , message , 1 )
2019-07-03 13:15:26 +05:00
command = ' chown -R vmail:vmail /home/vmail '
ProcessUtilities . normalExecutioner ( command )
message = ' Emails successfully restored '
logging . statusWriter ( self . logFile , message , 1 )
return 1
2019-12-10 15:09:10 +05:00
except BaseException as msg :
2019-07-03 13:15:26 +05:00
message = ' Failed to restore emails from archive file %s , error message: %s . [ExtractBackup] ' % (
self . backupFile , str ( msg ) )
logging . statusWriter ( self . logFile , message , 1 )
return 0
2021-03-16 00:26:36 +05:00
def MainController ( self ) :
if self . ExtractBackup ( ) :
pass
else :
return
self . LoadDomains ( )
if self . CreateMainWebsite ( ) :
pass
else :
return 0
if self . CreateChildDomains ( ) :
pass
else :
return 0
if self . CreateDNSRecords ( ) :
pass
else :
return 0
if self . RestoreDatabases ( ) :
pass
else :
return 0
if self . createCronJobs ( ) :
pass
else :
return 0
self . RestoreEmails ( )
self . FixPermissions ( )
message = ' Backup file %s successfully restored. ' % ( self . backupFile )
logging . statusWriter ( self . logFile , message , 1 )
return 1
2019-07-03 13:15:26 +05:00
def main ( ) :
LogFile = ' /home/cyberpanel/ %s ' % ( str ( randint ( 1000 , 9999 ) ) )
message = ' Backup logs to be generated in %s ' % ( LogFile )
print ( message )
parser = argparse . ArgumentParser ( description = ' CyberPanel cPanel Importer ' )
parser . add_argument ( ' --path ' , help = ' Path where cPanel .tar.gz files are stored. ' )
args = parser . parse_args ( )
for items in os . listdir ( args . path ) :
2019-07-16 23:23:16 +05:00
if items . endswith ( ' .tar.gz ' ) :
finalPath = ' %s / %s ' % ( args . path . rstrip ( ' / ' ) , items )
2019-07-24 22:37:37 +05:00
try :
cI = cPanelImporter ( finalPath , LogFile )
if cI . MainController ( ) :
pass
else :
pass
except :
2019-07-16 23:23:16 +05:00
pass
2019-07-03 13:15:26 +05:00
if __name__ == " __main__ " :
main ( )