Skip to content
Permalink
Browse files
Add remote backup user support for non root user. This is experimenta…
…l patch to hopefully solve #165
  • Loading branch information
meramsey committed Apr 24, 2020
1 parent e7e3f2b commit 39ee501fda0dc54f466c2c3868cfb4700104889e
Showing 3 changed files with 117 additions and 108 deletions.
@@ -48,6 +48,12 @@ <h3 class="title-hero">
</div>
</div>

<div class="form-group">
<label class="col-sm-3 control-label">{% trans "User" %}</label>
<div class="col-sm-6">
<input placeholder="{% trans "Backup server SSH User, leave empty for root." %}" type="text" class="form-control" ng-model="backupSSHPort" required>
</div>
</div>

<div class="form-group">
<label class="col-sm-3 control-label"></label>
@@ -135,7 +135,7 @@ def createLocalBackup(virtualHost, backupLogPath):
return 0, str(msg)

@staticmethod
def createBackup(virtualHost, ipAddress, backupLogPath , port):
def createBackup(virtualHost, ipAddress, backupLogPath , port='22', user='root'):
try:

backupSchedule.remoteBackupLogging(backupLogPath, "Preparing to create backup for: " + virtualHost)
@@ -152,7 +152,7 @@ def createBackup(virtualHost, ipAddress, backupLogPath , port):

backupSchedule.remoteBackupLogging(backupLogPath, "Preparing to send backup for: " + virtualHost +" to " + ipAddress)

backupSchedule.sendBackup(backupPath+".tar.gz", ipAddress, backupLogPath, port)
backupSchedule.sendBackup(backupPath+".tar.gz", ipAddress, backupLogPath, port, user)

backupSchedule.remoteBackupLogging(backupLogPath, "Backup for: " + virtualHost + " is sent to " + ipAddress)

@@ -180,7 +180,7 @@ def createBackup(virtualHost, ipAddress, backupLogPath , port):
logging.CyberCPLogFileWriter.writeToFile(str(msg) + " [backupSchedule.createBackup]")

@staticmethod
def sendBackup(backupPath, IPAddress, backupLogPath , port):
def sendBackup(backupPath, IPAddress, backupLogPath , port='22', user='root'):
try:

## IPAddress of local server
@@ -193,7 +193,7 @@ def sendBackup(backupPath, IPAddress, backupLogPath , port):
##

writeToFile = open(backupLogPath, "a")
command = "sudo scp -o StrictHostKeyChecking=no -P "+port+" -i /root/.ssh/cyberpanel " + backupPath + " root@"+IPAddress+":/home/backup/" + ipAddressLocal + "/" + time.strftime("%a-%b") + "/"
command = "sudo scp -o StrictHostKeyChecking=no -P "+port+" -i /root/.ssh/cyberpanel " + backupPath + " " + user + "@" + IPAddress+":~/backup/" + ipAddressLocal + "/" + time.strftime("%a-%b") + "/"
subprocess.call(shlex.split(command), stdout=writeToFile)

## Remove backups already sent to remote destinations
@@ -222,6 +222,10 @@ def prepare():
data = open(destinations,'r').readlines()
ipAddress = data[0].strip("\n")
port = data[1].strip("\n")
user = data[2].strip("\n")

# Set the user to root if not specified aka empty
user = user if bool(user) is not False else 'root'

## IPAddress of local server

@@ -241,16 +245,16 @@ def prepare():
"Connection to: " + ipAddress + " Failed, please resetup this destination from CyberPanel, aborting.")
return 0
else:
## Create backup dir on remote server
## Create backup dir on remote server in ~/backup

command = "sudo ssh -o StrictHostKeyChecking=no -p " + port + " -i /root/.ssh/cyberpanel root@" + ipAddress + " mkdir -p /home/backup/" + ipAddressLocal + "/" + time.strftime(
command = "sudo ssh -o StrictHostKeyChecking=no -p " + port + " -i /root/.ssh/cyberpanel " + user + "@" + ipAddress + " mkdir -p ~/backup/" + ipAddressLocal + "/" + time.strftime(
"%a-%b")
subprocess.call(shlex.split(command))
pass

for virtualHost in os.listdir("/home"):
if match(r'^[a-zA-Z0-9-]*[a-zA-Z0-9-]{0,61}[a-zA-Z0-9-](?:\.[a-zA-Z0-9-]{2,})+$', virtualHost, M | I):
backupSchedule.createBackup(virtualHost, ipAddress, backupLogPath, port)
backupSchedule.createBackup(virtualHost, ipAddress, backupLogPath, port, user)


backupSchedule.remoteBackupLogging(backupLogPath, "Remote backup job completed.\n")

0 comments on commit 39ee501

Please sign in to comment.