add code for remote backups on sft

This commit is contained in:
Usman Nasir
2020-09-27 21:41:50 +05:00
parent 640ef4431f
commit c254e8fa58
2 changed files with 102 additions and 37 deletions

View File

@@ -368,26 +368,30 @@ class IncScheduler():
if destinationConfig['type'] == 'local': if destinationConfig['type'] == 'local':
finalPath = '%s/%s' % (destinationConfig['path'].rstrip('/'), currentTime) finalPath = '%s/%s' % (destinationConfig['path'].rstrip('/'), currentTime)
command = 'mkdir %s' % (finalPath) command = 'mkdir -p %s' % (finalPath)
ProcessUtilities.executioner(command) ProcessUtilities.executioner(command)
if jobConfig[IncScheduler.frequency] == type: if jobConfig[IncScheduler.frequency] == type:
NormalBackupJobLogs.objects.filter(owner=backupjob).delete() NormalBackupJobLogs.objects.filter(owner=backupjob).delete()
NormalBackupJobLogs(owner=backupjob, status=backupSchedule.INFO, NormalBackupJobLogs(owner=backupjob, status=backupSchedule.INFO,
message='Starting %s backup on %s..' % (type, time.strftime("%m.%d.%Y_%H-%M-%S"))).save() message='Starting %s backup on %s..' % (type, time.strftime("%m.%d.%Y_%H-%M-%S"))).save()
actualDomain = 0
try:
if jobConfig[IncScheduler.allSites] == 'all': if jobConfig[IncScheduler.allSites] == 'all':
websites = Websites.objects.all() websites = Websites.objects.all()
actualDomain = 1
else: else:
websites = backupjob.normalbackupsites_set.all() websites = backupjob.normalbackupsites_set.all()
except:
websites = backupjob.normalbackupsites_set.all()
for site in websites: for site in websites:
try:
if actualDomain:
domain = site.domain domain = site.domain
except: else:
domain = site.domain.domain domain = site.domain.domain
retValues = backupSchedule.createLocalBackup(domain, '/dev/null') retValues = backupSchedule.createLocalBackup(domain, '/dev/null')
@@ -420,6 +424,67 @@ Automatic backup failed for %s on %s.
NormalBackupJobLogs(owner=backupjob, status=backupSchedule.INFO, NormalBackupJobLogs(owner=backupjob, status=backupSchedule.INFO,
message='Backup completed for %s on %s.' % ( message='Backup completed for %s on %s.' % (
domain, time.strftime("%m.%d.%Y_%H-%M-%S"))).save() domain, time.strftime("%m.%d.%Y_%H-%M-%S"))).save()
else:
import subprocess
import shlex
finalPath = '%s/%s' % (destinationConfig['path'].rstrip('/'), currentTime)
command = "ssh -o StrictHostKeyChecking=no -p " + destinationConfig['port'] + " -i /root/.ssh/cyberpanel " + destinationConfig['username'] + "@" + destinationConfig['ip'] + " mkdir -p %s" % (finalPath)
subprocess.call(shlex.split(command))
if jobConfig[IncScheduler.frequency] == type:
NormalBackupJobLogs.objects.filter(owner=backupjob).delete()
NormalBackupJobLogs(owner=backupjob, status=backupSchedule.INFO,
message='Starting %s backup on %s..' % (
type, time.strftime("%m.%d.%Y_%H-%M-%S"))).save()
actualDomain = 0
try:
if jobConfig[IncScheduler.allSites] == 'all':
websites = Websites.objects.all()
actualDomain = 1
else:
websites = backupjob.normalbackupsites_set.all()
except:
websites = backupjob.normalbackupsites_set.all()
for site in websites:
if actualDomain:
domain = site.domain
else:
domain = site.domain.domain
retValues = backupSchedule.createLocalBackup(domain, '/dev/null')
if retValues[0] == 0:
NormalBackupJobLogs(owner=backupjob, status=backupSchedule.ERROR,
message='Backup failed for %s on %s.' % (
domain, time.strftime("%m.%d.%Y_%H-%M-%S"))).save()
SUBJECT = "Automatic backup failed for %s on %s." % (domain, currentTime)
adminEmailPath = '/home/cyberpanel/adminEmail'
adminEmail = open(adminEmailPath, 'r').read().rstrip('\n')
sender = 'root@%s' % (socket.gethostname())
TO = [adminEmail]
message = """\
From: %s
To: %s
Subject: %s
Automatic backup failed for %s on %s.
""" % (sender, ", ".join(TO), SUBJECT, domain, currentTime)
logging.SendEmail(sender, TO, message)
else:
backupPath = retValues[1] + ".tar.gz"
command = "scp -o StrictHostKeyChecking=no -P " + destinationConfig['port'] + " -i /root/.ssh/cyberpanel " + backupPath + " " + destinationConfig['username'] + "@" + destinationConfig['ip'] + ":%s" % (finalPath)
ProcessUtilities.executioner(command)
NormalBackupJobLogs(owner=backupjob, status=backupSchedule.INFO,
message='Backup completed for %s on %s.' % (
domain, time.strftime("%m.%d.%Y_%H-%M-%S"))).save()
def main(): def main():

View File

@@ -277,7 +277,7 @@ class backupSchedule:
## ##
writeToFile = open(backupLogPath, "a") writeToFile = open(backupLogPath, "a")
command = "sudo scp -o StrictHostKeyChecking=no -P "+port+" -i /root/.ssh/cyberpanel " + backupPath + " " + user + "@" + IPAddress+":~/backup/" + ipAddressLocal + "/" + time.strftime("%a-%b") + "/" command = "scp -o StrictHostKeyChecking=no -P "+port+" -i /root/.ssh/cyberpanel " + backupPath + " " + user + "@" + IPAddress+":~/backup/" + ipAddressLocal + "/" + time.strftime("%m.%d.%Y_%H-%M-%S") + "/"
subprocess.call(shlex.split(command), stdout=writeToFile) subprocess.call(shlex.split(command), stdout=writeToFile)
if os.path.exists(ProcessUtilities.debugPath): if os.path.exists(ProcessUtilities.debugPath):
@@ -366,7 +366,7 @@ class backupSchedule:
else: else:
## Create backup dir on remote server in ~/backup ## Create backup dir on remote server in ~/backup
command = "sudo ssh -o StrictHostKeyChecking=no -p " + port + " -i /root/.ssh/cyberpanel " + user + "@" + ipAddress + " mkdir -p ~/backup/" + ipAddressLocal + "/" + time.strftime( command = "ssh -o StrictHostKeyChecking=no -p " + port + " -i /root/.ssh/cyberpanel " + user + "@" + ipAddress + " mkdir -p ~/backup/" + ipAddressLocal + "/" + time.strftime(
"%a-%b") "%a-%b")
subprocess.call(shlex.split(command)) subprocess.call(shlex.split(command))
pass pass