upgrader.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. from binascii import hexlify
  2. import datetime
  3. import logging
  4. logger = logging.getLogger(__name__)
  5. import os
  6. import shutil
  7. import time
  8. from .helpers import get_keys_dir, get_cache_dir, ProgressIndicatorPercent
  9. from .locking import UpgradableLock
  10. from .repository import Repository, MAGIC
  11. from .key import KeyfileKey, KeyfileNotFoundError
  12. ATTIC_MAGIC = b'ATTICSEG'
  13. class AtticRepositoryUpgrader(Repository):
  14. def __init__(self, *args, **kw):
  15. kw['lock'] = False # do not create borg lock files (now) in attic repo
  16. super().__init__(*args, **kw)
  17. def upgrade(self, dryrun=True, inplace=False, progress=False):
  18. """convert an attic repository to a borg repository
  19. those are the files that need to be upgraded here, from most
  20. important to least important: segments, key files, and various
  21. caches, the latter being optional, as they will be rebuilt if
  22. missing.
  23. we nevertheless do the order in reverse, as we prefer to do
  24. the fast stuff first, to improve interactivity.
  25. """
  26. with self:
  27. backup = None
  28. if not inplace:
  29. backup = '{}.upgrade-{:%Y-%m-%d-%H:%M:%S}'.format(self.path, datetime.datetime.now())
  30. logger.info('making a hardlink copy in %s', backup)
  31. if not dryrun:
  32. shutil.copytree(self.path, backup, copy_function=os.link)
  33. logger.info("opening attic repository with borg and converting")
  34. # now lock the repo, after we have made the copy
  35. self.lock = UpgradableLock(os.path.join(self.path, 'lock'), exclusive=True, timeout=1.0).acquire()
  36. segments = [filename for i, filename in self.io.segment_iterator()]
  37. try:
  38. keyfile = self.find_attic_keyfile()
  39. except KeyfileNotFoundError:
  40. logger.warning("no key file found for repository")
  41. else:
  42. self.convert_keyfiles(keyfile, dryrun)
  43. # partial open: just hold on to the lock
  44. self.lock = UpgradableLock(os.path.join(self.path, 'lock'),
  45. exclusive=True).acquire()
  46. try:
  47. self.convert_cache(dryrun)
  48. self.convert_repo_index(dryrun=dryrun, inplace=inplace)
  49. self.convert_segments(segments, dryrun=dryrun, inplace=inplace, progress=progress)
  50. self.borg_readme()
  51. finally:
  52. self.lock.release()
  53. self.lock = None
  54. return backup
  55. def borg_readme(self):
  56. readme = os.path.join(self.path, 'README')
  57. os.remove(readme)
  58. with open(readme, 'w') as fd:
  59. fd.write('This is a Borg repository\n')
  60. @staticmethod
  61. def convert_segments(segments, dryrun=True, inplace=False, progress=False):
  62. """convert repository segments from attic to borg
  63. replacement pattern is `s/ATTICSEG/BORG_SEG/` in files in
  64. `$ATTIC_REPO/data/**`.
  65. luckily the magic string length didn't change so we can just
  66. replace the 8 first bytes of all regular files in there."""
  67. logger.info("converting %d segments..." % len(segments))
  68. segment_count = len(segments)
  69. pi = ProgressIndicatorPercent(total=segment_count, msg="Converting segments %3.0f%%", same_line=True)
  70. for i, filename in enumerate(segments):
  71. if progress:
  72. pi.show(i)
  73. if dryrun:
  74. time.sleep(0.001)
  75. else:
  76. AtticRepositoryUpgrader.header_replace(filename, ATTIC_MAGIC, MAGIC, inplace=inplace)
  77. if progress:
  78. pi.finish()
  79. @staticmethod
  80. def header_replace(filename, old_magic, new_magic, inplace=True):
  81. with open(filename, 'r+b') as segment:
  82. segment.seek(0)
  83. # only write if necessary
  84. if segment.read(len(old_magic)) == old_magic:
  85. if inplace:
  86. segment.seek(0)
  87. segment.write(new_magic)
  88. else:
  89. # rename the hardlink and rewrite the file. this works
  90. # because the file is still open. so even though the file
  91. # is renamed, we can still read it until it is closed.
  92. os.rename(filename, filename + '.tmp')
  93. with open(filename, 'wb') as new_segment:
  94. new_segment.write(new_magic)
  95. new_segment.write(segment.read())
  96. # the little dance with the .tmp file is necessary
  97. # because Windows won't allow overwriting an open file.
  98. os.unlink(filename + '.tmp')
  99. def find_attic_keyfile(self):
  100. """find the attic keyfiles
  101. the keyfiles are loaded by `KeyfileKey.find_key_file()`. that
  102. finds the keys with the right identifier for the repo.
  103. this is expected to look into $HOME/.attic/keys or
  104. $ATTIC_KEYS_DIR for key files matching the given Borg
  105. repository.
  106. it is expected to raise an exception (KeyfileNotFoundError) if
  107. no key is found. whether that exception is from Borg or Attic
  108. is unclear.
  109. this is split in a separate function in case we want to use
  110. the attic code here directly, instead of our local
  111. implementation."""
  112. return AtticKeyfileKey.find_key_file(self)
  113. @staticmethod
  114. def convert_keyfiles(keyfile, dryrun):
  115. """convert key files from attic to borg
  116. replacement pattern is `s/ATTIC KEY/BORG_KEY/` in
  117. `get_keys_dir()`, that is `$ATTIC_KEYS_DIR` or
  118. `$HOME/.attic/keys`, and moved to `$BORG_KEYS_DIR` or
  119. `$HOME/.config/borg/keys`.
  120. no need to decrypt to convert. we need to rewrite the whole
  121. key file because magic string length changed, but that's not a
  122. problem because the keyfiles are small (compared to, say,
  123. all the segments)."""
  124. logger.info("converting keyfile %s" % keyfile)
  125. with open(keyfile, 'r') as f:
  126. data = f.read()
  127. data = data.replace(AtticKeyfileKey.FILE_ID, KeyfileKey.FILE_ID, 1)
  128. keyfile = os.path.join(get_keys_dir(), os.path.basename(keyfile))
  129. logger.info("writing borg keyfile to %s" % keyfile)
  130. if not dryrun:
  131. with open(keyfile, 'w') as f:
  132. f.write(data)
  133. def convert_repo_index(self, dryrun, inplace):
  134. """convert some repo files
  135. those are all hash indexes, so we need to
  136. `s/ATTICIDX/BORG_IDX/` in a few locations:
  137. * the repository index (in `$ATTIC_REPO/index.%d`, where `%d`
  138. is the `Repository.get_index_transaction_id()`), which we
  139. should probably update, with a lock, see
  140. `Repository.open()`, which i'm not sure we should use
  141. because it may write data on `Repository.close()`...
  142. """
  143. transaction_id = self.get_index_transaction_id()
  144. if transaction_id is None:
  145. logger.warning('no index file found for repository %s' % self.path)
  146. else:
  147. index = os.path.join(self.path, 'index.%d' % transaction_id)
  148. logger.info("converting repo index %s" % index)
  149. if not dryrun:
  150. AtticRepositoryUpgrader.header_replace(index, b'ATTICIDX', b'BORG_IDX', inplace=inplace)
  151. def convert_cache(self, dryrun):
  152. """convert caches from attic to borg
  153. those are all hash indexes, so we need to
  154. `s/ATTICIDX/BORG_IDX/` in a few locations:
  155. * the `files` and `chunks` cache (in `$ATTIC_CACHE_DIR` or
  156. `$HOME/.cache/attic/<repoid>/`), which we could just drop,
  157. but if we'd want to convert, we could open it with the
  158. `Cache.open()`, edit in place and then `Cache.close()` to
  159. make sure we have locking right
  160. """
  161. # copy of attic's get_cache_dir()
  162. attic_cache_dir = os.environ.get('ATTIC_CACHE_DIR',
  163. os.path.join(os.path.expanduser('~'),
  164. '.cache', 'attic'))
  165. attic_cache_dir = os.path.join(attic_cache_dir, hexlify(self.id).decode('ascii'))
  166. borg_cache_dir = os.path.join(get_cache_dir(), hexlify(self.id).decode('ascii'))
  167. def copy_cache_file(path):
  168. """copy the given attic cache path into the borg directory
  169. does nothing if dryrun is True. also expects
  170. attic_cache_dir and borg_cache_dir to be set in the parent
  171. scope, to the directories path including the repository
  172. identifier.
  173. :params path: the basename of the cache file to copy
  174. (example: "files" or "chunks") as a string
  175. :returns: the borg file that was created or None if no
  176. Attic cache file was found.
  177. """
  178. attic_file = os.path.join(attic_cache_dir, path)
  179. if os.path.exists(attic_file):
  180. borg_file = os.path.join(borg_cache_dir, path)
  181. if os.path.exists(borg_file):
  182. logger.warning("borg cache file already exists in %s, not copying from Attic", borg_file)
  183. else:
  184. logger.info("copying attic cache file from %s to %s" % (attic_file, borg_file))
  185. if not dryrun:
  186. shutil.copyfile(attic_file, borg_file)
  187. return borg_file
  188. else:
  189. logger.warning("no %s cache file found in %s" % (path, attic_file))
  190. return None
  191. # XXX: untested, because generating cache files is a PITA, see
  192. # Archiver.do_create() for proof
  193. if os.path.exists(attic_cache_dir):
  194. if not os.path.exists(borg_cache_dir):
  195. os.makedirs(borg_cache_dir)
  196. # file that we don't have a header to convert, just copy
  197. for cache in ['config', 'files']:
  198. copy_cache_file(cache)
  199. # we need to convert the headers of those files, copy first
  200. for cache in ['chunks']:
  201. cache = copy_cache_file(cache)
  202. logger.info("converting cache %s" % cache)
  203. if not dryrun:
  204. AtticRepositoryUpgrader.header_replace(cache, b'ATTICIDX', b'BORG_IDX')
  205. class AtticKeyfileKey(KeyfileKey):
  206. """backwards compatible Attic key file parser"""
  207. FILE_ID = 'ATTIC KEY'
  208. # verbatim copy from attic
  209. @staticmethod
  210. def get_keys_dir():
  211. """Determine where to repository keys and cache"""
  212. return os.environ.get('ATTIC_KEYS_DIR',
  213. os.path.join(os.path.expanduser('~'), '.attic', 'keys'))
  214. @classmethod
  215. def find_key_file(cls, repository):
  216. """copy of attic's `find_key_file`_
  217. this has two small modifications:
  218. 1. it uses the above `get_keys_dir`_ instead of the global one,
  219. assumed to be borg's
  220. 2. it uses `repository.path`_ instead of
  221. `repository._location.canonical_path`_ because we can't
  222. assume the repository has been opened by the archiver yet
  223. """
  224. get_keys_dir = cls.get_keys_dir
  225. id = hexlify(repository.id).decode('ascii')
  226. keys_dir = get_keys_dir()
  227. if not os.path.exists(keys_dir):
  228. raise KeyfileNotFoundError(repository.path, keys_dir)
  229. for name in os.listdir(keys_dir):
  230. filename = os.path.join(keys_dir, name)
  231. with open(filename, 'r') as fd:
  232. line = fd.readline().strip()
  233. if line and line.startswith(cls.FILE_ID) and line[10:] == id:
  234. return filename
  235. raise KeyfileNotFoundError(repository.path, keys_dir)
  236. class BorgRepositoryUpgrader(Repository):
  237. def upgrade(self, dryrun=True, inplace=False, progress=False):
  238. """convert an old borg repository to a current borg repository
  239. """
  240. logger.info("converting borg 0.xx to borg current")
  241. with self:
  242. try:
  243. keyfile = self.find_borg0xx_keyfile()
  244. except KeyfileNotFoundError:
  245. logger.warning("no key file found for repository")
  246. else:
  247. self.move_keyfiles(keyfile, dryrun)
  248. def find_borg0xx_keyfile(self):
  249. return Borg0xxKeyfileKey.find_key_file(self)
  250. def move_keyfiles(self, keyfile, dryrun):
  251. filename = os.path.basename(keyfile)
  252. new_keyfile = os.path.join(get_keys_dir(), filename)
  253. try:
  254. os.rename(keyfile, new_keyfile)
  255. except FileExistsError:
  256. # likely the attic -> borg upgrader already put it in the final location
  257. pass
  258. class Borg0xxKeyfileKey(KeyfileKey):
  259. """backwards compatible borg 0.xx key file parser"""
  260. @staticmethod
  261. def get_keys_dir():
  262. return os.environ.get('BORG_KEYS_DIR',
  263. os.path.join(os.path.expanduser('~'), '.borg', 'keys'))
  264. @classmethod
  265. def find_key_file(cls, repository):
  266. get_keys_dir = cls.get_keys_dir
  267. id = hexlify(repository.id).decode('ascii')
  268. keys_dir = get_keys_dir()
  269. if not os.path.exists(keys_dir):
  270. raise KeyfileNotFoundError(repository.path, keys_dir)
  271. for name in os.listdir(keys_dir):
  272. filename = os.path.join(keys_dir, name)
  273. with open(filename, 'r') as fd:
  274. line = fd.readline().strip()
  275. if line and line.startswith(cls.FILE_ID) and line[len(cls.FILE_ID) + 1:] == id:
  276. return filename
  277. raise KeyfileNotFoundError(repository.path, keys_dir)