frontends.rst 21 KB

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