99import tarfile
1010import 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
2828def _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
7081def _importCsvToTable (fileName , deleteDataFirst = False ):
7182 """Imports given CSV file to the database."""
@@ -92,6 +103,8 @@ def _importCsvToTable(fileName, deleteDataFirst=False):
92103
93104def 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+
111126if __name__ == '__main__' :
112127 print 'Backup tool works as a library.'
0 commit comments