-
-
Notifications
You must be signed in to change notification settings - Fork 836
attic to borg one time converter #231
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
63 commits
Select commit
Hold shift + click to select a range
de54228
first stab at an attic-borg converter
anarcat 9ab1e19
keyfile conversion code
anarcat e88a994
reshuffle and document
anarcat 2d19881
some debugging code
anarcat c7af4c7
more debug
anarcat 312c3cf
rewrite converter to avoid using attic code
anarcat aa25a21
move conversion code to a separate class for clarity
anarcat 5a16803
remove needless use of self
anarcat c30df4e
move converter code out of test suite
anarcat 77ed6de
skip converter tests if attic isn't installed
anarcat e554365
remove unused import
anarcat f35e8e1
add dry run support to converter
anarcat a5f32b0
add convert command
anarcat 1b29699
cosmetic: reorder
anarcat 1ba856d
refactor: group test repo subroutine
anarcat bcd94b9
split up keyfile, segments and overall testing in converter
anarcat c990829
add attic dependency for build as a separate factor
anarcat a81755f
use triple-double-quoted instead of single-double-quoted
anarcat efbad39
help text review: magic s/number/string/, s/can/must/
anarcat c2913f5
style: don't use continue for nothing
anarcat dbd4ac7
add missing colon
anarcat 5b8cb63
remove duplicate code with the unit test
anarcat ef0ed40
fix typo
anarcat d665163
use builtin NotImplementedError instead of writing our own
anarcat d5198c5
split out depends in imports
anarcat 5f6eb87
much nicer validation checking
anarcat 4a85f2d
fix most pep8 warnings
anarcat b9c474d
pep8: put pytest skip marker after imports
anarcat 79d9aeb
use permanently instead of irrevocably, which is less common
anarcat 57801a2
keep tests simple by always adding attic depends
anarcat 58815bc
fix commandline dispatch for converter
anarcat 98e4e6b
lock repository when converting segments
anarcat f5cb0f4
rewrite convert tests with pytest fixtures
anarcat a08bcb2
refactor common code
anarcat 7f6fd1f
add docs for all converter test code
anarcat 6c318a0
re-pep8
anarcat 946aca9
avoid flooding the console
anarcat 0d457bc
clarify what to do about the cache warning
anarcat 3bb3bd4
add percentage progress
anarcat 6a72252
release lock properly if segment conversion crashes
anarcat 180dfcb
remove needless indentation
anarcat 35b2195
only write magic num if necessary
anarcat a7902e5
cosmetic: show 100% when done, not n-1/n%
anarcat 7c32f55
repository index conversion
anarcat 022de5b
untested file/chunks cache conversion
anarcat 4f9a411
remove unneeded fixture decorator
anarcat 28a033d
remove debug output that clobbers segment spinner
anarcat 55f79b4
complete cache conversion code
anarcat 8022e56
don't clobber existing borg cache
anarcat 3e7fa0d
also copy the cache config file to workaround #234
anarcat 081b91b
remove needless paren
anarcat 41e9942
follow naming of tested module
anarcat d4d1b41
remove needless autouse
anarcat 6904058
update docs to reflect that cache is converted
anarcat ad85f64
whitespace
anarcat ea5d004
also document the cache locations
anarcat 2c66e7c
make percentage a real percentage
anarcat 3773681
rewire cache copy mechanisms
anarcat 6905412
style fixes (pep8, append, file builtin)
anarcat 48b7c8c
avoid checking for non-existent files
anarcat c91c5d0
rename convert command to upgrade
anarcat fded221
mention borg delete borg
anarcat 5409cba
also copy files cache verbatim
anarcat 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 hidden or 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 hidden or 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,163 @@ | ||
| import os | ||
| import shutil | ||
| import tempfile | ||
|
|
||
| import pytest | ||
|
|
||
| try: | ||
| import attic.repository | ||
| import attic.key | ||
| import attic.helpers | ||
| except ImportError: | ||
| attic = None | ||
|
|
||
| from ..upgrader import AtticRepositoryUpgrader, AtticKeyfileKey | ||
| from ..helpers import get_keys_dir | ||
| from ..key import KeyfileKey | ||
| from ..repository import Repository, MAGIC | ||
|
|
||
| pytestmark = pytest.mark.skipif(attic is None, | ||
| reason='cannot find an attic install') | ||
|
|
||
|
|
||
| def repo_valid(path): | ||
| """ | ||
| utility function to check if borg can open a repository | ||
|
|
||
| :param path: the path to the repository | ||
| :returns: if borg can check the repository | ||
| """ | ||
| repository = Repository(str(path), create=False) | ||
| # can't check raises() because check() handles the error | ||
| state = repository.check() | ||
| repository.close() | ||
| return state | ||
|
|
||
|
|
||
| def key_valid(path): | ||
| """ | ||
| check that the new keyfile is alright | ||
|
|
||
| :param path: the path to the key file | ||
| :returns: if the file starts with the borg magic string | ||
| """ | ||
| keyfile = os.path.join(get_keys_dir(), | ||
| os.path.basename(path)) | ||
| with open(keyfile, 'r') as f: | ||
| return f.read().startswith(KeyfileKey.FILE_ID) | ||
|
|
||
|
|
||
| @pytest.fixture() | ||
| def attic_repo(tmpdir): | ||
| """ | ||
| create an attic repo with some stuff in it | ||
|
|
||
| :param tmpdir: path to the repository to be created | ||
| :returns: a attic.repository.Repository object | ||
| """ | ||
| attic_repo = attic.repository.Repository(str(tmpdir), create=True) | ||
| # throw some stuff in that repo, copied from `RepositoryTestCase.test1` | ||
| for x in range(100): | ||
| attic_repo.put(('%-32d' % x).encode('ascii'), b'SOMEDATA') | ||
| attic_repo.commit() | ||
| attic_repo.close() | ||
| return attic_repo | ||
|
|
||
|
|
||
| def test_convert_segments(tmpdir, attic_repo): | ||
| """test segment conversion | ||
|
|
||
| this will load the given attic repository, list all the segments | ||
| then convert them one at a time. we need to close the repo before | ||
| conversion otherwise we have errors from borg | ||
|
|
||
| :param tmpdir: a temporary directory to run the test in (builtin | ||
| fixture) | ||
| :param attic_repo: a populated attic repository (fixture) | ||
| """ | ||
| # check should fail because of magic number | ||
| assert not repo_valid(tmpdir) | ||
| print("opening attic repository with borg and converting") | ||
| repo = AtticRepositoryUpgrader(str(tmpdir), create=False) | ||
| segments = [filename for i, filename in repo.io.segment_iterator()] | ||
| repo.close() | ||
| repo.convert_segments(segments, dryrun=False) | ||
| repo.convert_cache(dryrun=False) | ||
| assert repo_valid(tmpdir) | ||
|
|
||
|
|
||
| class MockArgs: | ||
| """ | ||
| mock attic location | ||
|
|
||
| this is used to simulate a key location with a properly loaded | ||
| repository object to create a key file | ||
| """ | ||
| def __init__(self, path): | ||
| self.repository = attic.helpers.Location(path) | ||
|
|
||
|
|
||
| @pytest.fixture() | ||
| def attic_key_file(attic_repo, tmpdir): | ||
| """ | ||
| create an attic key file from the given repo, in the keys | ||
| subdirectory of the given tmpdir | ||
|
|
||
| :param attic_repo: an attic.repository.Repository object (fixture | ||
| define above) | ||
| :param tmpdir: a temporary directory (a builtin fixture) | ||
| :returns: the KeyfileKey object as returned by | ||
| attic.key.KeyfileKey.create() | ||
| """ | ||
| keys_dir = str(tmpdir.mkdir('keys')) | ||
|
|
||
| # we use the repo dir for the created keyfile, because we do | ||
| # not want to clutter existing keyfiles | ||
| os.environ['ATTIC_KEYS_DIR'] = keys_dir | ||
|
|
||
| # we use the same directory for the converted files, which | ||
| # will clutter the previously created one, which we don't care | ||
| # about anyways. in real runs, the original key will be retained. | ||
| os.environ['BORG_KEYS_DIR'] = keys_dir | ||
| os.environ['ATTIC_PASSPHRASE'] = 'test' | ||
| return attic.key.KeyfileKey.create(attic_repo, | ||
| MockArgs(keys_dir)) | ||
|
|
||
|
|
||
| def test_keys(tmpdir, attic_repo, attic_key_file): | ||
| """test key conversion | ||
|
|
||
| test that we can convert the given key to a properly formatted | ||
| borg key. assumes that the ATTIC_KEYS_DIR and BORG_KEYS_DIR have | ||
| been properly populated by the attic_key_file fixture. | ||
|
|
||
| :param tmpdir: a temporary directory (a builtin fixture) | ||
| :param attic_repo: an attic.repository.Repository object (fixture | ||
| define above) | ||
| :param attic_key_file: an attic.key.KeyfileKey (fixture created above) | ||
| """ | ||
| repository = AtticRepositoryUpgrader(str(tmpdir), create=False) | ||
| keyfile = AtticKeyfileKey.find_key_file(repository) | ||
| AtticRepositoryUpgrader.convert_keyfiles(keyfile, dryrun=False) | ||
| assert key_valid(attic_key_file.path) | ||
|
|
||
|
|
||
| def test_convert_all(tmpdir, attic_repo, attic_key_file): | ||
| """test all conversion steps | ||
|
|
||
| this runs everything. mostly redundant test, since everything is | ||
| done above. yet we expect a NotImplementedError because we do not | ||
| convert caches yet. | ||
|
|
||
| :param tmpdir: a temporary directory (a builtin fixture) | ||
| :param attic_repo: an attic.repository.Repository object (fixture | ||
| define above) | ||
| :param attic_key_file: an attic.key.KeyfileKey (fixture created above) | ||
| """ | ||
| # check should fail because of magic number | ||
| assert not repo_valid(tmpdir) | ||
| print("opening attic repository with borg and converting") | ||
| repo = AtticRepositoryUpgrader(str(tmpdir), create=False) | ||
| repo.upgrade(dryrun=False) | ||
| assert key_valid(attic_key_file.path) | ||
| assert repo_valid(tmpdir) |
Oops, something went wrong.
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.
just as a reminder to change this (and other similar places) to logging.warning (or .error) after we have reasonable logging in place.
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.
sure. can't wait. :)