mirror of
https://github.com/usmannasir/cyberpanel.git
synced 2025-11-07 22:06:05 +01:00
backup emails rustic
This commit is contained in:
@@ -4,16 +4,11 @@ import os
|
|||||||
import sys
|
import sys
|
||||||
import time
|
import time
|
||||||
import requests
|
import requests
|
||||||
import urllib.request
|
|
||||||
|
|
||||||
|
|
||||||
import plogical.mysqlUtilities as mysqlUtilities
|
|
||||||
sys.path.append('/usr/local/CyberCP')
|
sys.path.append('/usr/local/CyberCP')
|
||||||
import django
|
import django
|
||||||
import plogical.CyberCPLogFileWriter as logging
|
import plogical.CyberCPLogFileWriter as logging
|
||||||
|
import plogical.mysqlUtilities as mysqlUtilities
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "CyberCP.settings")
|
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "CyberCP.settings")
|
||||||
try:
|
try:
|
||||||
@@ -243,7 +238,7 @@ class CPBackupsV2:
|
|||||||
try:
|
try:
|
||||||
if self.data['BackupDatabase']:
|
if self.data['BackupDatabase']:
|
||||||
self.UpdateStatus('Backing up databases..,10', CPBackupsV2.RUNNING)
|
self.UpdateStatus('Backing up databases..,10', CPBackupsV2.RUNNING)
|
||||||
if self.BackupDataBaseRustic() == 0:
|
if self.BackupDataBasesRustic() == 0:
|
||||||
self.UpdateStatus(f'Failed to create backup for databases.', CPBackupsV2.FAILED)
|
self.UpdateStatus(f'Failed to create backup for databases.', CPBackupsV2.FAILED)
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
@@ -257,7 +252,7 @@ class CPBackupsV2:
|
|||||||
|
|
||||||
if self.data['BackupEmails']:
|
if self.data['BackupEmails']:
|
||||||
self.UpdateStatus('Backing up emails..,75', CPBackupsV2.RUNNING)
|
self.UpdateStatus('Backing up emails..,75', CPBackupsV2.RUNNING)
|
||||||
if self.BackupEmails() == 0:
|
if self.BackupEmailsRustic() == 0:
|
||||||
return 0
|
return 0
|
||||||
self.UpdateStatus('Emails backup completed successfully..,85', CPBackupsV2.RUNNING)
|
self.UpdateStatus('Emails backup completed successfully..,85', CPBackupsV2.RUNNING)
|
||||||
|
|
||||||
@@ -280,31 +275,40 @@ class CPBackupsV2:
|
|||||||
self.website.BackupLock = 0
|
self.website.BackupLock = 0
|
||||||
self.website.save()
|
self.website.save()
|
||||||
|
|
||||||
def BackupDataBases(self):
|
# def BackupDataBases(self):
|
||||||
|
#
|
||||||
### This function will backup databases of the website, also need to take care of database that we need to exclude
|
# ### This function will backup databases of the website, also need to take care of database that we need to exclude
|
||||||
### excluded databases are in a list self.data['ExcludedDatabases'] only backup databases if backupdatabase check is on
|
# ### excluded databases are in a list self.data['ExcludedDatabases'] only backup databases if backupdatabase check is on
|
||||||
## For example if self.data['BackupDatabase'] is one then only run this function otherwise not
|
# ## For example if self.data['BackupDatabase'] is one then only run this function otherwise not
|
||||||
|
#
|
||||||
from plogical.mysqlUtilities import mysqlUtilities
|
# command = f'chown {self.website.externalApp}:{self.website.externalApp} {self.FinalPathRuctic}'
|
||||||
|
# ProcessUtilities.executioner(command)
|
||||||
for dbs in self.config['databases']:
|
#
|
||||||
|
# command = f'rustic init -r {self.FinalPathRuctic} --password ""'
|
||||||
### Pending: Need to only backup database present in the list of databases that need backing up
|
# ProcessUtilities.executioner(command, self.website.externalApp)
|
||||||
|
#
|
||||||
for key, value in dbs.items():
|
# command = f'chown cyberpanel:cyberpanel {self.FinalPathRuctic}'
|
||||||
print(f'DB {key}')
|
# ProcessUtilities.executioner(command)
|
||||||
|
#
|
||||||
if mysqlUtilities.createDatabaseBackup(key, self.FinalPath) == 0:
|
# from plogical.mysqlUtilities import mysqlUtilities
|
||||||
self.UpdateStatus(f'Failed to create backup for database {key}.', CPBackupsV2.RUNNING)
|
#
|
||||||
return 0
|
# for dbs in self.config['databases']:
|
||||||
|
#
|
||||||
for dbUsers in value:
|
# ### Pending: Need to only backup database present in the list of databases that need backing up
|
||||||
print(f'User: {dbUsers["user"]}, Host: {dbUsers["host"]}, Pass: {dbUsers["password"]}')
|
#
|
||||||
|
# for key, value in dbs.items():
|
||||||
|
# print(f'DB {key}')
|
||||||
|
#
|
||||||
return 1
|
# if mysqlUtilities.createDatabaseBackup(key, self.FinalPath) == 0:
|
||||||
|
# self.UpdateStatus(f'Failed to create backup for database {key}.', CPBackupsV2.RUNNING)
|
||||||
|
# return 0
|
||||||
|
#
|
||||||
|
# for dbUsers in value:
|
||||||
|
# print(f'User: {dbUsers["user"]}, Host: {dbUsers["host"]}, Pass: {dbUsers["password"]}')
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
# return 1
|
||||||
|
|
||||||
def BackupDataBasesRustic(self):
|
def BackupDataBasesRustic(self):
|
||||||
|
|
||||||
@@ -312,7 +316,14 @@ class CPBackupsV2:
|
|||||||
### excluded databases are in a list self.data['ExcludedDatabases'] only backup databases if backupdatabase check is on
|
### excluded databases are in a list self.data['ExcludedDatabases'] only backup databases if backupdatabase check is on
|
||||||
## For example if self.data['BackupDatabase'] is one then only run this function otherwise not
|
## For example if self.data['BackupDatabase'] is one then only run this function otherwise not
|
||||||
|
|
||||||
source = f'/home/{self.website.domain}'
|
command = f'chown {self.website.externalApp}:{self.website.externalApp} {self.FinalPathRuctic}'
|
||||||
|
ProcessUtilities.executioner(command)
|
||||||
|
|
||||||
|
command = f'rustic init -r {self.FinalPathRuctic} --password ""'
|
||||||
|
ProcessUtilities.executioner(command, self.website.externalApp)
|
||||||
|
|
||||||
|
command = f'chown cyberpanel:cyberpanel {self.FinalPathRuctic}'
|
||||||
|
ProcessUtilities.executioner(command)
|
||||||
|
|
||||||
from plogical.mysqlUtilities import mysqlUtilities
|
from plogical.mysqlUtilities import mysqlUtilities
|
||||||
|
|
||||||
@@ -323,44 +334,55 @@ class CPBackupsV2:
|
|||||||
for key, value in dbs.items():
|
for key, value in dbs.items():
|
||||||
print(f'DB {key}')
|
print(f'DB {key}')
|
||||||
|
|
||||||
if mysqlUtilities.createDatabaseBackup(key, self.FinalPath) == 0:
|
CurrentDBPath = f"{self.FinalPathRuctic}/{key}.sql"
|
||||||
CurrentDBPath = f"{self.FinalPath}/{key}.sql"
|
|
||||||
command = f'rustic -r {source}/rusticbackup backup {source} --password ""'
|
DBResult = mysqlUtilities.createDatabaseBackup(key, self.FinalPathRuctic)
|
||||||
|
|
||||||
|
|
||||||
|
if DBResult == 1:
|
||||||
|
|
||||||
|
command = f'chown {self.website.externalApp}:{self.website.externalApp} {CurrentDBPath}'
|
||||||
|
ProcessUtilities.executioner(command)
|
||||||
|
|
||||||
|
command = f'rustic -r {self.FinalPathRuctic} backup {CurrentDBPath} --password ""'
|
||||||
|
print(f'db command rustic: {command}')
|
||||||
ProcessUtilities.executioner(command, self.website.externalApp)
|
ProcessUtilities.executioner(command, self.website.externalApp)
|
||||||
self.UpdateStatus(f'Failed to create backup for database {key}.', CPBackupsV2.RUNNING)
|
|
||||||
return 0
|
|
||||||
|
|
||||||
for dbUsers in value:
|
for dbUsers in value:
|
||||||
print(f'User: {dbUsers["user"]}, Host: {dbUsers["host"]}, Pass: {dbUsers["password"]}')
|
print(f'User: {dbUsers["user"]}, Host: {dbUsers["host"]}, Pass: {dbUsers["password"]}')
|
||||||
|
|
||||||
|
else:
|
||||||
|
self.UpdateStatus(f'Failed to create backup for database {key}.', CPBackupsV2.RUNNING)
|
||||||
|
return 0
|
||||||
|
|
||||||
|
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
def BackupData(self):
|
# def BackupData(self):
|
||||||
|
#
|
||||||
### This function will backup data of the website, also need to take care of directories that we need to exclude
|
# ### This function will backup data of the website, also need to take care of directories that we need to exclude
|
||||||
### excluded directories are in a list self.data['ExcludedDirectories'] only backup data if backupdata check is on
|
# ### excluded directories are in a list self.data['ExcludedDirectories'] only backup data if backupdata check is on
|
||||||
## For example if self.data['BackupData'] is one then only run this function otherwise not
|
# ## For example if self.data['BackupData'] is one then only run this function otherwise not
|
||||||
|
#
|
||||||
destination = f'{self.FinalPath}/data'
|
# destination = f'{self.FinalPath}/data'
|
||||||
source = f'/home/{self.website.domain}'
|
# source = f'/home/{self.website.domain}'
|
||||||
|
#
|
||||||
## Pending add user provided folders in the exclude list
|
# ## Pending add user provided folders in the exclude list
|
||||||
|
#
|
||||||
exclude = f'--exclude=.cache --exclude=.cache --exclude=.cache --exclude=.wp-cli ' \
|
# exclude = f'--exclude=.cache --exclude=.cache --exclude=.cache --exclude=.wp-cli ' \
|
||||||
f'--exclude=backup --exclude=incbackup --exclude=incbackup --exclude=logs --exclude=lscache'
|
# f'--exclude=backup --exclude=incbackup --exclude=incbackup --exclude=logs --exclude=lscache'
|
||||||
|
#
|
||||||
command = f'mkdir -p {destination}'
|
# command = f'mkdir -p {destination}'
|
||||||
ProcessUtilities.executioner(command, 'cyberpanel')
|
# ProcessUtilities.executioner(command, 'cyberpanel')
|
||||||
|
#
|
||||||
command = f'chown {self.website.externalApp}:{self.website.externalApp} {destination}'
|
# command = f'chown {self.website.externalApp}:{self.website.externalApp} {destination}'
|
||||||
ProcessUtilities.executioner(command)
|
# ProcessUtilities.executioner(command)
|
||||||
|
#
|
||||||
command = f'rsync -av {exclude} {source}/ {destination}/'
|
# command = f'rsync -av {exclude} {source}/ {destination}/'
|
||||||
ProcessUtilities.executioner(command, self.website.externalApp)
|
# ProcessUtilities.executioner(command, self.website.externalApp)
|
||||||
|
#
|
||||||
return 1
|
# return 1
|
||||||
|
|
||||||
def BackupRustic(self):
|
def BackupRustic(self):
|
||||||
|
|
||||||
@@ -385,31 +407,49 @@ class CPBackupsV2:
|
|||||||
|
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
def BackupEmails(self):
|
def BackupEmailsRustic(self):
|
||||||
|
|
||||||
### This function will backup emails of the website, also need to take care of emails that we need to exclude
|
### This function will backup emails of the website, also need to take care of emails that we need to exclude
|
||||||
### excluded emails are in a list self.data['ExcludedEmails'] only backup data if backupemail check is on
|
### excluded emails are in a list self.data['ExcludedEmails'] only backup data if backupemail check is on
|
||||||
## For example if self.data['BackupEmails'] is one then only run this function otherwise not
|
## For example if self.data['BackupEmails'] is one then only run this function otherwise not
|
||||||
|
|
||||||
destination = f'{self.FinalPath}/emails'
|
|
||||||
source = f'/home/vmail/{self.website.domain}'
|
source = f'/home/vmail/{self.website.domain}'
|
||||||
|
|
||||||
## Pending add user provided folders in the exclude list
|
## Pending add user provided folders in the exclude list
|
||||||
|
|
||||||
exclude = f'--exclude=.cache --exclude=.cache --exclude=.cache --exclude=.wp-cli ' \
|
exclude = f' --exclude-if-present rusticbackup --exclude-if-present logs '
|
||||||
f'--exclude=backup --exclude=incbackup --exclude=incbackup --exclude=logs --exclude=lscache'
|
|
||||||
|
|
||||||
command = f'mkdir -p {destination}'
|
command = f'rustic -r {self.FinalPathRuctic} backup {source} --password "" {exclude}'
|
||||||
ProcessUtilities.executioner(command, 'cyberpanel')
|
|
||||||
|
|
||||||
command = f'chown vmail:vmail {destination}'
|
|
||||||
ProcessUtilities.executioner(command)
|
ProcessUtilities.executioner(command)
|
||||||
|
|
||||||
command = f'rsync -av {source}/ {destination}/'
|
|
||||||
ProcessUtilities.executioner(command, 'vmail')
|
|
||||||
|
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
|
# def BackupEmails(self):
|
||||||
|
#
|
||||||
|
# ### This function will backup emails of the website, also need to take care of emails that we need to exclude
|
||||||
|
# ### excluded emails are in a list self.data['ExcludedEmails'] only backup data if backupemail check is on
|
||||||
|
# ## For example if self.data['BackupEmails'] is one then only run this function otherwise not
|
||||||
|
#
|
||||||
|
# destination = f'{self.FinalPath}/emails'
|
||||||
|
# source = f'/home/vmail/{self.website.domain}'
|
||||||
|
#
|
||||||
|
# ## Pending add user provided folders in the exclude list
|
||||||
|
#
|
||||||
|
# exclude = f'--exclude=.cache --exclude=.cache --exclude=.cache --exclude=.wp-cli ' \
|
||||||
|
# f'--exclude=backup --exclude=incbackup --exclude=incbackup --exclude=logs --exclude=lscache'
|
||||||
|
#
|
||||||
|
# command = f'mkdir -p {destination}'
|
||||||
|
# ProcessUtilities.executioner(command, 'cyberpanel')
|
||||||
|
#
|
||||||
|
# command = f'chown vmail:vmail {destination}'
|
||||||
|
# ProcessUtilities.executioner(command)
|
||||||
|
#
|
||||||
|
# command = f'rsync -av {source}/ {destination}/'
|
||||||
|
# ProcessUtilities.executioner(command, 'vmail')
|
||||||
|
#
|
||||||
|
# return 1
|
||||||
|
|
||||||
def InstallRustic(self):
|
def InstallRustic(self):
|
||||||
try:
|
try:
|
||||||
|
|
||||||
@@ -460,4 +500,3 @@ if __name__ == "__main__":
|
|||||||
except:
|
except:
|
||||||
cpbuv2 = CPBackupsV2({'domain': 'cyberpanel.net', 'BasePath': '/home/backup', 'BackupDatabase': 1, 'BackupData': 1, 'BackupEmails': 1} )
|
cpbuv2 = CPBackupsV2({'domain': 'cyberpanel.net', 'BasePath': '/home/backup', 'BackupDatabase': 1, 'BackupData': 1, 'BackupEmails': 1} )
|
||||||
cpbuv2.InitiateBackup()
|
cpbuv2.InitiateBackup()
|
||||||
cpbuv2.Incrmentalback()
|
|
||||||
@@ -285,6 +285,10 @@ password=%s
|
|||||||
os.chmod(cnfPath, 0o600)
|
os.chmod(cnfPath, 0o600)
|
||||||
|
|
||||||
command = 'mysqldump --defaults-extra-file=/home/cyberpanel/.my.cnf -u %s --host=%s --port %s %s' % (mysqluser, mysqlhost, mysqlport, databaseName)
|
command = 'mysqldump --defaults-extra-file=/home/cyberpanel/.my.cnf -u %s --host=%s --port %s %s' % (mysqluser, mysqlhost, mysqlport, databaseName)
|
||||||
|
|
||||||
|
if os.path.exists(ProcessUtilities.debugPath):
|
||||||
|
logging.CyberCPLogFileWriter.writeToFile(command)
|
||||||
|
|
||||||
cmd = shlex.split(command)
|
cmd = shlex.split(command)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@@ -296,7 +300,6 @@ password=%s
|
|||||||
logging.CyberCPLogFileWriter.writeToFile(
|
logging.CyberCPLogFileWriter.writeToFile(
|
||||||
"Database: " + databaseName + "could not be backed! [createDatabaseBackup]")
|
"Database: " + databaseName + "could not be backed! [createDatabaseBackup]")
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
except subprocess.CalledProcessError as msg:
|
except subprocess.CalledProcessError as msg:
|
||||||
logging.CyberCPLogFileWriter.writeToFile(
|
logging.CyberCPLogFileWriter.writeToFile(
|
||||||
"Database: " + databaseName + "could not be backed! Error: %s. [createDatabaseBackup]" % (str(msg)))
|
"Database: " + databaseName + "could not be backed! Error: %s. [createDatabaseBackup]" % (str(msg)))
|
||||||
|
|||||||
Reference in New Issue
Block a user