Skip to content

Commit 39ee501

Browse files
committed
Add remote backup user support for non root user. This is experimental patch to hopefully solve #165
1 parent e7e3f2b commit 39ee501

File tree

3 files changed

+117
-108
lines changed

3 files changed

+117
-108
lines changed

backup/templates/backup/backupDestinations.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ <h3 class="title-hero">
4848
</div>
4949
</div>
5050

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

5258
<div class="form-group">
5359
<label class="col-sm-3 control-label"></label>

plogical/backupSchedule.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ def createLocalBackup(virtualHost, backupLogPath):
135135
return 0, str(msg)
136136

137137
@staticmethod
138-
def createBackup(virtualHost, ipAddress, backupLogPath , port):
138+
def createBackup(virtualHost, ipAddress, backupLogPath , port='22', user='root'):
139139
try:
140140

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

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

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

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

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

182182
@staticmethod
183-
def sendBackup(backupPath, IPAddress, backupLogPath , port):
183+
def sendBackup(backupPath, IPAddress, backupLogPath , port='22', user='root'):
184184
try:
185185

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

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

199199
## Remove backups already sent to remote destinations
@@ -222,6 +222,10 @@ def prepare():
222222
data = open(destinations,'r').readlines()
223223
ipAddress = data[0].strip("\n")
224224
port = data[1].strip("\n")
225+
user = data[2].strip("\n")
226+
227+
# Set the user to root if not specified aka empty
228+
user = user if bool(user) is not False else 'root'
225229

226230
## IPAddress of local server
227231

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

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

251255
for virtualHost in os.listdir("/home"):
252256
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):
253-
backupSchedule.createBackup(virtualHost, ipAddress, backupLogPath, port)
257+
backupSchedule.createBackup(virtualHost, ipAddress, backupLogPath, port, user)
254258

255259

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

0 commit comments

Comments
 (0)