Skip to content

Commit

Permalink
Working backup and restore scripts (Issue #606)
Browse files Browse the repository at this point in the history
  • Loading branch information
failys committed Feb 12, 2021
1 parent 795da3b commit 4feef4c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 11 deletions.
6 changes: 5 additions & 1 deletion cairis/bin/backup_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from cairis.core.ARM import ARMException
from cairis.core.Borg import Borg
import cairis.core.BorgFactory
from cairis.core.dba import accounts,canonicalDbUser,dbtoken
from cairis.core.dba import accounts,canonicalDbUser,dbtoken,emailHashes
from cairis.core.MySQLDatabaseProxy import MySQLDatabaseProxy
from cairis.mio.ModelExport import exportPackage

Expand Down Expand Up @@ -62,6 +62,10 @@ def main():
for p in os.listdir(b.tmpDir):
modelName = b.tmpDir + '/' + p
archive.add(modelName, arcname=p, filter=lambda x: x if x.name.endswith('.cairis') else None)
hashFile = b.tmpDir + '/hashes.txt'
with open(hashFile,'w') as f:
f.write('\n'.join(list(map(lambda x : ','.join(x),emailHashes(b.rPasswd,b.dbHost,b.dbPort)))))
archive.add(hashFile, arcname='hashes.txt')
if __name__ == '__main__':
try:
main()
Expand Down
25 changes: 15 additions & 10 deletions cairis/bin/restore_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from cairis.core.ARM import ARMException
from cairis.core.Borg import Borg
import cairis.core.BorgFactory
from cairis.core.dba import accounts,canonicalDbUser,dbtoken
from cairis.core.dba import accounts,canonicalDbUser,dbtoken,updateEmailHashes
from cairis.core.MySQLDatabaseProxy import MySQLDatabaseProxy
from cairis.mio.ModelExport import exportPackage
from cairis.bin.add_cairis_user import addCairisUser
Expand All @@ -46,25 +46,30 @@ def main():

modelFiles = []
models = []
hashes = []
with tarfile.open(args.archiveFile,'r') as archive:
modelFiles = archive.getmembers()
for modelFile in modelFiles:
accountName = modelFile.name.split('.cairis')[0]
try:
addCairisUser(accountName,accountName,accountName)
models.append((accountName,archive.extractfile(modelFile).read()))
logger.info('Re-created account ' + accountName)
except ARMException as ex:
logger.info('Error re-creating account ' + accountName + ' : ' + str(ex))

if (modelFile.name != 'hashes.txt'):
accountName = modelFile.name.split('.cairis')[0]
try:
addCairisUser(accountName,accountName,accountName)
models.append((accountName,archive.extractfile(modelFile).read()))
logger.info('Re-created account ' + accountName)
except ARMException as ex:
logger.info('Error re-creating account ' + accountName + ' : ' + str(ex))
else:
hashes = list(map(lambda x: x.decode().rstrip().split(','),archive.extractfile(modelFile).readlines()))
for accountName,model in models:
try:
cairis.core.BorgFactory.initialise(user=accountName,db='default')
package_import(model)
logger.info('Re-imported default model for account ' + accountName)
except ARMException as ex:
logger.info('Error re-importing default model for account ' + accountName + ' : ' + str(ex))

import pytest
pytest.set_trace()
updateEmailHashes(b.rPasswd,b.dbHost,b.dbPort,hashes)
if __name__ == '__main__':
try:
main()
Expand Down
7 changes: 7 additions & 0 deletions cairis/core/dba.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,3 +220,10 @@ def resetUsers(cairisRoot,rPasswd,dbHost,dbPort):
for email in accounts(rPasswd,dbHost,dbPort):
resetUser(cairisRoot,rPasswd,dbHost,dbPort,email)

def emailHashes(rPasswd,dbHost,dbPort):
sqlTxt = 'select email,password from cairis_user.auth_user'
return rootResponseList(sqlTxt)

def updateEmailHashes(rPasswd,dbHost,dbPort,ehs):
stmts = list(map(lambda x: 'update cairis_user.auth_user set password = "' + x[1] + '" where email = "' + x[0] + '"',ehs))
runAdminCommands(rPasswd,dbHost,dbPort,stmts)

0 comments on commit 4feef4c

Please sign in to comment.