Skip to content
This repository was archived by the owner on May 16, 2019. It is now read-only.

Commit 8b01ed3

Browse files
committed
Add API support for backup tool
1 parent ec1caf4 commit 8b01ed3

2 files changed

Lines changed: 49 additions & 21 deletions

File tree

api/restapi.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
__author__ = 'chris'
2+
import backupTool
23
import json
34
import os
45
from txrestapi.resource import APIResource
@@ -470,3 +471,15 @@ def get_node(node):
470471
seller_guid = unhexlify(c.contract["vendor_offer"]["listing"]["id"]["guid"])
471472
self.kserver.resolve(seller_guid).addCallback(get_node)
472473
return server.NOT_DONE_YET
474+
475+
@POST('^/api/v1/backup')
476+
def backup(self, request):
477+
tablesAndColumns = request.args["tablesAndColumns"][0]
478+
output = request.args["output"][0]
479+
return backupTool.backup(tablesAndColumns, output)
480+
481+
@POST('^/api/v1/restore')
482+
def restore(self, request):
483+
input = request.args["input"][0]
484+
deleteTableDataFirst = request.args["deleteTableDataFirst"][0]
485+
return backupTool.restore(input, deleteTableDataFirst)

backupTool.py

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,21 @@
99
import tarfile
1010
import time
1111

12-
TABLES = [
13-
('hashmap', ['hash', 'filepath']),
14-
('profile', ['id', 'serializedUserInfo']),
15-
('listings', ['id', 'serializedListings']),
16-
('keys', ['type', 'privkey', 'pubkey']),
17-
('followers', ['id', 'serializedFollowers']),
18-
('following', ['id', 'serializedFollowing']),
19-
('messages', ['guid', 'handle', 'signed_pubkey', 'encryption_pubkey', 'subject', 'message_type', 'message', 'timestamp', 'avatar_hash', 'signature', 'outgoing']),
20-
('notifications', ['guid', 'handle', 'message', 'timestamp', 'avatar_hash']),
21-
('vendors', ['guid', 'ip', 'port', 'signedPubkey']),
22-
('moderators', ['guid', 'signedPubkey', 'encryptionKey', 'encryptionSignature', 'bitcoinKey', 'bitcoinSignature', 'handle']),
23-
('purchases', ['id', 'title', 'timestamp', 'btc', 'address', 'status', 'thumbnail', 'seller', 'proofSig']),
24-
('sales', ['id', 'title', 'timestamp', 'btc', 'address', 'status', 'thumbnail', 'seller']),
25-
('dht', ['keyword', 'id', 'value', 'birthday'])
26-
]
12+
TABLES = {
13+
'hashmap': ['hash', 'filepath'],
14+
'profile': ['id', 'serializedUserInfo'],
15+
'listings': ['id', 'serializedListings'],
16+
'keys': ['type', 'privkey', 'pubkey'],
17+
'followers': ['id', 'serializedFollowers'],
18+
'following': ['id', 'serializedFollowing'],
19+
'messages': ['guid', 'handle', 'signed_pubkey', 'encryption_pubkey', 'subject', 'message_type', 'message', 'timestamp', 'avatar_hash', 'signature', 'outgoing'],
20+
'notifications': ['guid', 'handle', 'message', 'timestamp', 'avatar_hash'],
21+
'vendors': ['guid', 'ip', 'port', 'signedPubkey'],
22+
'moderators': ['guid', 'signedPubkey', 'encryptionKey', 'encryptionSignature', 'bitcoinKey', 'bitcoinSignature', 'handle'],
23+
'purchases': ['id', 'title', 'timestamp', 'btc', 'address', 'status', 'thumbnail', 'seller', 'proofSig'],
24+
'sales': ['id', 'title', 'timestamp', 'btc', 'address', 'status', 'thumbnail', 'seller'],
25+
'dht': ['keyword', 'id', 'value', 'birthday']
26+
}
2727

2828
def _getDatabase():
2929
Database = db.Database()
@@ -48,15 +48,25 @@ def _exportDatabaseToCsv(tablesAndColumns):
4848
writer.writerows(data)
4949
return result
5050

51-
def backup(tablesAndColumns=None, output=None):
51+
def backup(tableList=None, output=None):
5252
"""Archives given tables and files in a single tar archive."""
5353
os.chdir(DATA_FOLDER)
5454

55-
# Remove existing database files and re-make them
56-
if os.path.exists('backup'):
57-
shutil.rmtree('backup')
58-
os.makedirs('backup')
59-
_exportDatabaseToCsv(tablesAndColumns)
55+
if tableList:
56+
# Parse table list
57+
tableList = tableList.replace(' ', '').split(',')
58+
tablesAndColumns = []
59+
for table in tableList:
60+
if table in TABLES:
61+
tablesAndColumns.append((table, TABLES[table]))
62+
else:
63+
return 'ERROR, Table not found: {0}'.format(table)
64+
65+
# Remove existing database files and re-make them
66+
if os.path.exists('backup'):
67+
shutil.rmtree('backup')
68+
os.makedirs('backup')
69+
_exportDatabaseToCsv(tablesAndColumns)
6070

6171
# Archive files
6272
files = os.listdir(DATA_FOLDER)
@@ -66,6 +76,7 @@ def backup(tablesAndColumns=None, output=None):
6676
for f in files:
6777
tar.add(f)
6878
tar.close()
79+
return 'Success'
6980

7081
def _importCsvToTable(fileName, deleteDataFirst=False):
7182
"""Imports given CSV file to the database."""
@@ -92,6 +103,8 @@ def _importCsvToTable(fileName, deleteDataFirst=False):
92103

93104
def restore(input, deleteTableDataFirst=False):
94105
"""Restores files and tables of given archive."""
106+
if not input:
107+
return 'Input path is needed'
95108
os.chdir(DATA_FOLDER)
96109

97110
# Remove existing database files if any
@@ -108,5 +121,7 @@ def restore(input, deleteTableDataFirst=False):
108121
for f in files:
109122
_importCsvToTable(f, deleteTableDataFirst)
110123

124+
return 'Success'
125+
111126
if __name__ == '__main__':
112127
print 'Backup tool works as a library.'

0 commit comments

Comments
 (0)