frontends.rst 22 KB

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