This repository has been archived by the owner on May 16, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 173
Added a backup tool to import/export store data #35
Open
marcjamot
wants to merge
17
commits into
OpenBazaar:master
Choose a base branch
from
hauxir:develop
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
3655048
temp fix for OB server
hauxir e812d26
test form
hauxir 4a69399
Add base functionality for backup tool python code
marcjamot cd97a39
Remove print in code
marcjamot ec1caf4
Updated backup to use generated OpenBazaar folder
marcjamot 8b01ed3
Add API support for backup tool
marcjamot 1e8a6ec
Add backup test html
marcjamot d18ca0a
Separate archive from database export
marcjamot e4fe5fd
Added a backup tool to backup your store data
hauxir 4469bf6
test form
hauxir 4949aa6
Add base functionality for backup tool python code
marcjamot e64a0e2
Remove print in code
marcjamot ee4bd6c
Updated backup to use generated OpenBazaar folder
marcjamot 54cd325
Add backup test html
marcjamot 1e2daef
Add test for backup tool
marcjamot e7a5246
Rebase master
marcjamot 14053e8
Merge branch 'develop' of https://github.com/hauxir/OpenBazaar-Server…
marcjamot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
"""Import and export data for the OpenBazaar server.""" | ||
__author__ = 'marc' | ||
from constants import DATA_FOLDER | ||
import db.datastore as db | ||
import os | ||
import sqlite3 as lite | ||
import tarfile | ||
import time | ||
|
||
BACKUP_FOLDER = 'backup' | ||
|
||
def _getdatabase(): | ||
"""Retrieves the OpenBazaar database file.""" | ||
database = db.Database() | ||
return database.DATABASE | ||
|
||
def backupfiles(output_name=None): | ||
"""Archives OpenBazaar files in a single tar archive.""" | ||
os.chdir(DATA_FOLDER) | ||
if not os.path.exists(BACKUP_FOLDER): | ||
os.makedirs(BACKUP_FOLDER) | ||
if not output_name: | ||
output_name = 'backup_{0}.tar.gz'.format(time.strftime('%Y-%m-%d')) | ||
if not os.path.isabs(output_name): | ||
output = BACKUP_FOLDER + os.sep + output_name | ||
if os.path.isfile(output): | ||
raise IOError(output + ' already exists.') | ||
|
||
# Lock the database | ||
db_file = _getdatabase() | ||
db_connection = lite.connect(db_file) | ||
db_connection.commit() | ||
|
||
# Archive files | ||
files = os.listdir(DATA_FOLDER) | ||
with tarfile.open(output, 'w:gz') as tar: | ||
for fil in files: | ||
if fil != BACKUP_FOLDER: | ||
tar.add(fil) | ||
tar.close() | ||
|
||
# Unlock database | ||
db_connection.rollback() | ||
db_connection.close() | ||
|
||
return True | ||
|
||
|
||
def restorefiles(input_file): | ||
"""Restores files of given archive to OpenBazaar folder.""" | ||
os.chdir(DATA_FOLDER) | ||
if not os.path.isabs(input_file): | ||
input_file = BACKUP_FOLDER + os.sep + input_file | ||
|
||
if not os.path.isfile(input_file): | ||
raise IOError(input_file + ' does not exist.') | ||
|
||
# Unarchive files | ||
with tarfile.open(input_file, 'r:gz') as tar: | ||
tar.extractall() | ||
|
||
return True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
"""Test for db/backuptool""" | ||
from constants import DATA_FOLDER | ||
import os | ||
import unittest | ||
|
||
import db.backuptool as bt | ||
|
||
TEST_FOLDER = 'test' | ||
TEST_TAR = 'test.tar.gz' | ||
TEST_FILE_1 = 'test.txt' | ||
TEST_FILE_2 = TEST_FOLDER + os.sep + 'test2.txt' | ||
|
||
class BackuptoolTest(unittest.TestCase): | ||
"""Test class for backuptool functions""" | ||
def setUp(self): | ||
os.chdir(DATA_FOLDER) | ||
# Create test folder | ||
if not os.path.exists(TEST_FOLDER): | ||
os.makedirs(TEST_FOLDER) | ||
# Create test files "test.txt", "test/test2.txt" | ||
fil = open(TEST_FILE_1, 'w') | ||
fil.close() | ||
fil = open(TEST_FILE_2, 'w') | ||
fil.close() | ||
# Backup to "test.tar.gz" | ||
if os.path.exists(bt.BACKUP_FOLDER + os.sep + TEST_TAR): | ||
os.remove(bt.BACKUP_FOLDER + os.sep + TEST_TAR) | ||
bt.backupfiles(TEST_TAR) | ||
# Remove test files and directory | ||
os.remove(TEST_FILE_1) | ||
os.remove(TEST_FILE_2) | ||
# Restore from "test.tar.gz" | ||
bt.restorefiles(TEST_TAR) | ||
|
||
def tearDown(self): | ||
os.remove(TEST_FILE_1) | ||
os.remove(TEST_FILE_2) | ||
os.remove(bt.BACKUP_FOLDER + os.sep + TEST_TAR) | ||
|
||
def test_backupexists(self): | ||
"""Checks if the backup file exists""" | ||
self.assertTrue(os.path.isfile(bt.BACKUP_FOLDER + os.sep + TEST_TAR)) | ||
|
||
def test_restoreexists(self): | ||
"""Checks if the restored files exists""" | ||
self.assertTrue(os.path.isfile(TEST_FILE_1)) | ||
self.assertTrue(os.path.isfile(TEST_FILE_2)) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PEP8-nitpicking: Please add an extra blank line here, before the class definition.