frontends.rst 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  1. .. include:: ../global.rst.inc
  2. .. highlight:: none
  3. .. _json_output:
  4. All about JSON: How to develop frontends
  5. ========================================
  6. Borg does not have a public API on the Python level. That does not keep you from writing :code:`import borg`,
  7. but does mean that there are no release-to-release guarantees on what you might find in that package, not
  8. even for point releases (1.1.x), and there is no documentation beyond the code and the internals documents.
  9. Borg does on the other hand provide an API on a command-line level. In other words, a frontend should to
  10. (for example) create a backup archive just invoke :ref:`borg_create`, give commandline parameters/options
  11. as needed and parse JSON output from borg.
  12. Important: JSON output is expected to be UTF-8, but currently borg depends on the locale being configured
  13. for that (must be a UTF-8 locale and *not* "C" or "ascii"), so that Python will choose to encode to UTF-8.
  14. The same applies to any inputs read by borg, they are expected to be UTF-8 encoded also.
  15. We consider this a bug (see :issue:`2273`) and might fix it later, so borg will use UTF-8 independent of
  16. the locale.
  17. On POSIX systems, you can usually set environment vars to choose a UTF-8 locale:
  18. ::
  19. export LANG=en_US.UTF-8
  20. export LC_CTYPE=en_US.UTF-8
  21. Logging
  22. -------
  23. Especially for graphical frontends it is important to be able to convey and reformat progress information
  24. in meaningful ways. The ``--log-json`` option turns the stderr stream of Borg into a stream of JSON lines,
  25. where each line is a JSON object. The *type* key of the object determines its other contents.
  26. Since JSON can only encode text, any string representing a file system path may miss non-text parts.
  27. The following types are in use. Progress information is governed by the usual rules for progress information,
  28. it is not produced unless ``--progress`` is specified.
  29. archive_progress
  30. Output during operations creating archives (:ref:`borg_create` and :ref:`borg_recreate`).
  31. The following keys exist, each represents the current progress.
  32. original_size
  33. Original size of data processed so far (before compression and deduplication)
  34. compressed_size
  35. Compressed size
  36. deduplicated_size
  37. Deduplicated size
  38. nfiles
  39. Number of (regular) files processed so far
  40. path
  41. Current path
  42. time
  43. Unix timestamp (float)
  44. progress_message
  45. A message-based progress information with no concrete progress information, just a message
  46. saying what is currently being worked on.
  47. operation
  48. unique, opaque integer ID of the operation
  49. :ref:`msgid <msgid>`
  50. Message ID of the operation (may be *null*)
  51. finished
  52. boolean indicating whether the operation has finished, only the last object for an *operation*
  53. can have this property set to *true*.
  54. message
  55. current progress message (may be empty/absent)
  56. time
  57. Unix timestamp (float)
  58. progress_percent
  59. Absolute progress information with defined end/total and current value.
  60. operation
  61. unique, opaque integer ID of the operation
  62. :ref:`msgid <msgid>`
  63. Message ID of the operation (may be *null*)
  64. finished
  65. boolean indicating whether the operation has finished, only the last object for an *operation*
  66. can have this property set to *true*.
  67. message
  68. A formatted progress message, this will include the percentage and perhaps other information
  69. current
  70. Current value (always less-or-equal to *total*)
  71. info
  72. Array that describes the current item, may be *null*, contents depend on *msgid*
  73. total
  74. Total value
  75. time
  76. Unix timestamp (float)
  77. file_status
  78. This is only output by :ref:`borg_create` and :ref:`borg_recreate` if ``--list`` is specified. The usual
  79. rules for the file listing applies, including the ``--filter`` option.
  80. status
  81. Single-character status as for regular list output
  82. path
  83. Path of the file system object
  84. log_message
  85. Any regular log output invokes this type. Regular log options and filtering applies to these as well.
  86. time
  87. Unix timestamp (float)
  88. levelname
  89. Upper-case log level name (also called severity). Defined levels are: DEBUG, INFO, WARNING, ERROR, CRITICAL
  90. name
  91. Name of the emitting entity
  92. message
  93. Formatted log message
  94. :ref:`msgid <msgid>`
  95. Message ID, may be *null* or absent
  96. See Prompts_ for the types used by prompts.
  97. .. rubric:: Examples (reformatted, each object would be on exactly one line)
  98. .. highlight:: json
  99. :ref:`borg_extract` progress::
  100. {"message": "100.0% Extracting: src/borgbackup.egg-info/entry_points.txt",
  101. "current": 13000228, "total": 13004993, "info": ["src/borgbackup.egg-info/entry_points.txt"],
  102. "operation": 1, "msgid": "extract", "type": "progress_percent", "finished": false}
  103. {"message": "100.0% Extracting: src/borgbackup.egg-info/SOURCES.txt",
  104. "current": 13004993, "total": 13004993, "info": ["src/borgbackup.egg-info/SOURCES.txt"],
  105. "operation": 1, "msgid": "extract", "type": "progress_percent", "finished": false}
  106. {"operation": 1, "msgid": "extract", "type": "progress_percent", "finished": true}
  107. :ref:`borg_create` file listing with progress::
  108. {"original_size": 0, "compressed_size": 0, "deduplicated_size": 0, "nfiles": 0, "type": "archive_progress", "path": "src"}
  109. {"type": "file_status", "status": "U", "path": "src/borgbackup.egg-info/entry_points.txt"}
  110. {"type": "file_status", "status": "U", "path": "src/borgbackup.egg-info/SOURCES.txt"}
  111. {"type": "file_status", "status": "d", "path": "src/borgbackup.egg-info"}
  112. {"type": "file_status", "status": "d", "path": "src"}
  113. {"original_size": 13176040, "compressed_size": 11386863, "deduplicated_size": 503, "nfiles": 277, "type": "archive_progress", "path": ""}
  114. Internal transaction progress::
  115. {"message": "Saving files cache", "operation": 2, "msgid": "cache.commit", "type": "progress_message", "finished": false}
  116. {"message": "Saving cache config", "operation": 2, "msgid": "cache.commit", "type": "progress_message", "finished": false}
  117. {"message": "Saving chunks cache", "operation": 2, "msgid": "cache.commit", "type": "progress_message", "finished": false}
  118. {"operation": 2, "msgid": "cache.commit", "type": "progress_message", "finished": true}
  119. A debug log message::
  120. {"message": "35 self tests completed in 0.08 seconds",
  121. "type": "log_message", "created": 1488278449.5575905, "levelname": "DEBUG", "name": "borg.archiver"}
  122. Prompts
  123. -------
  124. Prompts assume a JSON form as well when the ``--log-json`` option is specified. Responses
  125. are still read verbatim from *stdin*, while prompts are JSON messages printed to *stderr*,
  126. just like log messages.
  127. Prompts use the *question_prompt* and *question_prompt_retry* types for the prompt itself,
  128. and *question_invalid_answer*, *question_accepted_default*, *question_accepted_true*,
  129. *question_accepted_false* and *question_env_answer* types for information about
  130. prompt processing.
  131. The *message* property contains the same string displayed regularly in the same situation,
  132. while the *msgid* property may contain a msgid_, typically the name of the
  133. environment variable that can be used to override the prompt. It is the same for all JSON
  134. messages pertaining to the same prompt.
  135. .. rubric:: Examples (reformatted, each object would be on exactly one line)
  136. .. highlight:: none
  137. Providing an invalid answer::
  138. {"type": "question_prompt", "msgid": "BORG_CHECK_I_KNOW_WHAT_I_AM_DOING",
  139. "message": "... Type 'YES' if you understand this and want to continue: "}
  140. incorrect answer # input on stdin
  141. {"type": "question_invalid_answer", "msgid": "BORG_CHECK_I_KNOW_WHAT_I_AM_DOING", "is_prompt": false,
  142. "message": "Invalid answer, aborting."}
  143. Providing a false (negative) answer::
  144. {"type": "question_prompt", "msgid": "BORG_CHECK_I_KNOW_WHAT_I_AM_DOING",
  145. "message": "... Type 'YES' if you understand this and want to continue: "}
  146. NO # input on stdin
  147. {"type": "question_accepted_false", "msgid": "BORG_CHECK_I_KNOW_WHAT_I_AM_DOING",
  148. "message": "Aborting.", "is_prompt": false}
  149. Providing a true (affirmative) answer::
  150. {"type": "question_prompt", "msgid": "BORG_CHECK_I_KNOW_WHAT_I_AM_DOING",
  151. "message": "... Type 'YES' if you understand this and want to continue: "}
  152. YES # input on stdin
  153. # no further output, just like the prompt without --log-json
  154. Passphrase prompts
  155. ------------------
  156. Passphrase prompts should be handled differently. Use the environment variables *BORG_PASSPHRASE*
  157. and *BORG_NEW_PASSPHRASE* (see :ref:`env_vars` for reference) to pass passphrases to Borg, don't
  158. use the interactive passphrase prompts.
  159. When setting a new passphrase (:ref:`borg_init`, :ref:`borg_key_change-passphrase`) normally
  160. Borg prompts whether it should display the passphrase. This can be suppressed by setting
  161. the environment variable *BORG_DISPLAY_PASSPHRASE* to *no*.
  162. When "confronted" with an unknown repository, where the application does not know whether
  163. the repository is encrypted, the following algorithm can be followed to detect encryption:
  164. 1. Set *BORG_PASSPHRASE* to gibberish (for example a freshly generated UUID4, which cannot
  165. possibly be the passphrase)
  166. 2. Invoke ``borg list repository ...``
  167. 3. If this fails, due the repository being encrypted and the passphrase obviously being
  168. wrong, you'll get an error with the *PassphraseWrong* msgid.
  169. The repository is encrypted, for further access the application will need the passphrase.
  170. 4. If this does not fail, then the repository is not encrypted.
  171. Standard output
  172. ---------------
  173. *stdout* is different and more command-dependent than logging. Commands like :ref:`borg_info`, :ref:`borg_create`
  174. and :ref:`borg_list` implement a ``--json`` option which turns their regular output into a single JSON object.
  175. Dates are formatted according to ISO 8601 in local time. No explicit time zone is specified *at this time*
  176. (subject to change). The equivalent strftime format string is '%Y-%m-%dT%H:%M:%S.%f',
  177. e.g. ``2017-08-07T12:27:20.123456``.
  178. The root object at least contains a *repository* key with an object containing:
  179. id
  180. The ID of the repository, normally 64 hex characters
  181. location
  182. Canonicalized repository path, thus this may be different from what is specified on the command line
  183. last_modified
  184. Date when the repository was last modified by the Borg client
  185. The *encryption* key, if present, contains:
  186. mode
  187. Textual encryption mode name (same as :ref:`borg_init` ``--encryption`` names)
  188. keyfile
  189. Path to the local key file used for access. Depending on *mode* this key may be absent.
  190. The *cache* key, if present, contains:
  191. path
  192. Path to the local repository cache
  193. stats
  194. Object containing cache stats:
  195. total_chunks
  196. Number of chunks
  197. total_unique_chunks
  198. Number of unique chunks
  199. total_size
  200. Total uncompressed size of all chunks multiplied with their reference counts
  201. total_csize
  202. Total compressed and encrypted size of all chunks multiplied with their reference counts
  203. unique_size
  204. Uncompressed size of all chunks
  205. unique_csize
  206. Compressed and encrypted size of all chunks
  207. .. highlight: json
  208. Example *borg info* output::
  209. {
  210. "cache": {
  211. "path": "/home/user/.cache/borg/0cbe6166b46627fd26b97f8831e2ca97584280a46714ef84d2b668daf8271a23",
  212. "stats": {
  213. "total_chunks": 511533,
  214. "total_csize": 17948017540,
  215. "total_size": 22635749792,
  216. "total_unique_chunks": 54892,
  217. "unique_csize": 1920405405,
  218. "unique_size": 2449675468
  219. }
  220. },
  221. "encryption": {
  222. "mode": "repokey"
  223. },
  224. "repository": {
  225. "id": "0cbe6166b46627fd26b97f8831e2ca97584280a46714ef84d2b668daf8271a23",
  226. "last_modified": "2017-08-07T12:27:20.789123",
  227. "location": "/home/user/testrepo"
  228. },
  229. "security_dir": "/home/user/.config/borg/security/0cbe6166b46627fd26b97f8831e2ca97584280a46714ef84d2b668daf8271a23",
  230. "archives": []
  231. }
  232. Archive formats
  233. +++++++++++++++
  234. :ref:`borg_info` uses an extended format for archives, which is more expensive to retrieve, while
  235. :ref:`borg_list` uses a simpler format that is faster to retrieve. Either return archives in an
  236. array under the *archives* key, while :ref:`borg_create` returns a single archive object under the
  237. *archive* key.
  238. Both formats contain a *name* key with the archive name, the *id* key with the hexadecimal archive ID,
  239. and the *start* key with the start timestamp.
  240. *borg info* and *borg create* further have:
  241. end
  242. End timestamp
  243. duration
  244. Duration in seconds between start and end in seconds (float)
  245. stats
  246. Archive statistics (freshly calculated, this is what makes "info" more expensive)
  247. original_size
  248. Size of files and metadata before compression
  249. compressed_size
  250. Size after compression
  251. deduplicated_size
  252. Deduplicated size (against the current repository, not when the archive was created)
  253. nfiles
  254. Number of regular files in the archive
  255. limits
  256. Object describing the utilization of Borg limits
  257. max_archive_size
  258. Float between 0 and 1 describing how large this archive is relative to the maximum size allowed by Borg
  259. command_line
  260. Array of strings of the command line that created the archive
  261. The note about paths from above applies here as well.
  262. chunker_params
  263. The chunker parameters the archive has been created with.
  264. :ref:`borg_info` further has:
  265. hostname
  266. Hostname of the creating host
  267. username
  268. Name of the creating user
  269. comment
  270. Archive comment, if any
  271. Some keys/values are more expensive to compute than others (e.g. because it requires opening the archive,
  272. not just the manifest). To optimize for speed, `borg list repo` does not determine these values except
  273. when they are requested. The `--format` option is used for that (for normal mode as well as for `--json`
  274. mode), so, to have the comment included in the json output, you will need:
  275. ::
  276. borg list repo --format "{name}{comment}" --json`
  277. Example of a simple archive listing (``borg list --last 1 --json``)::
  278. {
  279. "archives": [
  280. {
  281. "id": "80cd07219ad725b3c5f665c1dcf119435c4dee1647a560ecac30f8d40221a46a",
  282. "name": "host-system-backup-2017-02-27",
  283. "start": "2017-08-07T12:27:20.789123"
  284. }
  285. ],
  286. "encryption": {
  287. "mode": "repokey"
  288. },
  289. "repository": {
  290. "id": "0cbe6166b46627fd26b97f8831e2ca97584280a46714ef84d2b668daf8271a23",
  291. "last_modified": "2017-08-07T12:27:20.789123",
  292. "location": "/home/user/repository"
  293. }
  294. }
  295. The same archive with more information (``borg info --last 1 --json``)::
  296. {
  297. "archives": [
  298. {
  299. "chunker_params": [
  300. "buzhash",
  301. 13,
  302. 23,
  303. 16,
  304. 4095
  305. ],
  306. "command_line": [
  307. "/home/user/.local/bin/borg",
  308. "create",
  309. "/home/user/repository",
  310. "..."
  311. ],
  312. "comment": "",
  313. "duration": 5.641542,
  314. "end": "2017-02-27T12:27:20.789123",
  315. "hostname": "host",
  316. "id": "80cd07219ad725b3c5f665c1dcf119435c4dee1647a560ecac30f8d40221a46a",
  317. "limits": {
  318. "max_archive_size": 0.0001330855110409714
  319. },
  320. "name": "host-system-backup-2017-02-27",
  321. "start": "2017-02-27T12:27:20.789123",
  322. "stats": {
  323. "compressed_size": 1880961894,
  324. "deduplicated_size": 2791,
  325. "nfiles": 53669,
  326. "original_size": 2400471280
  327. },
  328. "username": "user"
  329. }
  330. ],
  331. "cache": {
  332. "path": "/home/user/.cache/borg/0cbe6166b46627fd26b97f8831e2ca97584280a46714ef84d2b668daf8271a23",
  333. "stats": {
  334. "total_chunks": 511533,
  335. "total_csize": 17948017540,
  336. "total_size": 22635749792,
  337. "total_unique_chunks": 54892,
  338. "unique_csize": 1920405405,
  339. "unique_size": 2449675468
  340. }
  341. },
  342. "encryption": {
  343. "mode": "repokey"
  344. },
  345. "repository": {
  346. "id": "0cbe6166b46627fd26b97f8831e2ca97584280a46714ef84d2b668daf8271a23",
  347. "last_modified": "2017-08-07T12:27:20.789123",
  348. "location": "/home/user/repository"
  349. }
  350. }
  351. File listings
  352. +++++++++++++
  353. Listing the contents of an archive can produce *a lot* of JSON. Since many JSON implementations
  354. don't support a streaming mode of operation, which is pretty much required to deal with this amount of
  355. JSON, output is generated in the `JSON lines <http://jsonlines.org/>`_ format, which is simply
  356. a number of JSON objects separated by new lines.
  357. Each item (file, directory, ...) is described by one object in the :ref:`borg_list` output.
  358. Refer to the *borg list* documentation for the available keys and their meaning.
  359. Example (excerpt) of ``borg list --json-lines``::
  360. {"type": "d", "mode": "drwxr-xr-x", "user": "user", "group": "user", "uid": 1000, "gid": 1000, "path": "linux", "healthy": true, "source": "", "linktarget": "", "flags": null, "mtime": "2017-02-27T12:27:20.023407", "size": 0}
  361. {"type": "d", "mode": "drwxr-xr-x", "user": "user", "group": "user", "uid": 1000, "gid": 1000, "path": "linux/baz", "healthy": true, "source": "", "linktarget": "", "flags": null, "mtime": "2017-02-27T12:27:20.585407", "size": 0}
  362. .. _msgid:
  363. Message IDs
  364. -----------
  365. Message IDs are strings that essentially give a log message or operation a name, without actually using the
  366. full text, since texts change more frequently. Message IDs are unambiguous and reduce the need to parse
  367. log messages.
  368. Assigned message IDs are:
  369. .. See scripts/errorlist.py; this is slightly edited.
  370. Errors
  371. Archive.AlreadyExists
  372. Archive {} already exists
  373. Archive.DoesNotExist
  374. Archive {} does not exist
  375. Archive.IncompatibleFilesystemEncodingError
  376. Failed to encode filename "{}" into file system encoding "{}". Consider configuring the LANG environment variable.
  377. Cache.CacheInitAbortedError
  378. Cache initialization aborted
  379. Cache.EncryptionMethodMismatch
  380. Repository encryption method changed since last access, refusing to continue
  381. Cache.RepositoryAccessAborted
  382. Repository access aborted
  383. Cache.RepositoryIDNotUnique
  384. Cache is newer than repository - do you have multiple, independently updated repos with same ID?
  385. Cache.RepositoryReplay
  386. Cache is newer than repository - this is either an attack or unsafe (multiple repos with same ID)
  387. Buffer.MemoryLimitExceeded
  388. Requested buffer size {} is above the limit of {}.
  389. ExtensionModuleError
  390. The Borg binary extension modules do not seem to be properly installed
  391. IntegrityError
  392. Data integrity error: {}
  393. NoManifestError
  394. Repository has no manifest.
  395. PlaceholderError
  396. Formatting Error: "{}".format({}): {}({})
  397. KeyfileInvalidError
  398. Invalid key file for repository {} found in {}.
  399. KeyfileMismatchError
  400. Mismatch between repository {} and key file {}.
  401. KeyfileNotFoundError
  402. No key file for repository {} found in {}.
  403. PassphraseWrong
  404. passphrase supplied in BORG_PASSPHRASE is incorrect
  405. PasswordRetriesExceeded
  406. exceeded the maximum password retries
  407. RepoKeyNotFoundError
  408. No key entry found in the config of repository {}.
  409. UnsupportedManifestError
  410. Unsupported manifest envelope. A newer version is required to access this repository.
  411. UnsupportedPayloadError
  412. Unsupported payload type {}. A newer version is required to access this repository.
  413. NotABorgKeyFile
  414. This file is not a borg key backup, aborting.
  415. RepoIdMismatch
  416. This key backup seems to be for a different backup repository, aborting.
  417. UnencryptedRepo
  418. Keymanagement not available for unencrypted repositories.
  419. UnknownKeyType
  420. Keytype {0} is unknown.
  421. LockError
  422. Failed to acquire the lock {}.
  423. LockErrorT
  424. Failed to acquire the lock {}.
  425. ConnectionClosed
  426. Connection closed by remote host
  427. InvalidRPCMethod
  428. RPC method {} is not valid
  429. PathNotAllowed
  430. Repository path not allowed
  431. RemoteRepository.RPCServerOutdated
  432. Borg server is too old for {}. Required version {}
  433. UnexpectedRPCDataFormatFromClient
  434. Borg {}: Got unexpected RPC data format from client.
  435. UnexpectedRPCDataFormatFromServer
  436. Got unexpected RPC data format from server:
  437. {}
  438. Repository.AlreadyExists
  439. Repository {} already exists.
  440. Repository.CheckNeeded
  441. Inconsistency detected. Please run "borg check {}".
  442. Repository.DoesNotExist
  443. Repository {} does not exist.
  444. Repository.InsufficientFreeSpaceError
  445. Insufficient free space to complete transaction (required: {}, available: {}).
  446. Repository.InvalidRepository
  447. {} is not a valid repository. Check repo config.
  448. Repository.AtticRepository
  449. Attic repository detected. Please run "borg upgrade {}".
  450. Repository.ObjectNotFound
  451. Object with key {} not found in repository {}.
  452. Operations
  453. - cache.begin_transaction
  454. - cache.download_chunks, appears with ``borg create --no-cache-sync``
  455. - cache.commit
  456. - cache.sync
  457. *info* is one string element, the name of the archive currently synced.
  458. - repository.compact_segments
  459. - repository.replay_segments
  460. - repository.check
  461. - check.verify_data
  462. - check.rebuild_manifest
  463. - extract
  464. *info* is one string element, the name of the path currently extracted.
  465. - extract.permissions
  466. - archive.delete
  467. - archive.calc_stats
  468. - prune
  469. - upgrade.convert_segments
  470. Prompts
  471. BORG_UNKNOWN_UNENCRYPTED_REPO_ACCESS_IS_OK
  472. For "Warning: Attempting to access a previously unknown unencrypted repository"
  473. BORG_RELOCATED_REPO_ACCESS_IS_OK
  474. For "Warning: The repository at location ... was previously located at ..."
  475. BORG_CHECK_I_KNOW_WHAT_I_AM_DOING
  476. For "Warning: 'check --repair' is an experimental feature that might result in data loss."
  477. BORG_DELETE_I_KNOW_WHAT_I_AM_DOING
  478. For "You requested to completely DELETE the repository *including* all archives it contains:"
  479. BORG_RECREATE_I_KNOW_WHAT_I_AM_DOING
  480. For "recreate is an experimental feature."