converter.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. from binascii import hexlify
  2. import os
  3. import shutil
  4. import time
  5. from .helpers import get_keys_dir, get_cache_dir
  6. from .locking import UpgradableLock
  7. from .repository import Repository, MAGIC
  8. from .key import KeyfileKey, KeyfileNotFoundError
  9. ATTIC_MAGIC = b'ATTICSEG'
  10. class AtticRepositoryConverter(Repository):
  11. def convert(self, dryrun=True):
  12. """convert an attic repository to a borg repository
  13. those are the files that need to be converted here, from most
  14. important to least important: segments, key files, and various
  15. caches, the latter being optional, as they will be rebuilt if
  16. missing."""
  17. print("reading segments from attic repository using borg")
  18. # we need to open it to load the configuration and other fields
  19. self.open(self.path, exclusive=False)
  20. segments = [ filename for i, filename in self.io.segment_iterator() ]
  21. try:
  22. keyfile = self.find_attic_keyfile()
  23. except KeyfileNotFoundError:
  24. print("no key file found for repository")
  25. else:
  26. self.convert_keyfiles(keyfile, dryrun)
  27. self.close()
  28. # partial open: just hold on to the lock
  29. self.lock = UpgradableLock(os.path.join(self.path, 'lock'),
  30. exclusive=True).acquire()
  31. try:
  32. self.convert_segments(segments, dryrun)
  33. self.convert_cache(dryrun)
  34. finally:
  35. self.lock.release()
  36. self.lock = None
  37. @staticmethod
  38. def convert_segments(segments, dryrun):
  39. """convert repository segments from attic to borg
  40. replacement pattern is `s/ATTICSEG/BORG_SEG/` in files in
  41. `$ATTIC_REPO/data/**`.
  42. luckily the magic string length didn't change so we can just
  43. replace the 8 first bytes of all regular files in there."""
  44. print("converting %d segments..." % len(segments))
  45. i = 0
  46. for filename in segments:
  47. i += 1
  48. print("\rconverting segment %d/%d in place, %.2f%% done (%s)"
  49. % (i, len(segments), float(i)/len(segments), filename), end='')
  50. if dryrun:
  51. time.sleep(0.001)
  52. else:
  53. AtticRepositoryConverter.header_replace(filename, ATTIC_MAGIC, MAGIC)
  54. print()
  55. @staticmethod
  56. def header_replace(filename, old_magic, new_magic):
  57. print("changing header on %s" % filename)
  58. with open(filename, 'r+b') as segment:
  59. segment.seek(0)
  60. # only write if necessary
  61. if (segment.read(len(old_magic)) == old_magic):
  62. segment.seek(0)
  63. segment.write(new_magic)
  64. def find_attic_keyfile(self):
  65. """find the attic keyfiles
  66. the keyfiles are loaded by `KeyfileKey.find_key_file()`. that
  67. finds the keys with the right identifier for the repo.
  68. this is expected to look into $HOME/.attic/keys or
  69. $ATTIC_KEYS_DIR for key files matching the given Borg
  70. repository.
  71. it is expected to raise an exception (KeyfileNotFoundError) if
  72. no key is found. whether that exception is from Borg or Attic
  73. is unclear.
  74. this is split in a separate function in case we want to use
  75. the attic code here directly, instead of our local
  76. implementation."""
  77. return AtticKeyfileKey.find_key_file(self)
  78. @staticmethod
  79. def convert_keyfiles(keyfile, dryrun):
  80. """convert key files from attic to borg
  81. replacement pattern is `s/ATTIC KEY/BORG_KEY/` in
  82. `get_keys_dir()`, that is `$ATTIC_KEYS_DIR` or
  83. `$HOME/.attic/keys`, and moved to `$BORG_KEYS_DIR` or
  84. `$HOME/.borg/keys`.
  85. no need to decrypt to convert. we need to rewrite the whole
  86. key file because magic string length changed, but that's not a
  87. problem because the keyfiles are small (compared to, say,
  88. all the segments)."""
  89. print("converting keyfile %s" % keyfile)
  90. with open(keyfile, 'r') as f:
  91. data = f.read()
  92. data = data.replace(AtticKeyfileKey.FILE_ID, KeyfileKey.FILE_ID, 1)
  93. keyfile = os.path.join(get_keys_dir(), os.path.basename(keyfile))
  94. print("writing borg keyfile to %s" % keyfile)
  95. if not dryrun:
  96. with open(keyfile, 'w') as f:
  97. f.write(data)
  98. def convert_cache(self, dryrun):
  99. """convert caches from attic to borg
  100. those are all hash indexes, so we need to
  101. `s/ATTICIDX/BORG_IDX/` in a few locations:
  102. * the repository index (in `$ATTIC_REPO/index.%d`, where `%d`
  103. is the `Repository.get_index_transaction_id()`), which we
  104. should probably update, with a lock, see
  105. `Repository.open()`, which i'm not sure we should use
  106. because it may write data on `Repository.close()`...
  107. * the `files` and `chunks` cache (in `$ATTIC_CACHE_DIR` or
  108. `$HOME/.cache/attic/<repoid>/`), which we could just drop,
  109. but if we'd want to convert, we could open it with the
  110. `Cache.open()`, edit in place and then `Cache.close()` to
  111. make sure we have locking right
  112. """
  113. caches = []
  114. transaction_id = self.get_index_transaction_id()
  115. if transaction_id is None:
  116. print('no index file found for repository %s' % self.path)
  117. else:
  118. caches += [os.path.join(self.path, 'index.%d' % transaction_id).encode('utf-8')]
  119. # copy of attic's get_cache_dir()
  120. attic_cache_dir = os.environ.get('ATTIC_CACHE_DIR',
  121. os.path.join(os.path.expanduser('~'), '.cache', 'attic'))
  122. # XXX: untested, because generating cache files is a PITA, see
  123. # Archiver.do_create() for proof
  124. for cache in [ 'files', 'chunks' ]:
  125. attic_cache = os.path.join(attic_cache_dir, hexlify(self.id).decode('ascii'), cache)
  126. if os.path.exists(attic_cache):
  127. borg_cache = os.path.join(get_cache_dir(), hexlify(self.id).decode('ascii'), cache)
  128. shutil.copy(attic_cache, borg_cache)
  129. caches += [borg_cache]
  130. for cache in caches:
  131. print("converting cache %s" % cache)
  132. AtticRepositoryConverter.header_replace(cache, b'ATTICIDX', b'BORG_IDX')
  133. class AtticKeyfileKey(KeyfileKey):
  134. """backwards compatible Attic key file parser"""
  135. FILE_ID = 'ATTIC KEY'
  136. # verbatim copy from attic
  137. @staticmethod
  138. def get_keys_dir():
  139. """Determine where to repository keys and cache"""
  140. return os.environ.get('ATTIC_KEYS_DIR',
  141. os.path.join(os.path.expanduser('~'), '.attic', 'keys'))
  142. @classmethod
  143. def find_key_file(cls, repository):
  144. """copy of attic's `find_key_file`_
  145. this has two small modifications:
  146. 1. it uses the above `get_keys_dir`_ instead of the global one,
  147. assumed to be borg's
  148. 2. it uses `repository.path`_ instead of
  149. `repository._location.canonical_path`_ because we can't
  150. assume the repository has been opened by the archiver yet
  151. """
  152. get_keys_dir = cls.get_keys_dir
  153. id = hexlify(repository.id).decode('ascii')
  154. keys_dir = get_keys_dir()
  155. for name in os.listdir(keys_dir):
  156. filename = os.path.join(keys_dir, name)
  157. with open(filename, 'r') as fd:
  158. line = fd.readline().strip()
  159. if line and line.startswith(cls.FILE_ID) and line[10:] == id:
  160. return filename
  161. raise KeyfileNotFoundError(repository.path, get_keys_dir())