2015-10-01 03:18:03 +00:00
|
|
|
from binascii import hexlify
|
do not upgrade repositories in place by default
instead, we perform the equivalent of `cp -al` on the repository to
keep a backup, and then rewrite the files, breaking the hardlinks as
necessary.
it has to be confirmed that the rest of Borg will also break hardlinks
when operating on files in the repository. if Borg operates in place
on any files of the repository, it could jeoperdize the backup, so
this needs to be verified. I believe that most files are written to a
temporary file and moved into place, however, so the backup should be
safe.
the rationale behind the backup copy is that we want to be extra
careful with user's data by default. the old behavior is retained
through the `--inplace`/`-i` commandline flag. plus, this way we don't
need to tell users to go through extra steps (`cp -a`, in particular)
before running the command.
also, it can take a long time to do the copy of the attic repository
we wish to work on. since `cp -a` doesn't provide progress
information, the new default behavior provides a nicer user experience
of giving an overall impression of the upgrade progress, while
retaining compatibility with Attic by default (in a separate
repository, of course).
this makes the upgrade command much less scary to use and hopefully
will convert drones to the borg collective.
the only place where the default inplace behavior is retained is in
the header_replace() function, to avoid breaking the cache conversion
code and to keep API stability and semantic coherence ("replace" by
defaults means in place).
2015-10-15 22:02:24 +00:00
|
|
|
import datetime
|
2015-10-03 18:20:53 +00:00
|
|
|
import logging
|
|
|
|
logger = logging.getLogger(__name__)
|
2015-10-01 03:18:03 +00:00
|
|
|
import os
|
2015-10-01 20:01:01 +00:00
|
|
|
import shutil
|
2015-10-01 15:25:02 +00:00
|
|
|
import time
|
2015-10-01 03:18:03 +00:00
|
|
|
|
2015-12-02 00:26:26 +00:00
|
|
|
from .helpers import get_keys_dir, get_cache_dir, ProgressIndicatorPercent
|
2015-10-01 13:35:17 +00:00
|
|
|
from .locking import UpgradableLock
|
2015-10-01 03:18:03 +00:00
|
|
|
from .repository import Repository, MAGIC
|
|
|
|
from .key import KeyfileKey, KeyfileNotFoundError
|
|
|
|
|
2015-10-01 18:28:49 +00:00
|
|
|
ATTIC_MAGIC = b'ATTICSEG'
|
|
|
|
|
2015-10-03 15:49:01 +00:00
|
|
|
|
2015-10-03 16:36:52 +00:00
|
|
|
class AtticRepositoryUpgrader(Repository):
|
2015-12-10 10:11:06 +00:00
|
|
|
def __init__(self, *args, **kw):
|
|
|
|
kw['lock'] = False # do not create borg lock files (now) in attic repo
|
|
|
|
super().__init__(*args, **kw)
|
|
|
|
|
2016-01-16 19:32:24 +00:00
|
|
|
def upgrade(self, dryrun=True, inplace=False, progress=False):
|
2015-10-01 12:34:18 +00:00
|
|
|
"""convert an attic repository to a borg repository
|
2015-10-01 03:18:03 +00:00
|
|
|
|
2015-10-03 16:36:52 +00:00
|
|
|
those are the files that need to be upgraded here, from most
|
2015-10-01 03:18:03 +00:00
|
|
|
important to least important: segments, key files, and various
|
|
|
|
caches, the latter being optional, as they will be rebuilt if
|
2015-10-01 20:24:28 +00:00
|
|
|
missing.
|
|
|
|
|
|
|
|
we nevertheless do the order in reverse, as we prefer to do
|
|
|
|
the fast stuff first, to improve interactivity.
|
|
|
|
"""
|
do not upgrade repositories in place by default
instead, we perform the equivalent of `cp -al` on the repository to
keep a backup, and then rewrite the files, breaking the hardlinks as
necessary.
it has to be confirmed that the rest of Borg will also break hardlinks
when operating on files in the repository. if Borg operates in place
on any files of the repository, it could jeoperdize the backup, so
this needs to be verified. I believe that most files are written to a
temporary file and moved into place, however, so the backup should be
safe.
the rationale behind the backup copy is that we want to be extra
careful with user's data by default. the old behavior is retained
through the `--inplace`/`-i` commandline flag. plus, this way we don't
need to tell users to go through extra steps (`cp -a`, in particular)
before running the command.
also, it can take a long time to do the copy of the attic repository
we wish to work on. since `cp -a` doesn't provide progress
information, the new default behavior provides a nicer user experience
of giving an overall impression of the upgrade progress, while
retaining compatibility with Attic by default (in a separate
repository, of course).
this makes the upgrade command much less scary to use and hopefully
will convert drones to the borg collective.
the only place where the default inplace behavior is retained is in
the header_replace() function, to avoid breaking the cache conversion
code and to keep API stability and semantic coherence ("replace" by
defaults means in place).
2015-10-15 22:02:24 +00:00
|
|
|
backup = None
|
|
|
|
if not inplace:
|
|
|
|
backup = '{}.upgrade-{:%Y-%m-%d-%H:%M:%S}'.format(self.path, datetime.datetime.now())
|
|
|
|
logger.info('making a hardlink copy in %s', backup)
|
|
|
|
if not dryrun:
|
2015-10-15 22:14:44 +00:00
|
|
|
shutil.copytree(self.path, backup, copy_function=os.link)
|
2015-10-09 16:34:02 +00:00
|
|
|
logger.info("opening attic repository with borg and converting")
|
2015-12-10 10:11:06 +00:00
|
|
|
# now lock the repo, after we have made the copy
|
|
|
|
self.lock = UpgradableLock(os.path.join(self.path, 'lock'), exclusive=True, timeout=1.0).acquire()
|
2015-10-03 15:49:01 +00:00
|
|
|
segments = [filename for i, filename in self.io.segment_iterator()]
|
2015-10-01 03:18:03 +00:00
|
|
|
try:
|
|
|
|
keyfile = self.find_attic_keyfile()
|
|
|
|
except KeyfileNotFoundError:
|
2015-10-03 18:20:53 +00:00
|
|
|
logger.warning("no key file found for repository")
|
2015-10-01 03:18:03 +00:00
|
|
|
else:
|
2015-10-01 03:50:35 +00:00
|
|
|
self.convert_keyfiles(keyfile, dryrun)
|
2015-10-01 03:18:03 +00:00
|
|
|
self.close()
|
2015-10-01 13:35:17 +00:00
|
|
|
# partial open: just hold on to the lock
|
|
|
|
self.lock = UpgradableLock(os.path.join(self.path, 'lock'),
|
|
|
|
exclusive=True).acquire()
|
2015-10-01 18:22:29 +00:00
|
|
|
try:
|
2015-10-01 19:43:16 +00:00
|
|
|
self.convert_cache(dryrun)
|
2015-12-11 21:18:18 +00:00
|
|
|
self.convert_repo_index(dryrun=dryrun, inplace=inplace)
|
2016-01-16 19:32:24 +00:00
|
|
|
self.convert_segments(segments, dryrun=dryrun, inplace=inplace, progress=progress)
|
2015-12-10 09:51:56 +00:00
|
|
|
self.borg_readme()
|
2015-10-01 18:22:29 +00:00
|
|
|
finally:
|
|
|
|
self.lock.release()
|
|
|
|
self.lock = None
|
do not upgrade repositories in place by default
instead, we perform the equivalent of `cp -al` on the repository to
keep a backup, and then rewrite the files, breaking the hardlinks as
necessary.
it has to be confirmed that the rest of Borg will also break hardlinks
when operating on files in the repository. if Borg operates in place
on any files of the repository, it could jeoperdize the backup, so
this needs to be verified. I believe that most files are written to a
temporary file and moved into place, however, so the backup should be
safe.
the rationale behind the backup copy is that we want to be extra
careful with user's data by default. the old behavior is retained
through the `--inplace`/`-i` commandline flag. plus, this way we don't
need to tell users to go through extra steps (`cp -a`, in particular)
before running the command.
also, it can take a long time to do the copy of the attic repository
we wish to work on. since `cp -a` doesn't provide progress
information, the new default behavior provides a nicer user experience
of giving an overall impression of the upgrade progress, while
retaining compatibility with Attic by default (in a separate
repository, of course).
this makes the upgrade command much less scary to use and hopefully
will convert drones to the borg collective.
the only place where the default inplace behavior is retained is in
the header_replace() function, to avoid breaking the cache conversion
code and to keep API stability and semantic coherence ("replace" by
defaults means in place).
2015-10-15 22:02:24 +00:00
|
|
|
return backup
|
2015-10-01 03:18:03 +00:00
|
|
|
|
2015-12-10 09:51:56 +00:00
|
|
|
def borg_readme(self):
|
|
|
|
readme = os.path.join(self.path, 'README')
|
|
|
|
os.remove(readme)
|
|
|
|
with open(readme, 'w') as fd:
|
|
|
|
fd.write('This is a Borg repository\n')
|
|
|
|
|
2015-10-01 03:18:03 +00:00
|
|
|
@staticmethod
|
2016-01-16 19:32:24 +00:00
|
|
|
def convert_segments(segments, dryrun=True, inplace=False, progress=False):
|
2015-10-01 12:34:18 +00:00
|
|
|
"""convert repository segments from attic to borg
|
2015-10-01 03:18:03 +00:00
|
|
|
|
|
|
|
replacement pattern is `s/ATTICSEG/BORG_SEG/` in files in
|
|
|
|
`$ATTIC_REPO/data/**`.
|
|
|
|
|
2015-10-01 12:36:20 +00:00
|
|
|
luckily the magic string length didn't change so we can just
|
2015-10-01 12:34:18 +00:00
|
|
|
replace the 8 first bytes of all regular files in there."""
|
2015-10-03 18:20:53 +00:00
|
|
|
logger.info("converting %d segments..." % len(segments))
|
2015-12-02 00:26:26 +00:00
|
|
|
segment_count = len(segments)
|
|
|
|
pi = ProgressIndicatorPercent(total=segment_count, msg="Converting segments %3.0f%%", same_line=True)
|
|
|
|
for i, filename in enumerate(segments):
|
2016-01-16 19:32:24 +00:00
|
|
|
if progress:
|
|
|
|
pi.show(i)
|
2015-10-01 15:25:02 +00:00
|
|
|
if dryrun:
|
|
|
|
time.sleep(0.001)
|
|
|
|
else:
|
do not upgrade repositories in place by default
instead, we perform the equivalent of `cp -al` on the repository to
keep a backup, and then rewrite the files, breaking the hardlinks as
necessary.
it has to be confirmed that the rest of Borg will also break hardlinks
when operating on files in the repository. if Borg operates in place
on any files of the repository, it could jeoperdize the backup, so
this needs to be verified. I believe that most files are written to a
temporary file and moved into place, however, so the backup should be
safe.
the rationale behind the backup copy is that we want to be extra
careful with user's data by default. the old behavior is retained
through the `--inplace`/`-i` commandline flag. plus, this way we don't
need to tell users to go through extra steps (`cp -a`, in particular)
before running the command.
also, it can take a long time to do the copy of the attic repository
we wish to work on. since `cp -a` doesn't provide progress
information, the new default behavior provides a nicer user experience
of giving an overall impression of the upgrade progress, while
retaining compatibility with Attic by default (in a separate
repository, of course).
this makes the upgrade command much less scary to use and hopefully
will convert drones to the borg collective.
the only place where the default inplace behavior is retained is in
the header_replace() function, to avoid breaking the cache conversion
code and to keep API stability and semantic coherence ("replace" by
defaults means in place).
2015-10-15 22:02:24 +00:00
|
|
|
AtticRepositoryUpgrader.header_replace(filename, ATTIC_MAGIC, MAGIC, inplace=inplace)
|
2016-01-16 19:32:24 +00:00
|
|
|
if progress:
|
|
|
|
pi.finish()
|
2015-10-01 03:18:03 +00:00
|
|
|
|
2015-10-01 19:43:16 +00:00
|
|
|
@staticmethod
|
do not upgrade repositories in place by default
instead, we perform the equivalent of `cp -al` on the repository to
keep a backup, and then rewrite the files, breaking the hardlinks as
necessary.
it has to be confirmed that the rest of Borg will also break hardlinks
when operating on files in the repository. if Borg operates in place
on any files of the repository, it could jeoperdize the backup, so
this needs to be verified. I believe that most files are written to a
temporary file and moved into place, however, so the backup should be
safe.
the rationale behind the backup copy is that we want to be extra
careful with user's data by default. the old behavior is retained
through the `--inplace`/`-i` commandline flag. plus, this way we don't
need to tell users to go through extra steps (`cp -a`, in particular)
before running the command.
also, it can take a long time to do the copy of the attic repository
we wish to work on. since `cp -a` doesn't provide progress
information, the new default behavior provides a nicer user experience
of giving an overall impression of the upgrade progress, while
retaining compatibility with Attic by default (in a separate
repository, of course).
this makes the upgrade command much less scary to use and hopefully
will convert drones to the borg collective.
the only place where the default inplace behavior is retained is in
the header_replace() function, to avoid breaking the cache conversion
code and to keep API stability and semantic coherence ("replace" by
defaults means in place).
2015-10-15 22:02:24 +00:00
|
|
|
def header_replace(filename, old_magic, new_magic, inplace=True):
|
2015-10-01 19:43:16 +00:00
|
|
|
with open(filename, 'r+b') as segment:
|
|
|
|
segment.seek(0)
|
|
|
|
# only write if necessary
|
2015-10-02 13:43:10 +00:00
|
|
|
if segment.read(len(old_magic)) == old_magic:
|
do not upgrade repositories in place by default
instead, we perform the equivalent of `cp -al` on the repository to
keep a backup, and then rewrite the files, breaking the hardlinks as
necessary.
it has to be confirmed that the rest of Borg will also break hardlinks
when operating on files in the repository. if Borg operates in place
on any files of the repository, it could jeoperdize the backup, so
this needs to be verified. I believe that most files are written to a
temporary file and moved into place, however, so the backup should be
safe.
the rationale behind the backup copy is that we want to be extra
careful with user's data by default. the old behavior is retained
through the `--inplace`/`-i` commandline flag. plus, this way we don't
need to tell users to go through extra steps (`cp -a`, in particular)
before running the command.
also, it can take a long time to do the copy of the attic repository
we wish to work on. since `cp -a` doesn't provide progress
information, the new default behavior provides a nicer user experience
of giving an overall impression of the upgrade progress, while
retaining compatibility with Attic by default (in a separate
repository, of course).
this makes the upgrade command much less scary to use and hopefully
will convert drones to the borg collective.
the only place where the default inplace behavior is retained is in
the header_replace() function, to avoid breaking the cache conversion
code and to keep API stability and semantic coherence ("replace" by
defaults means in place).
2015-10-15 22:02:24 +00:00
|
|
|
if inplace:
|
|
|
|
segment.seek(0)
|
|
|
|
segment.write(new_magic)
|
|
|
|
else:
|
2015-10-18 14:43:59 +00:00
|
|
|
# rename the hardlink and rewrite the file. this works
|
|
|
|
# because the file is still open. so even though the file
|
|
|
|
# is renamed, we can still read it until it is closed.
|
2015-10-18 01:59:51 +00:00
|
|
|
os.rename(filename, filename + '.tmp')
|
do not upgrade repositories in place by default
instead, we perform the equivalent of `cp -al` on the repository to
keep a backup, and then rewrite the files, breaking the hardlinks as
necessary.
it has to be confirmed that the rest of Borg will also break hardlinks
when operating on files in the repository. if Borg operates in place
on any files of the repository, it could jeoperdize the backup, so
this needs to be verified. I believe that most files are written to a
temporary file and moved into place, however, so the backup should be
safe.
the rationale behind the backup copy is that we want to be extra
careful with user's data by default. the old behavior is retained
through the `--inplace`/`-i` commandline flag. plus, this way we don't
need to tell users to go through extra steps (`cp -a`, in particular)
before running the command.
also, it can take a long time to do the copy of the attic repository
we wish to work on. since `cp -a` doesn't provide progress
information, the new default behavior provides a nicer user experience
of giving an overall impression of the upgrade progress, while
retaining compatibility with Attic by default (in a separate
repository, of course).
this makes the upgrade command much less scary to use and hopefully
will convert drones to the borg collective.
the only place where the default inplace behavior is retained is in
the header_replace() function, to avoid breaking the cache conversion
code and to keep API stability and semantic coherence ("replace" by
defaults means in place).
2015-10-15 22:02:24 +00:00
|
|
|
with open(filename, 'wb') as new_segment:
|
|
|
|
new_segment.write(new_magic)
|
|
|
|
new_segment.write(segment.read())
|
2015-10-18 01:59:51 +00:00
|
|
|
# the little dance with the .tmp file is necessary
|
2015-10-18 14:43:59 +00:00
|
|
|
# because Windows won't allow overwriting an open file.
|
2015-10-18 01:59:51 +00:00
|
|
|
os.unlink(filename + '.tmp')
|
2015-10-01 19:43:16 +00:00
|
|
|
|
2015-10-01 03:18:03 +00:00
|
|
|
def find_attic_keyfile(self):
|
2015-10-01 12:34:18 +00:00
|
|
|
"""find the attic keyfiles
|
2015-10-01 03:18:03 +00:00
|
|
|
|
|
|
|
the keyfiles are loaded by `KeyfileKey.find_key_file()`. that
|
2015-10-01 12:41:44 +00:00
|
|
|
finds the keys with the right identifier for the repo.
|
2015-10-01 03:18:03 +00:00
|
|
|
|
|
|
|
this is expected to look into $HOME/.attic/keys or
|
|
|
|
$ATTIC_KEYS_DIR for key files matching the given Borg
|
|
|
|
repository.
|
|
|
|
|
|
|
|
it is expected to raise an exception (KeyfileNotFoundError) if
|
|
|
|
no key is found. whether that exception is from Borg or Attic
|
|
|
|
is unclear.
|
|
|
|
|
|
|
|
this is split in a separate function in case we want to use
|
|
|
|
the attic code here directly, instead of our local
|
2015-10-01 12:34:18 +00:00
|
|
|
implementation."""
|
2015-10-01 03:18:03 +00:00
|
|
|
return AtticKeyfileKey.find_key_file(self)
|
|
|
|
|
|
|
|
@staticmethod
|
2015-10-01 03:50:35 +00:00
|
|
|
def convert_keyfiles(keyfile, dryrun):
|
2015-10-01 03:18:03 +00:00
|
|
|
|
2015-10-01 12:34:18 +00:00
|
|
|
"""convert key files from attic to borg
|
2015-10-01 03:18:03 +00:00
|
|
|
|
|
|
|
replacement pattern is `s/ATTIC KEY/BORG_KEY/` in
|
|
|
|
`get_keys_dir()`, that is `$ATTIC_KEYS_DIR` or
|
|
|
|
`$HOME/.attic/keys`, and moved to `$BORG_KEYS_DIR` or
|
2016-01-28 22:15:49 +00:00
|
|
|
`$HOME/.config/borg/keys`.
|
2015-10-01 03:18:03 +00:00
|
|
|
|
|
|
|
no need to decrypt to convert. we need to rewrite the whole
|
2015-10-01 12:36:20 +00:00
|
|
|
key file because magic string length changed, but that's not a
|
2015-10-01 03:18:03 +00:00
|
|
|
problem because the keyfiles are small (compared to, say,
|
2015-10-01 12:34:18 +00:00
|
|
|
all the segments)."""
|
2015-10-03 18:20:53 +00:00
|
|
|
logger.info("converting keyfile %s" % keyfile)
|
2015-10-01 03:18:03 +00:00
|
|
|
with open(keyfile, 'r') as f:
|
|
|
|
data = f.read()
|
2015-10-01 18:23:43 +00:00
|
|
|
data = data.replace(AtticKeyfileKey.FILE_ID, KeyfileKey.FILE_ID, 1)
|
|
|
|
keyfile = os.path.join(get_keys_dir(), os.path.basename(keyfile))
|
2015-10-03 18:20:53 +00:00
|
|
|
logger.info("writing borg keyfile to %s" % keyfile)
|
2015-10-01 03:50:35 +00:00
|
|
|
if not dryrun:
|
|
|
|
with open(keyfile, 'w') as f:
|
|
|
|
f.write(data)
|
|
|
|
|
2015-12-11 21:18:18 +00:00
|
|
|
def convert_repo_index(self, dryrun, inplace):
|
2015-12-10 10:35:48 +00:00
|
|
|
"""convert some repo files
|
2015-10-01 03:18:03 +00:00
|
|
|
|
|
|
|
those are all hash indexes, so we need to
|
|
|
|
`s/ATTICIDX/BORG_IDX/` in a few locations:
|
2015-10-03 15:49:01 +00:00
|
|
|
|
2015-10-01 03:18:03 +00:00
|
|
|
* the repository index (in `$ATTIC_REPO/index.%d`, where `%d`
|
|
|
|
is the `Repository.get_index_transaction_id()`), which we
|
|
|
|
should probably update, with a lock, see
|
|
|
|
`Repository.open()`, which i'm not sure we should use
|
|
|
|
because it may write data on `Repository.close()`...
|
2015-10-01 12:34:18 +00:00
|
|
|
"""
|
2015-10-01 19:43:16 +00:00
|
|
|
transaction_id = self.get_index_transaction_id()
|
|
|
|
if transaction_id is None:
|
2015-10-03 18:20:53 +00:00
|
|
|
logger.warning('no index file found for repository %s' % self.path)
|
2015-10-01 19:43:16 +00:00
|
|
|
else:
|
2015-12-11 21:18:18 +00:00
|
|
|
index = os.path.join(self.path, 'index.%d' % transaction_id)
|
2015-12-10 10:35:48 +00:00
|
|
|
logger.info("converting repo index %s" % index)
|
2015-10-15 22:39:34 +00:00
|
|
|
if not dryrun:
|
2015-12-11 21:18:18 +00:00
|
|
|
AtticRepositoryUpgrader.header_replace(index, b'ATTICIDX', b'BORG_IDX', inplace=inplace)
|
2015-10-01 20:01:01 +00:00
|
|
|
|
2015-12-10 10:35:48 +00:00
|
|
|
def convert_cache(self, dryrun):
|
|
|
|
"""convert caches from attic to borg
|
|
|
|
|
|
|
|
those are all hash indexes, so we need to
|
|
|
|
`s/ATTICIDX/BORG_IDX/` in a few locations:
|
|
|
|
|
|
|
|
* the `files` and `chunks` cache (in `$ATTIC_CACHE_DIR` or
|
|
|
|
`$HOME/.cache/attic/<repoid>/`), which we could just drop,
|
|
|
|
but if we'd want to convert, we could open it with the
|
|
|
|
`Cache.open()`, edit in place and then `Cache.close()` to
|
|
|
|
make sure we have locking right
|
|
|
|
"""
|
2015-10-01 20:01:01 +00:00
|
|
|
# copy of attic's get_cache_dir()
|
|
|
|
attic_cache_dir = os.environ.get('ATTIC_CACHE_DIR',
|
2015-10-03 15:49:01 +00:00
|
|
|
os.path.join(os.path.expanduser('~'),
|
|
|
|
'.cache', 'attic'))
|
2015-10-03 15:07:37 +00:00
|
|
|
attic_cache_dir = os.path.join(attic_cache_dir, hexlify(self.id).decode('ascii'))
|
|
|
|
borg_cache_dir = os.path.join(get_cache_dir(), hexlify(self.id).decode('ascii'))
|
2015-10-01 20:01:01 +00:00
|
|
|
|
2015-10-03 15:49:01 +00:00
|
|
|
def copy_cache_file(path):
|
|
|
|
"""copy the given attic cache path into the borg directory
|
2015-10-03 15:07:37 +00:00
|
|
|
|
|
|
|
does nothing if dryrun is True. also expects
|
|
|
|
attic_cache_dir and borg_cache_dir to be set in the parent
|
|
|
|
scope, to the directories path including the repository
|
|
|
|
identifier.
|
|
|
|
|
2015-10-03 15:49:01 +00:00
|
|
|
:params path: the basename of the cache file to copy
|
2015-10-03 15:07:37 +00:00
|
|
|
(example: "files" or "chunks") as a string
|
|
|
|
|
2015-10-15 22:42:20 +00:00
|
|
|
:returns: the borg file that was created or None if no
|
|
|
|
Attic cache file was found.
|
2015-10-03 15:07:37 +00:00
|
|
|
|
|
|
|
"""
|
2015-10-03 15:49:01 +00:00
|
|
|
attic_file = os.path.join(attic_cache_dir, path)
|
2015-10-03 15:07:37 +00:00
|
|
|
if os.path.exists(attic_file):
|
2015-10-03 15:49:01 +00:00
|
|
|
borg_file = os.path.join(borg_cache_dir, path)
|
2015-10-03 15:07:37 +00:00
|
|
|
if os.path.exists(borg_file):
|
2015-10-17 13:58:40 +00:00
|
|
|
logger.warning("borg cache file already exists in %s, not copying from Attic", borg_file)
|
2015-10-01 20:27:19 +00:00
|
|
|
else:
|
2015-10-03 18:20:53 +00:00
|
|
|
logger.info("copying attic cache file from %s to %s" % (attic_file, borg_file))
|
2015-10-01 20:27:19 +00:00
|
|
|
if not dryrun:
|
2015-10-03 15:07:37 +00:00
|
|
|
shutil.copyfile(attic_file, borg_file)
|
2015-10-15 22:42:20 +00:00
|
|
|
return borg_file
|
2015-10-01 20:24:28 +00:00
|
|
|
else:
|
2015-10-03 18:20:53 +00:00
|
|
|
logger.warning("no %s cache file found in %s" % (path, attic_file))
|
2015-10-15 22:42:20 +00:00
|
|
|
return None
|
2015-10-03 15:07:37 +00:00
|
|
|
|
2015-10-03 15:52:12 +00:00
|
|
|
# XXX: untested, because generating cache files is a PITA, see
|
|
|
|
# Archiver.do_create() for proof
|
2015-10-03 15:07:37 +00:00
|
|
|
if os.path.exists(attic_cache_dir):
|
|
|
|
if not os.path.exists(borg_cache_dir):
|
|
|
|
os.makedirs(borg_cache_dir)
|
2015-10-03 15:52:12 +00:00
|
|
|
|
2015-10-03 16:56:03 +00:00
|
|
|
# file that we don't have a header to convert, just copy
|
|
|
|
for cache in ['config', 'files']:
|
|
|
|
copy_cache_file(cache)
|
2015-10-03 15:07:37 +00:00
|
|
|
|
2015-10-03 15:52:12 +00:00
|
|
|
# we need to convert the headers of those files, copy first
|
2015-10-03 16:56:03 +00:00
|
|
|
for cache in ['chunks']:
|
2015-10-15 22:42:20 +00:00
|
|
|
cache = copy_cache_file(cache)
|
|
|
|
logger.info("converting cache %s" % cache)
|
|
|
|
if not dryrun:
|
|
|
|
AtticRepositoryUpgrader.header_replace(cache, b'ATTICIDX', b'BORG_IDX')
|
2015-10-01 19:43:16 +00:00
|
|
|
|
2015-10-01 03:18:03 +00:00
|
|
|
|
|
|
|
class AtticKeyfileKey(KeyfileKey):
|
2015-10-01 12:44:17 +00:00
|
|
|
"""backwards compatible Attic key file parser"""
|
2015-10-01 03:18:03 +00:00
|
|
|
FILE_ID = 'ATTIC KEY'
|
|
|
|
|
|
|
|
# verbatim copy from attic
|
|
|
|
@staticmethod
|
|
|
|
def get_keys_dir():
|
|
|
|
"""Determine where to repository keys and cache"""
|
|
|
|
return os.environ.get('ATTIC_KEYS_DIR',
|
|
|
|
os.path.join(os.path.expanduser('~'), '.attic', 'keys'))
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def find_key_file(cls, repository):
|
2015-10-01 12:34:18 +00:00
|
|
|
"""copy of attic's `find_key_file`_
|
2015-10-01 03:18:03 +00:00
|
|
|
|
|
|
|
this has two small modifications:
|
|
|
|
|
|
|
|
1. it uses the above `get_keys_dir`_ instead of the global one,
|
|
|
|
assumed to be borg's
|
|
|
|
|
|
|
|
2. it uses `repository.path`_ instead of
|
|
|
|
`repository._location.canonical_path`_ because we can't
|
|
|
|
assume the repository has been opened by the archiver yet
|
2015-10-01 12:34:18 +00:00
|
|
|
"""
|
2015-10-01 03:18:03 +00:00
|
|
|
get_keys_dir = cls.get_keys_dir
|
|
|
|
id = hexlify(repository.id).decode('ascii')
|
|
|
|
keys_dir = get_keys_dir()
|
2016-01-18 17:12:44 +00:00
|
|
|
if not os.path.exists(keys_dir):
|
|
|
|
raise KeyfileNotFoundError(repository.path, keys_dir)
|
2015-10-01 03:18:03 +00:00
|
|
|
for name in os.listdir(keys_dir):
|
|
|
|
filename = os.path.join(keys_dir, name)
|
|
|
|
with open(filename, 'r') as fd:
|
|
|
|
line = fd.readline().strip()
|
|
|
|
if line and line.startswith(cls.FILE_ID) and line[10:] == id:
|
|
|
|
return filename
|
2016-01-18 17:12:44 +00:00
|
|
|
raise KeyfileNotFoundError(repository.path, keys_dir)
|
2016-01-29 00:23:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
class BorgRepositoryUpgrader(Repository):
|
|
|
|
def upgrade(self, dryrun=True, inplace=False, progress=False):
|
|
|
|
"""convert an old borg repository to a current borg repository
|
|
|
|
"""
|
|
|
|
logger.info("converting borg 0.xx to borg current")
|
|
|
|
try:
|
|
|
|
keyfile = self.find_borg0xx_keyfile()
|
|
|
|
except KeyfileNotFoundError:
|
|
|
|
logger.warning("no key file found for repository")
|
|
|
|
else:
|
|
|
|
self.move_keyfiles(keyfile, dryrun)
|
|
|
|
|
|
|
|
def find_borg0xx_keyfile(self):
|
|
|
|
return Borg0xxKeyfileKey.find_key_file(self)
|
|
|
|
|
|
|
|
def move_keyfiles(self, keyfile, dryrun):
|
|
|
|
filename = os.path.basename(keyfile)
|
|
|
|
new_keyfile = os.path.join(get_keys_dir(), filename)
|
|
|
|
try:
|
|
|
|
os.rename(keyfile, new_keyfile)
|
|
|
|
except FileExistsError:
|
|
|
|
# likely the attic -> borg upgrader already put it in the final location
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class Borg0xxKeyfileKey(KeyfileKey):
|
|
|
|
"""backwards compatible borg 0.xx key file parser"""
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_keys_dir():
|
|
|
|
return os.environ.get('BORG_KEYS_DIR',
|
|
|
|
os.path.join(os.path.expanduser('~'), '.borg', 'keys'))
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def find_key_file(cls, repository):
|
|
|
|
get_keys_dir = cls.get_keys_dir
|
|
|
|
id = hexlify(repository.id).decode('ascii')
|
|
|
|
keys_dir = get_keys_dir()
|
|
|
|
if not os.path.exists(keys_dir):
|
|
|
|
raise KeyfileNotFoundError(repository.path, keys_dir)
|
|
|
|
for name in os.listdir(keys_dir):
|
|
|
|
filename = os.path.join(keys_dir, name)
|
|
|
|
with open(filename, 'r') as fd:
|
|
|
|
line = fd.readline().strip()
|
2016-01-30 20:32:45 +00:00
|
|
|
if line and line.startswith(cls.FILE_ID) and line[len(cls.FILE_ID) + 1:] == id:
|
2016-01-29 00:23:24 +00:00
|
|
|
return filename
|
|
|
|
raise KeyfileNotFoundError(repository.path, keys_dir)
|