Duply-code

From duply
Jump to: navigation, search

duply sourcecode

duply's svn repository is not the most recent source for the code. Usually the svn only gets updated on releases. But not always. Development snapshots are taken daily. These are not guaranteed to work, but might contain unreleased fixes. Check the snapshot's changelog header accordingly.

SVN access

Browse the repository

http://ftplicity.svn.sourceforge.net/viewvc/ftplicity/duply/trunk/

SVN access

svn co https://ftplicity.svn.sourceforge.net/svnroot/ftplicity/duply/trunk duply

SVN Log

http://ftplicity.svn.sourceforge.net/viewvc/ftplicity/duply/?view=log

Latest Development Snapshot

mod time 2018-06-24

plain/text -> http://duply.net/tmp/duply.sh

  1. #!/usr/bin/env bash
  2. #
  3. ################################################################################
  4. # duply (grown out of ftplicity), is a shell front end to duplicity that #
  5. # simplifies the usage by managing settings for backup jobs in profiles. #
  6. # It supports executing multiple commands in a batch mode to enable single #
  7. # line cron entries and executes pre/post backup scripts. #
  8. # Since version 1.5.0 all duplicity backends are supported. Hence the name #
  9. # changed from ftplicity to duply. #
  10. # See http://duply.net or http://ftplicity.sourceforge.net/ for more info. #
  11. # (c) 2006 Christiane Ruetten, Heise Zeitschriften Verlag, Germany #
  12. # (c) 2008-2018 Edgar Soldin (changes since version 1.3) #
  13. ################################################################################
  14. # LICENSE: #
  15. # This program is licensed under GPLv2. #
  16. # Please read the accompanying license information in gpl.txt. #
  17. ################################################################################
  18. # TODO/IDEAS/KNOWN PROBLEMS:
  19. # - possibility to restore time frames (incl. deleted files)
  20. # realizable by listing each backup and restore from
  21. # oldest to the newest, problem: not performant
  22. # - search file in all backups function and show available
  23. # versions with backups date (list old avail since 0.6.06)
  24. # - edit profile opens conf file in vi
  25. # - implement log-fd interpretation
  26. # - add a duplicity option check against the options pending
  27. # deprecation since 0.5.10 namely --time-separator
  28. # --short-filenames
  29. # --old-filenames
  30. # - add 'exclude_<command>' list usage eg. exclude_verify
  31. # - featreq 25: a download/install duplicity option
  32. # - hint on install software if a piece is missing
  33. # - import/export profile from/to .tgz function !!!
  34. #
  35. # CHANGELOG:
  36. # 2.1 ()
  37. # - be more verbose when duplicity version detection fails
  38. # - using info shows python binary's path for easier identification now
  39. # - reworked python interpreter handling, it's either
  40. # configured per PYTHON var
  41. # unconfigured, parsed from duplicity shebang
  42. # or set to current duplicity default 'python2' (was 'python' until now)
  43. # - do not quotewrap strings because of slashes (eg. paths) anymore
  44. # - bugfix: improved in/exclude stripping from conf DUPL_PARAMS
  45. #
  46. # 2.0.4 (20.02.2018)
  47. # - bugfix 114: "duply usage is not current" wrt. purgeFull/Incr
  48. # - bugfix 115: typo in error message - "Not GPG_KEY entries" should be "No"
  49. # - bugfix 117: no duply_ prefix when ARCH_DIR is set in conf
  50. # - bugfix debian 882159: duply: occasionally shows negative runtimes
  51. #
  52. # 2.0.3 (29.08.2017)
  53. # - bugfix: "line 2231: CMDS: bad array subscript"
  54. # - bugfix 112: "env: illegal option -- u" on MacOSX
  55. #
  56. # 2.0.2 (23.05.2017)
  57. # - bugfix: never insert creds into file:// targets
  58. # - bugfix: avail profiles hint sometimes shortend the names by one char
  59. # - bugfix 108: CMD_NEXT variable should ignore conditional commands (and, or)
  60. # - export condition before/after next/prev command as CND_PREV,CND_NEXT now
  61. # - bugfix 97: Unknown command should be ERROR, not WARNING
  62. #
  63. # 2.0.1 (16.11.2016)
  64. # - bugfix 104: Duply 2.0 sets wrong archive dir, --name was always 'duply_'
  65. #
  66. # 2.0 (27.10.2016)
  67. # made this a major version change, as we broke backward compatibility anyway
  68. # (see last change in v1.10). got complaints that rightfully pointed out
  69. # that should only come w/ a major version change. so, here we go ;)
  70. # if your backend stops working w/ this version create a new profile and
  71. # export the env vars needed as described in the comments of the conf file
  72. # directly above the SOURCE setting.
  73. # Changes:
  74. # - making sure multi spaces in TARGET survive awk processing
  75. # - new env var PROFILE exported to scripts
  76. # - fix 102: expose a unique timestamp variable for pre/post scripts
  77. # actually a featreq. exporting RUN_START nanosec unix timestamp
  78. # - fix 101: GPG_AGENT_INFO is 'bogus' (thx Thomas Harning Jr.)
  79. # - fix 96: duply cannot handle two consecutive spaces in paths
  80. #
  81. # 1.11.3 (29.5.2016)
  82. # - fix wrong "WARNING: No running gpg-agent ..." when sign key was not set
  83. #
  84. # 1.11.2 (11.2.2016)
  85. # - fix "gpg: unsafe" version print out
  86. # - bugfix 91: v1.11 [r47] broke asymmetric encryption when using GPG_KEYS_ENC
  87. # - bugfix 90: S3: TARGET_USER/PASS have no effect, added additional
  88. # documentation about needed env vars to template conf file
  89. #
  90. # 1.11.1 (18.12.2015)
  91. # - bugfix 89: "Duply has trouble with PYTHON-interpreter" on OSX homebrew
  92. # - reverted duply's default PYTHON to 'python'
  93. #
  94. # 1.11 (24.11.2015)
  95. # - remove obsolete --ssh-askpass routine
  96. # - add PYTHON conf var to allow global override of used python interpreter
  97. # - enforced usage of "python2" in PATH as default interpreter for internal
  98. # use _and_ to run duplicity (setup.py changed the shebang to the fixed
  99. # path /usr/bin/python until 0.7.05, which we circumvent this way)
  100. # - featreq 36: support gpg-connect-agent as a means to detect if an agent is
  101. # running (thx Thomas Harning Jr.), used gpg-agent for detection though
  102. # - quotewrapped run_cmd parameters to protect it from spaces eg. in TMP path
  103. # - key export routine respects gpg-agent usage now
  104. #
  105. # 1.10.1 (19.8.2015)
  106. # - bugfix 86: Duply+Swift outputs warning
  107. # - bugfix 87: Swift fails without BACKEND_URL
  108. #
  109. # 1.10 (31.7.2015)
  110. # - featreq 37: busybox issues - fix awk, grep version detection,
  111. # fix grep failure because --color=never switch is unsupported
  112. # (thx Thomas Harning Jr. for reporting and helping to debug/fix it)
  113. # - bugfix 81: --exclude-globbing-filelist is deprecated since 0.7.03
  114. # (thx Joachim Wiedorn, also for maintaining the debian package)
  115. # - implemented base-/dirname as bash functions
  116. # - featreq 31 " Support for duplicity Azure backend " - ignored a
  117. # contributed patch by Scott McKenzie and instead opted for removing almost
  118. # all code that deals with special env vars required by backends.
  119. # adding and modifying these results in too much overhead so i dropped this
  120. # feature. the future alternative for users is to consult the duplicity
  121. # manpage and add the needed export definitions to the conf file.
  122. # appended a commented example to the template conf below the auth section.
  123. #
  124. # 1.9.2 (21.6.2015)
  125. # - bugfix: exporting keys with gpg2.1 works now (thx Philip Jocks)
  126. # - documented GPG_OPTS needed for gpg2.1 to conf template (thx Troy Engel)
  127. # - bugfix 82: GREP_OPTIONS=--color=always disrupted time calculation
  128. # - added GPG conf var (see conf template for details)
  129. # - added grep version output as it is an integral needed binary
  130. # - added PYTHONPATH printout in version output
  131. #
  132. # 1.9.1 (13.10.2014)
  133. # - export CMD_ERR now for scripts to detect if CMD_PREV failed/succeeded
  134. # - bugfix: CMD_PREV contained command even if it was skipped
  135. #
  136. # 1.9.0 (24.8.2014)
  137. # - bugfix: env vars were not exported when external script was executable
  138. # - rework GPG_KEY handling, allow virtually anything now (uid, keyid etc.)
  139. # see gpg manpage, section "How to specify a user ID"
  140. # let gpg complain when the delivered values are invalid for whatever reason
  141. # - started to rework tmp space checking, exposed folder & writable check
  142. # TODO: reimplement enough file space available checking
  143. #
  144. # 1.8.0 (13.7.2014)
  145. # - add command verifyPath to expose 'verify --file-to-restore' action
  146. # - add time parameter support to verify command
  147. # - add section time formats to usage output
  148. #
  149. # 1.7.4 (24.6.2014)
  150. # - remove ubuntu one support, service is discontinued
  151. # - featreq 31: add authenticated swift (contributed by Justus Seifert)
  152. #
  153. # 1.7.3 (3.4.2014)
  154. # - bugfix: test routines, gpg2 asked for passphrase although GPG_PW was set
  155. #
  156. # 1.7.2 (1.4.2014 "April,April")
  157. # - bugfix: debian Bug#743190 "duply no longer allows restoration without
  158. # gpg passphrase in conf file"
  159. # GPG_AGENT_INFO env var is now needed to trigger --use-agent
  160. # - bugfix: gpg keyenc test routines didn't work if GPG_PW was not set
  161. #
  162. # 1.7.1 (30.3.2014)
  163. # - bugfix: purge-* commands renamed to purgeFull, purgeIncr due to
  164. # incompatibility with new minus batch separator
  165. #
  166. # 1.7.0 (20.3.2014)
  167. # - disabled gpg key id plausibility check, too many valid possibilities
  168. # - featreq 7 "Halt if precondition fails":
  169. # added and(+), or(-) batch command(separator) support
  170. # - featreq 26 "pre/post script with shebang line":
  171. # if a script is flagged executable it's executed in a subshell
  172. # now as opposed to sourced to bash, which is the default
  173. # - bugfix: do not check if dpbx, swift credentials are set anymore
  174. # - bugfix: properly escape profile name, archdir if used as arguments
  175. # - add DUPL_PRECMD conf setting for use with e.g. trickle
  176. #
  177. # 1.6.0 (1.1.2014)
  178. # - support gs backend
  179. # - support dropbox backend
  180. # - add gpg-agent support to gpg test routines
  181. # - autoenable --use-agent if passwords were not defined in config
  182. # - GPG_OPTS are now honored everywhere, keyrings or complete gpg
  183. # homedir can thus be configured to be located anywhere
  184. # - always import both secret and public key if avail from config profile
  185. # - new explanatory comments in initial exclude file
  186. # - bugfix 7: Duply only imports one key at a time
  187. #
  188. # 1.5.11 (19.07.2013)
  189. # - purge-incr command for remove-all-inc-of-but-n-full feature added
  190. # patch provided by Moritz Augsburger, thanks!
  191. # - documented version command in man page
  192. #
  193. # 1.5.10 (26.03.2013)
  194. # - minor indent and documentation fixes
  195. # - bugfix: exclude filter failed on ubuntu, mawk w/o posix char class support
  196. # - bugfix: fix url_decoding generally and for python3
  197. # - bugfix 3609075: wrong script results in status line (thx David Epping)
  198. #
  199. # 1.5.9 (22.11.2012)
  200. # - bugfix 3588926: filter --exclude* params for restore/fetch ate too much
  201. # - restore/fetch now also ignores --include* or --exclude='foobar'
  202. #
  203. # 1.5.8 (26.10.2012)
  204. # - bugfix 3575487: implement proper cloud files support
  205. #
  206. # 1.5.7 (10.06.2012)
  207. # - bugfix 3531450: Cannot use space in target URL (file:///) anymore
  208. #
  209. # 1.5.6 (24.5.2012)
  210. # - commands purge, purge-full have no default value anymore for security
  211. # reasons; instead max value can be given via cmd line or must be set
  212. # in profile; else an error is shown.
  213. # - minor man page modifications
  214. #
  215. # versioning scheme will be simplified to [major].[minor].[patch] version
  216. # with the next version raise
  217. #
  218. # 1.5.5.5 (4.2.2012)
  219. # - bugfix 3479605: SEL context confused profile folder's permission check
  220. # - colon ':' in url passphrase got ignored, added python driven url_decoding
  221. # for user & pass to better process special chars
  222. #
  223. # 1.5.5.4 (16.10.2011)
  224. # - bugfix 3421268: SFTP passwords from conf ignored and always prompted for
  225. # - add support for separate sign passphrase (needs duplicity 0.6.14+)
  226. #
  227. # 1.5.5.3 (1.10.2011)
  228. # - bugfix 3416690: preview threw echo1 error
  229. # - fix unknown cmds error usage & friends if more than 2 params were given
  230. #
  231. # 1.5.5.2 (23.9.2011)
  232. # - bugfix 3409643: ssh key auth did ask for passphrase (--ssh-askpass ?)
  233. # - bugfix: mawk does not support \W and did not split multikey definitions
  234. # - all parameters should survive single (') and double (") quotes now
  235. #
  236. # 1.5.5.1 (7.6.2011)
  237. # - featreq 3311881: add ftps as supported by duplicity 0.6.13 (thx mape2k)
  238. # - bugfix 3312208: signing detection broke symmetric gpg test routine
  239. #
  240. # 1.5.5 (2.5.2011)
  241. # - bugfix: fetch problem with space char in path, escape all params
  242. # containing non word chars
  243. # - list available profiles, if given profile cannot be found
  244. # - added --use-agent configuration hint
  245. # - bugfix 3174133: --exclude* params in conf DUPL_PARAMS broke
  246. # fetch/restore
  247. # - version command now prints out 'using installed' info
  248. # - featreq 3166169: autotrust imported keys, based on code submitted by
  249. # Martin Ellis - imported keys are now automagically trusted ultimately
  250. # - new txt2man feature to create manpages for package maintainers
  251. #
  252. # 1.5.4.2 (6.1.2011)
  253. # - new command changelog
  254. # - bugfix 3109884: freebsd awk segfaulted on printf '%*', use print again
  255. # - bugfix: freebsd awk hangs on 'awk -W version'
  256. # - bugfix 3150244: mawk does not know '--version'
  257. # - minor help text improvements
  258. # - new env vars CMD_PREV,CMD_NEXT replacing CMD env var for scripts
  259. #
  260. # 1.5.4.1 (4.12.2010)
  261. # - output awk, python, bash version now in prolog
  262. # - shebang uses /usr/bin/env now for freebsd compatibility,
  263. # bash not in /bin/bash
  264. # - new --disable-encryption parameter,
  265. # to override profile encr settings for one run
  266. # - added exclude-if-present setting to conf template
  267. # - bug 3126972: GPG_PW only needed for signing/symmetric encryption
  268. # (even though duplicity still needs it)
  269. #
  270. # 1.5.4 (15.11.2010)
  271. # - as of 1.5.3 already, new ARCH_DIR config option
  272. # - multiple key support
  273. # - ftplicity-Feature Requests-2994929: separate encryption and signing key
  274. # - key signing of symmetric encryption possible (duplicity patch committed)
  275. # - gpg tests disable switch
  276. # - gpg tests now previewable and more intelligent
  277. #
  278. # 1.5.3 (1.11.2010)
  279. # - bugfix 3056628: improve busybox compatibility, grep did not have -m param
  280. # - bugfix 2995408: allow empty password for PGP key
  281. # - bugfix 2996459: Duply erroneously escapes '-' symbol in username
  282. # - url_encode function is now pythonized
  283. # - rsync uses FTP_PASSWORD now if duplicity 0.6.10+ , else issue warning
  284. # - feature 3059262: Make pre and post aware of parameters,
  285. # internal parameters + CMD of pre or post
  286. #
  287. # 1.5.2.3 (16.4.2010)
  288. # - bugfix: date again, should now work virtually anywhere
  289. #
  290. # 1.5.2.2 (3.4.2010)
  291. # - minor bugfix: duplicity 0.6.8b version string now parsable
  292. # - added INSTALL.txt
  293. #
  294. # 1.5.2.1 (23.3.2010)
  295. # - bugfix: date formatting is awked now and should work on all platforms
  296. #
  297. # 1.5.2 (2.3.2010)
  298. # - bugfix: errors print to STD_ERR now, failed tasks print an error message
  299. # - added --name=duply_<profile> for duplicity 0.6.01+ to name cache folder
  300. # - simplified & cleaned profileless commands, removed second instance
  301. # - generalized separator time routines
  302. # - added support for --no-encryption (GPG_KEY='disabled'), see conf examples
  303. # - minor fixes
  304. #
  305. # 1.5.1.5 (5.2.2010)
  306. # - bugfix: added special handling of credentials for rsync, imap(s)
  307. #
  308. # 1.5.1.4 (7.1.2010)
  309. # - bugfix: nsecs defaults now to zeroes if date does not deliver [0-9]{9}
  310. # - check if ncftp binary is available if url protocol is ftp
  311. # - bugfix: duplicity output is now printed to screen directly to resolve
  312. # 'mem alloc problem' bug report
  313. # - bugfix: passwords will not be in the url anymore to solve the 'duply shows
  314. # sensitive data in process listing' bug report
  315. #
  316. # 1.5.1.3 (24.12.2009) 'merry xmas'
  317. # - bugfix: gpg pass now apostrophed to allow space and friends
  318. # - bugfix: credentials are now url encoded to allow special chars in them
  319. # a note about url encoding has been added to the conf template
  320. #
  321. # 1.5.1.2 (1.11.2009)
  322. # - bugfix: open parenthesis in password broke duplicity execution
  323. # - bugfix: ssh/scp backend does not always need credentials e.g. key auth
  324. #
  325. # 1.5.1.1 (21.09.2009)
  326. # - bugfix: fixed s3[+http] TARGET_PASS not needed routine
  327. # - bugfix: TYPO in duply 1.5.1 prohibited the use of /etc/duply
  328. # see https://sourceforge.net/tracker/index.php?func=detail&
  329. # aid=2864410&group_id=217745&atid=1041147
  330. #
  331. # 1.5.1 (21.09.2009) - duply (fka. ftplicity)
  332. # - first things first: ftplicity (being able to support all backends since
  333. # some time) will be called duply (fka. ftplicity) from now on. The addendum
  334. # is for the time being to circumvent confusion.
  335. # - bugfix: exit code is 1 (error) not 0 (success), if at least on duplicity
  336. # command failed
  337. # - s3[+http] now supported natively by translating user/pass to access_key/
  338. # secret_key environment variables needed by duplicity s3 boto backend
  339. # - bugfix: additional output lines do not confuse version check anymore
  340. # - list command supports now age parameter (patch by stefan on feature
  341. # request tracker)
  342. # - bugfix: option/param pairs are now correctly passed on to duplicity
  343. # - bugfix: s3[+http] needs no TARGET_PASS if command is read only
  344. #
  345. # 1.5.0.2 (31.07.1009)
  346. # - bugfix: insert password in target url didn't work with debian mawk
  347. # related to previous bug report
  348. #
  349. # 1.5.0.1 (23.07.2009)
  350. # - bugfix: gawk gensub dependency raised an error on debian's default mawk
  351. # replaced with match/substr command combination (bug report)
  352. # https://sf.net/tracker/?func=detail&atid=1041147&aid=2825388&
  353. # group_id=217745
  354. #
  355. # 1.5.0 (01.07.2009)
  356. # - removed ftp limitation, all duplicity backends should work now
  357. # - bugfix: date for separator failed on openwrt busybox date, added a
  358. # detecting workaround, milliseconds are not available w/ busybox date
  359. #
  360. # 1.4.2.1 (14.05.2009)
  361. # - bugfix: free temp space detection failed with lvm, fixed awk parse routine
  362. #
  363. # 1.4.2 (22.04.2009)
  364. # - gpg keys are now exported as gpgkey.[id].asc , the suffix reflects the
  365. # armored ascii nature, the id helps if the key is switched for some reason
  366. # im/export routines are updated accordingly (import is backward compatible
  367. # to the old profile/gpgkey files)
  368. # - profile argument is treated as path if it contains slashes
  369. # (for details see usage)
  370. # - non-ftplicity options (all but --preview currently) are now passed
  371. # on to duplicity
  372. # - removed need for stat in secure_conf, it is ls based now
  373. # - added profile folder readable check
  374. # - added gpg version & home info output
  375. # - awk utility availability is now checked, because it was mandatory already
  376. # - tmp space is now checked on writability and space requirement
  377. # test fails on less than 25MB or configured $VOLSIZE,
  378. # test warns if there is less than two times $VOLSIZE because
  379. # that's required for --asynchronous-upload option
  380. # - gpg functionality is tested now before executing duplicity
  381. # test drive contains encryption, decryption, comparison, cleanup
  382. # this is meant to detect non trusted or other gpg errors early
  383. # - added possibility of doing symmetric encryption with duplicity
  384. # set GPG_KEY="" or simply comment it out
  385. # - added hints in config template on the depreciation of
  386. # --short-filenames, --time-separator duplicity options
  387. #
  388. # new versioning scheme 1.4.2b => 1.4.2,
  389. # beta b's are replaced by a patch count number e.g. 1.4.2.1 will be assigned
  390. # to the first bug fixing version and 1.4.2.2 to the second and so on
  391. # also the releases will now have a release date formatted (Day.Month.Year)
  392. #
  393. # 1.4.1b1 - bugfix: ftplicity changed filesystem permission of a folder
  394. # named exactly as the profile if existing in executing dir
  395. # - improved plausibility checking of config and profile folder
  396. # - secure_conf only acts if needed and prints a warning now
  397. #
  398. # 1.4.1b - introduce status (duplicity collection-status) command
  399. # - pre/post script output printed always now, not only on errors
  400. # - new config parameter GPG_OPTS to pass gpg options
  401. # added examples & comments to profile template conf
  402. # - reworked separator times, added duration display
  403. # - added --preview switch, to preview generated command lines
  404. # - disabled MAX_AGE, MAX_FULL_BACKUPS, VERBOSITY in generated
  405. # profiles because they have reasonable defaults now if not set
  406. #
  407. # 1.4.0b1 - bugfix: incr forces incremental backups on duplicity,
  408. # therefore backup translates to pre_bkp_post now
  409. # - bugfix: new command bkp, which represents duplicity's
  410. # default action (incr or full if full_if_older matches
  411. # or no earlier backup chain is found)
  412. #
  413. # new versioning scheme 1.4 => 1.4.0, added new minor revision number
  414. # this is meant to slow down the rapid version growing but still keep
  415. # versions cleanly separated.
  416. # only additional features will raise the new minor revision number.
  417. # all releases start as beta, each bugfix release will raise the beta
  418. # count, usually new features arrive before a version 'ripes' to stable
  419. #
  420. # 1.4.0b
  421. # 1.4b - added startup info on version, time, selected profile
  422. # - added time output to separation lines
  423. # - introduced: command purge-full implements duplicity's
  424. # remove-all-but-n-full functionality (patch by unknown),
  425. # uses config variable $MAX_FULL_BACKUPS (default = 1)
  426. # - purge config var $MAX_AGE defaults to 1M (month) now
  427. # - command full does not execute pre/post anymore
  428. # use batch command pre_full_post if needed
  429. # - introduced batch mode cmd1_cmd2_etc
  430. # (in turn removed the bvp command)
  431. # - unknown/undefined command issues a warning/error now
  432. # - bugfix: version check works with 0.4.2 and older now
  433. # 1.3b3 - introduced pre/post commands to execute/debug scripts
  434. # - introduced bvp (backup, verify, purge)
  435. # - bugfix: removed need for awk gensub, now mawk compatible
  436. # 1.3b2 - removed pre/post need executable bit set
  437. # - profiles now under ~/.ftplicity as folders
  438. # - root can keep profiles in /etc/ftplicity, folder must be
  439. # created by hand, existing profiles must be moved there
  440. # - removed ftplicity in path requirement
  441. # - bugfix: bash < v.3 did not know '=~'
  442. # - bugfix: purge works again
  443. # 1.3 - introduces multiple profiles support
  444. # - modified some script errors/docs
  445. # - reordered gpg key check import routine
  446. # - added 'gpg key id not set' check
  447. # - added error_gpg (adds how to setup gpg key howto)
  448. # - bugfix: duplicity 0.4.4RC4+ parameter syntax changed
  449. # - duplicity_version_check routine introduced
  450. # - added time separator, shortnames, volsize, full_if_older
  451. # duplicity options to config file (inspired by stevie
  452. # from http://weareroot.de)
  453. # 1.1.1 - bugfix: encryption reactivated
  454. # 1.1 - introduced config directory
  455. # 1.0 - first release
  456. ################################################################################
  457.  
  458. # utility functions overriding binaries
  459.  
  460. # wrap grep to override possible env set GREP_OPTIONS=--color=always
  461. function grep {
  462. command env "GREP_OPTIONS=" grep "$@"
  463. }
  464.  
  465. # implement basename in plain bash
  466. function basename {
  467. local stripped="${1%/}"
  468. echo "${stripped##*/}"
  469. }
  470.  
  471. # implement dirname in plain bash
  472. function dirname {
  473. echo ${1%/*}
  474. }
  475.  
  476. # implement basic which in plain bash
  477. function which {
  478. type -p "$@"
  479. }
  480.  
  481. # check availability of executables via file name or file paths
  482. function lookup {
  483. local bin="$1"
  484. # look for file names in path via bash hash OR
  485. # look for executables at given relative/absolute location
  486. ( [ "${bin##*/}" == "$bin" ] && hash "$bin" 2>/dev/null ) || [ -x "$bin" ]
  487. }
  488.  
  489. # the python binary to use, exit code 0 when configured, else 1
  490. function python_binary {
  491. # if unset, parse from duplicity shebang
  492. if ! var_isset 'PYTHON'; then
  493. duplicity_python_binary_parse;
  494. echo $DUPL_PYTHON_BIN;
  495. return 1;
  496. else
  497. # tell if PYTHON was configured manually
  498. echo $PYTHON;
  499. return 0
  500. fi
  501. }
  502.  
  503. # important definitions #######################################################
  504.  
  505. ME_LONG="$0"
  506. ME="$(basename $0)"
  507. ME_NAME="${ME%%.*}"
  508. ME_VERSION="2.1dev"
  509. ME_WEBSITE="http://duply.net"
  510.  
  511. # default config values
  512. DEFAULT_SOURCE='/path/of/source'
  513. DEFAULT_TARGET='scheme://user[:password]@host[:port]/[/]path'
  514. DEFAULT_TARGET_USER='_backend_username_'
  515. DEFAULT_TARGET_PASS='_backend_password_'
  516. DEFAULT_GPG='gpg'
  517. DEFAULT_GPG_KEY='_KEY_ID_'
  518. DEFAULT_GPG_PW='_GPG_PASSWORD_'
  519. DEFAULT_PYTHON='python2'
  520.  
  521. # function definitions ##########################
  522.  
  523. function set_config { # sets global config vars
  524. local CONFHOME_COMPAT="$HOME/.ftplicity"
  525. local CONFHOME="$HOME/.duply"
  526. local CONFHOME_ETC_COMPAT="/etc/ftplicity"
  527. local CONFHOME_ETC="/etc/duply"
  528.  
  529. # confdir can be delivered as path (must contain /)
  530. if [ `echo $FTPLCFG | grep /` ] ; then
  531. CONFDIR=$(readlink -f $FTPLCFG 2>/dev/null || \
  532. ( echo $FTPLCFG|grep -v '^/' 1>/dev/null 2>&1 \
  533. && echo $(pwd)/${FTPLCFG} ) || \
  534. echo ${FTPLCFG})
  535. # or DEFAULT in home/.duply folder (NEW)
  536. elif [ -d "${CONFHOME}" ]; then
  537. CONFDIR="${CONFHOME}/${FTPLCFG}"
  538. # or in home/.ftplicity folder (OLD)
  539. elif [ -d "${CONFHOME_COMPAT}" ]; then
  540. CONFDIR="${CONFHOME_COMPAT}/${FTPLCFG}"
  541. warning_oldhome "${CONFHOME_COMPAT}" "${CONFHOME}"
  542. # root can put profiles under /etc/duply (NEW) if path exists
  543. elif [ -d "${CONFHOME_ETC}" ] && [ "$EUID" -eq 0 ]; then
  544. CONFDIR="${CONFHOME_ETC}/${FTPLCFG}"
  545. # root can keep profiles under /etc/ftplicity (OLD) if path exists
  546. elif [ -d "${CONFHOME_ETC_COMPAT}" ] && [ "$EUID" -eq 0 ]; then
  547. CONFDIR="${CONFHOME_ETC_COMPAT}/${FTPLCFG}"
  548. warning_oldhome "${CONFHOME_ETC_COMPAT}" "${CONFHOME_ETC}"
  549. # hmm no profile folder there, then use default for error later
  550. else
  551. CONFDIR="${CONFHOME}/${FTPLCFG}" # continue, will fail later in main
  552. fi
  553.  
  554. # remove trailing slash, get profile name etc.
  555. CONFDIR="${CONFDIR%/}"
  556. PROFILE="${CONFDIR##*/}"
  557. CONF="$CONFDIR/conf"
  558. PRE="$CONFDIR/pre"
  559. POST="$CONFDIR/post"
  560. EXCLUDE="$CONFDIR/exclude"
  561. KEYFILE="$CONFDIR/gpgkey.asc"
  562. }
  563.  
  564. function version_info { # print version information
  565. cat <<END
  566.   $ME_NAME version $ME_VERSION
  567.   ($ME_WEBSITE)
  568. END
  569. }
  570.  
  571. function version_info_using {
  572. cat <<END
  573. $(version_info)
  574.  
  575.   $(using_info)
  576. END
  577. }
  578.  
  579. function using_info {
  580. # init needed vars into global name space
  581. lookup duplicity && { duplicity_python_binary_parse; duplicity_version_get; }
  582. local NOTFOUND="INVALID"
  583. local AWK_VERSION GREP_VERSION PYTHON_RUNNER
  584. # freebsd awk (--version only), debian mawk (-W version only), deliver '' so awk does not wait for input
  585. AWK_VERSION=$( lookup awk && (awk --version 2>/dev/null || awk -W version 2>&1) | awk 'NR<=2&&tolower($0)~/(busybox|awk)/{success=1;print;exit} END{if(success<1) print "unknown"}' || echo "$NOTFOUND" )
  586. GREP_VERSION=$( lookup grep && grep --version 2>&1 | awk 'NR<=2&&tolower($0)~/(busybox|grep.*[0-9]+\.[0-9]+)/{success=1;print;exit} END{if(success<1) print "unknown"}' || echo "$NOTFOUND" )
  587. PYTHON_RUNNER=$(python_binary)
  588. local PYTHON_VERSION=$(lookup "$PYTHON_RUNNER" && "$PYTHON_RUNNER" -V 2>&1| awk '{print tolower($0);exit}' || echo "'$PYTHON_RUNNER' $NOTFOUND" )
  589. local GPG_INFO=$(gpg_avail && gpg --version 2>&1| awk '/^gpg.*[0-9\.]+$/&&length(v)<1{v=$1" "$3}/^Home:/{h=" ("$0")"}END{print v""h}' || echo "gpg $NOTFOUND")
  590. local BASH_VERSION=$(bash --version | awk 'NR==1{IGNORECASE=1;sub(/GNU bash, version[ ]+/,"",$0);print $0}')
  591. # print out
  592. echo -e "Using installed duplicity version ${DUPL_VERSION:-$NOTFOUND}\
  593. ${PYTHON_VERSION+, $PYTHON_VERSION ${PYTHON_RUNNER:+($(which "$PYTHON_RUNNER"))}${PYTHONPATH:+ 'PYTHONPATH=$PYTHONPATH'}}\
  594. ${GPG_INFO:+, $GPG_INFO}${AWK_VERSION:+, awk '${AWK_VERSION}'}${GREP_VERSION:+, grep '${GREP_VERSION}'}\
  595. ${BASH_VERSION:+, bash '${BASH_VERSION}'}."
  596. }
  597.  
  598. function usage_info { # print usage information
  599.  
  600. cat <<USAGE_EOF
  601. VERSION:
  602. $(version_info)
  603.  
  604. DESCRIPTION:
  605. Duply deals as a wrapper for the mighty duplicity magic.
  606. It simplifies running duplicity with cron or on command line by:
  607.  
  608. - keeping recurring settings in profiles per backup job
  609. - enabling batch operations eg. backup_verify+purge
  610. - executing pre/post scripts (different actions possible
  611. depending on previous or next command or it's exit status)
  612. - precondition checking for flawless duplicity operation
  613.  
  614. For each backup job one configuration profile must be created.
  615. The profile folder will be stored under '~/.${ME_NAME}/<profile>'
  616. (where ~ is the current users home directory).
  617. Hint:
  618. If the folder '/etc/${ME_NAME}' exists, the profiles for the super
  619. user root will be searched & created there.
  620.  
  621. USAGE:
  622. first time usage (profile creation):
  623. $ME <profile> create
  624.  
  625. general usage in single or batch mode (see EXAMPLES):
  626. $ME <profile> <command>[[_|+|-]<command>[_|+|-]...] [<options> ...]
  627.  
  628. For batches the conditional separators can also be written as pseudo commands
  629. and(+), or(-). See SEPARATORS for details.
  630.  
  631. Non $ME options are passed on to duplicity (see OPTIONS).
  632. All conf parameters can also be defined in the environment instead.
  633.  
  634. PROFILE:
  635. Indicated by a path or a profile name (<profile>), which is resolved
  636. to '~/.${ME_NAME}/<profile>' (~ expands to environment variable \$HOME).
  637.  
  638. Superuser root can place profiles under '/etc/${ME_NAME}'. Simply create
  639. the folder manually before running $ME_NAME as superuser.
  640. Note:
  641. Already existing profiles in root's home folder will cease to work
  642. unless they are moved to the new location manually.
  643.  
  644. example 1: $ME humbug backup
  645.  
  646. Alternatively a _path_ might be used e.g. useful for quick testing,
  647. restoring or exotic locations. Shell expansion should work as usual.
  648. Hint:
  649. The path must contain at least one path separator '/',
  650. e.g. './test' instead of only 'test'.
  651.  
  652. example 2: $ME ~/.${ME_NAME}/humbug backup
  653.  
  654. SEPARATORS:
  655. _ (underscore)
  656. neutral separator
  657. + (plus sign), _and_
  658. conditional AND
  659. the next command will only be executed if the previous succeeded
  660. - (minus sign), _or_
  661. conditional OR
  662. the next command will only be executed if the previous failed
  663.  
  664. example:
  665. 'pre+bkp-verify_post' translates to 'pre_and_bkp_or_verify_post'
  666.  
  667. COMMANDS:
  668. usage get usage help text
  669.  
  670. and/or pseudo commands for better batch cmd readability (see SEPARATORS)
  671. create creates a configuration profile
  672. backup backup with pre/post script execution (batch: pre_bkp_post),
  673. full (if full_if_older matches or no earlier backup is found)
  674. incremental (in all other cases)
  675. pre/post execute '<profile>/$(basename "$PRE")', '<profile>/$(basename "$POST")' scripts
  676. bkp as above but without executing pre/post scripts
  677. full force full backup
  678. incr force incremental backup
  679. list [<age>]
  680. list all files in backup (as it was at <age>, default: now)
  681. status prints backup sets and chains currently in repository
  682. verify [<age>] [--compare-data]
  683. list files changed, since age if given
  684. verifyPath <rel_path_in_bkp> <local_path> [<age>] [--compare-data]
  685. list changes of a file or folder path in backup compared to a
  686. local path, since age if given
  687. restore <target_path> [<age>]
  688. restore the complete backup to <target_path> [as it was at <age>]
  689. fetch <src_path> <target_path> [<age>]
  690. fetch single file/folder from backup [as it was at <age>]
  691. purge [<max_age>] [--force]
  692. list outdated backup files (older than \$MAX_AGE)
  693. [use --force to actually delete these files]
  694. purgeFull [<max_full_backups>] [--force]
  695. list outdated backup files (\$MAX_FULL_BACKUPS being the number of
  696. full backups and associated incrementals to keep, counting in
  697. reverse chronological order)
  698. [use --force to actually delete these files]
  699. purgeIncr [<max_fulls_with_incrs>] [--force]
  700. list outdated incremental backups (\$MAX_FULLS_WITH_INCRS being
  701. the number of full backups which associated incrementals will be
  702. kept, counting in reverse chronological order)
  703. [use --force to actually delete these files]
  704. cleanup [--force]
  705. list broken backup chain files archives (e.g. after unfinished run)
  706. [use --force to actually delete these files]
  707.  
  708. changelog print changelog / todo list
  709. txt2man feature for package maintainers - create a manpage based on the
  710. usage output. download txt2man from http://mvertes.free.fr/, put
  711. it in the PATH and run '$ME txt2man' to create a man page.
  712. version show version information of $ME_NAME and needed programs
  713.  
  714. OPTIONS:
  715. --force passed to duplicity (see commands:
  716. purge, purgeFull, purgeIncr, cleanup)
  717. --preview do nothing but print out generated duplicity command lines
  718. --disable-encryption
  719. disable encryption, overrides profile settings
  720.  
  721. TIME FORMATS:
  722. For all time related parameters like age, max_age etc.
  723. Refer to the duplicity manpage for all available formats. Here some examples:
  724. 2002-01-25T07:00:00+02:00 (full date time format string)
  725. 2002/3/5 (date string YYYY/MM/DD)
  726. 12D (interval, 12 days ago)
  727. 1h78m (interval, 1 hour 78 minutes ago)
  728.  
  729. PRE/POST SCRIPTS:
  730. Some useful internal duply variables are exported to the scripts.
  731.  
  732. PROFILE, CONFDIR, SOURCE, TARGET_URL_<PROT|HOSTPATH|USER|PASS>,
  733. GPG_<KEYS_ENC|KEY_SIGN|PW>, CMD_<PREV|NEXT>, CMD_ERR, RUN_START,
  734. CND_<PREV|NEXT> (condition before/after next/prev command)
  735.  
  736. The CMD_* variables were introduced to allow different actions according to
  737. the command the scripts were attached to e.g. 'pre_bkp_post_pre_verify_post'
  738. will call the pre script two times, with CMD_NEXT variable set to 'bkp'
  739. on the first and to 'verify' on the second run.
  740. CMD_ERR holds the exit code of the CMD_PREV .
  741.  
  742. EXAMPLES:
  743. create profile 'humbug':
  744. $ME humbug create (don't forget to edit this new conf file)
  745. backup 'humbug' now:
  746. $ME humbug backup
  747. list available backup sets of profile 'humbug':
  748. $ME humbug status
  749. list and delete outdated backups of 'humbug':
  750. $ME humbug purge --force
  751. restore latest backup of 'humbug' to /mnt/restore:
  752. $ME humbug restore /mnt/restore
  753. restore /etc/passwd of 'humbug' from 4 days ago to /root/pw:
  754. $ME humbug fetch etc/passwd /root/pw 4D
  755. (see "duplicity manpage", section TIME FORMATS)
  756. a one line batch job on 'humbug' for cron execution:
  757. $ME humbug backup_verify_purge --force
  758. batch job to run a full backup with pre/post scripts:
  759. $ME humbug pre_full_post
  760.  
  761. FILES:
  762. in profile folder '~/.${ME_NAME}/<profile>' or '/etc/${ME_NAME}'
  763. conf profile configuration file
  764. pre,post pre/post scripts (see above for details)
  765. gpgkey.*.asc exported GPG key files
  766. exclude a globbing list of included or excluded files/folders
  767. (see "duplicity manpage", section FILE SELECTION)
  768.  
  769. $(hint_profile)
  770.  
  771. SEE ALSO:
  772. duplicity man page:
  773. duplicity(1) or http://duplicity.nongnu.org/duplicity.1.html
  774. USAGE_EOF
  775. }
  776.  
  777. # to check call 'duply txt2man | man -l -'
  778. function usage_txt2man {
  779. usage_info | \
  780. awk '/^^[^[:lower:][:space:]][^[:lower:]]+$/{gsub(/[^[:upper:]]/," ",$0)}{print}' |\
  781. txt2man -t"$(toupper "${ME_NAME}")" -s1 -r"${ME_NAME}-${ME_VERSION}" -v'User Manuals'
  782. }
  783.  
  784. function changelog {
  785. cat $ME_LONG | awk '/^#####/{on=on+1}(on==3){sub(/^#( )?/,"",$0);print}'
  786. }
  787.  
  788. function create_config {
  789. if [ ! -d "$CONFDIR" ] ; then
  790. mkdir -p "$CONFDIR" || error "Couldn't create config '$CONFDIR'."
  791. # create initial config file
  792. cat <<EOF >"$CONF"
  793. # gpg encryption settings, simple settings:
  794. # GPG_KEY='disabled' - disables encryption alltogether
  795. # GPG_KEY='<key1>[,<key2>]'; GPG_PW='pass' - encrypt with keys,
  796. # sign if secret key of key1 is available use GPG_PW for sign & decrypt
  797. # Note: you can specify keys via all methods described in gpg manpage,
  798. # section "How to specify a user ID", escape commas (,) via backslash (\)
  799. # e.g. 'Mueller, Horst', 'Bernd' -> 'Mueller\, Horst, Bernd'
  800. # as they are used to separate the entries
  801. # GPG_PW='passphrase' - symmetric encryption using passphrase only
  802. GPG_KEY='${DEFAULT_GPG_KEY}'
  803. GPG_PW='${DEFAULT_GPG_PW}'
  804. # gpg encryption settings in detail (extended settings)
  805. # the above settings translate to the following more specific settings
  806. # GPG_KEYS_ENC='<keyid1>[,<keyid2>,...]' - list of pubkeys to encrypt to
  807. # GPG_KEY_SIGN='<keyid1>|disabled' - a secret key for signing
  808. # GPG_PW='<passphrase>' - needed for signing, decryption and symmetric
  809. # encryption. If you want to deliver different passphrases for e.g.
  810. # several keys or symmetric encryption plus key signing you can use
  811. # gpg-agent. Simply make sure that GPG_AGENT_INFO is set in environment.
  812. # also see "A NOTE ON SYMMETRIC ENCRYPTION AND SIGNING" in duplicity manpage
  813. # notes on en/decryption
  814. # private key and passphrase will only be needed for decryption or signing.
  815. # decryption happens on restore and incrementals (compare archdir contents).
  816. # for security reasons it makes sense to separate the signing key from the
  817. # encryption keys. https://answers.launchpad.net/duplicity/+question/107216
  818. #GPG_KEYS_ENC='<pubkey1>,<pubkey2>,...'
  819. #GPG_KEY_SIGN='<prvkey>'
  820. # set if signing key passphrase differs from encryption (key) passphrase
  821. # NOTE: available since duplicity 0.6.14, translates to SIGN_PASSPHRASE
  822. #GPG_PW_SIGN='<signpass>'
  823.  
  824. # uncomment and set a file path or name force duply to use this gpg executable
  825. # available in duplicity 0.7.04 and above (currently unreleased 06/2015)
  826. #GPG='/usr/local/gpg-2.1/bin/gpg'
  827.  
  828. # gpg options passed from duplicity to gpg process (default='')
  829. # e.g. "--trust-model pgp|classic|direct|always"
  830. # or "--compress-algo=bzip2 --bzip2-compress-level=9"
  831. # or "--personal-cipher-preferences AES256,AES192,AES..."
  832. # or "--homedir ~/.duply" - keep keyring and gpg settings duply specific
  833. # or "--pinentry-mode loopback" - needed for GPG 2.1+ _and_
  834. # also enable allow-loopback-pinentry in your .gnupg/gpg-agent.conf
  835. #GPG_OPTS=''
  836.  
  837. # disable preliminary tests with the following setting
  838. #GPG_TEST='disabled'
  839.  
  840. # backend, credentials & location of the backup target (URL-Format)
  841. # generic syntax is
  842. # scheme://[user[:password]@]host[:port]/[/]path
  843. # eg.
  844. # sftp://bob:secret@backupserver.com//home/bob/dupbkp
  845. # for details and available backends see duplicity manpage, section URL Format
  846. # http://duplicity.nongnu.org/duplicity.1.html#sect7
  847. # BE AWARE:
  848. # some backends (cloudfiles, S3 etc.) need additional env vars to be set to
  849. # work properly, read after the TARGET definition for more details.
  850. # ATTENTION:
  851. # characters other than A-Za-z0-9.-_.~ in the URL have to be
  852. # replaced by their url encoded pendants, see
  853. # http://en.wikipedia.org/wiki/Url_encoding
  854. # if you define the credentials as TARGET_USER, TARGET_PASS below $ME
  855. # will try to url_encode them for you if the need arises.
  856. TARGET='${DEFAULT_TARGET}'
  857. # optionally the username/password can be defined as extra variables
  858. # setting them here _and_ in TARGET results in an error
  859. # ATTENTION:
  860. # there are backends that do not support the user/pass auth scheme.
  861. # prominent examples are S3, Azure, Cloudfiles. when in doubt consult the
  862. # duplicity manpage. usually there is a NOTE section explaining if and which
  863. # env vars should be set.
  864. #TARGET_USER='${DEFAULT_TARGET_USER}'
  865. #TARGET_PASS='${DEFAULT_TARGET_PASS}'
  866. # eg. for cloud files backend it might look like this (uncomment for use!)
  867. #export CLOUDFILES_USERNAME='someuser'
  868. #export CLOUDFILES_APIKEY='somekey'
  869. #export CLOUDFILES_AUTHURL ='someurl'
  870. # the following is an incomplete list (<backend>: comma separated env vars list)
  871. # Azure: AZURE_ACCOUNT_NAME, AZURE_ACCOUNT_KEY
  872. # Cloudfiles: CLOUDFILES_USERNAME, CLOUDFILES_APIKEY, CLOUDFILES_AUTHURL
  873. # Google Cloud Storage: GS_ACCESS_KEY_ID, GS_SECRET_ACCESS_KEY
  874. # Pydrive: GOOGLE_DRIVE_ACCOUNT_KEY, GOOGLE_DRIVE_SETTINGS
  875. # S3: AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY
  876. # Swift: SWIFT_USERNAME, SWIFT_PASSWORD, SWIFT_AUTHURL,
  877. # SWIFT_TENANTNAME OR SWIFT_PREAUTHURL, SWIFT_PREAUTHTOKEN
  878.  
  879. # base directory to backup
  880. SOURCE='${DEFAULT_SOURCE}'
  881.  
  882. # a command that runs duplicity e.g.
  883. # shape bandwidth use via trickle
  884. # "trickle -s -u 640 -d 5120" # 5Mb up, 40Mb down"
  885. #DUPL_PRECMD=""
  886.  
  887. # override the used python interpreter, defaults to
  888. # - parsed result of duplicity's shebang or 'python2'
  889. # e.g. "python2" or "/usr/bin/python2.7"
  890. #PYTHON="python"
  891.  
  892. # exclude folders containing exclusion file (since duplicity 0.5.14)
  893. # Uncomment the following two lines to enable this setting.
  894. #FILENAME='.duplicity-ignore'
  895. #DUPL_PARAMS="\$DUPL_PARAMS --exclude-if-present '\$FILENAME'"
  896.  
  897. # Time frame for old backups to keep, Used for the "purge" command.
  898. # see duplicity man page, chapter TIME_FORMATS)
  899. #MAX_AGE=1M
  900.  
  901. # Number of full backups to keep. Used for the "purgeFull" command.
  902. # See duplicity man page, action "remove-all-but-n-full".
  903. #MAX_FULL_BACKUPS=1
  904.  
  905. # Number of full backups for which incrementals will be kept for.
  906. # Used for the "purgeIncr" command.
  907. # See duplicity man page, action "remove-all-inc-of-but-n-full".
  908. #MAX_FULLS_WITH_INCRS=1
  909.  
  910. # activates duplicity --full-if-older-than option (since duplicity v0.4.4.RC3)
  911. # forces a full backup if last full backup reaches a specified age, for the
  912. # format of MAX_FULLBKP_AGE see duplicity man page, chapter TIME_FORMATS
  913. # Uncomment the following two lines to enable this setting.
  914. #MAX_FULLBKP_AGE=1M
  915. #DUPL_PARAMS="\$DUPL_PARAMS --full-if-older-than \$MAX_FULLBKP_AGE "
  916.  
  917. # sets duplicity --volsize option (available since v0.4.3.RC7)
  918. # set the size of backup chunks to VOLSIZE MB instead of the default 25MB.
  919. # VOLSIZE must be number of MB's to set the volume size to.
  920. # Uncomment the following two lines to enable this setting.
  921. #VOLSIZE=50
  922. #DUPL_PARAMS="\$DUPL_PARAMS --volsize \$VOLSIZE "
  923.  
  924. # verbosity of output (error 0, warning 1-2, notice 3-4, info 5-8, debug 9)
  925. # default is 4, if not set
  926. #VERBOSITY=5
  927.  
  928. # temporary file space. at least the size of the biggest file in backup
  929. # for a successful restoration process. (default is '/tmp', if not set)
  930. #TEMP_DIR=/tmp
  931.  
  932. # Modifies archive-dir option (since 0.6.0) Defines a folder that holds
  933. # unencrypted meta data of the backup, enabling new incrementals without the
  934. # need to decrypt backend metadata first. If empty or deleted somehow, the
  935. # private key and it's password are needed.
  936. # NOTE: This is confidential data. Put it somewhere safe. It can grow quite
  937. # big over time so you might want to put it not in the home dir.
  938. # default '~/.cache/duplicity/duply_<profile>/'
  939. # if set '\${ARCH_DIR}/<profile>'
  940. #ARCH_DIR=/some/space/safe/.duply-cache
  941.  
  942. # DEPRECATED setting
  943. # sets duplicity --time-separator option (since v0.4.4.RC2) to allow users
  944. # to change the time separator from ':' to another character that will work
  945. # on their system. HINT: For Windows SMB shares, use --time-separator='_'.
  946. # NOTE: '-' is not valid as it conflicts with date separator.
  947. # ATTENTION: only use this with duplicity < 0.5.10, since then default file
  948. # naming is compatible and this option is pending depreciation
  949. #DUPL_PARAMS="\$DUPL_PARAMS --time-separator _ "
  950.  
  951. # DEPRECATED setting
  952. # activates duplicity --short-filenames option, when uploading to a file
  953. # system that can't have filenames longer than 30 characters (e.g. Mac OS 8)
  954. # or have problems with ':' as part of the filename (e.g. Microsoft Windows)
  955. # ATTENTION: only use this with duplicity < 0.5.10, later versions default file
  956. # naming is compatible and this option is pending depreciation
  957. #DUPL_PARAMS="\$DUPL_PARAMS --short-filenames "
  958.  
  959. # more duplicity command line options can be added in the following way
  960. # don't forget to leave a separating space char at the end
  961. #DUPL_PARAMS="\$DUPL_PARAMS --put_your_options_here "
  962.  
  963. EOF
  964.  
  965. # create initial exclude file
  966. cat <<EOF >"$EXCLUDE"
  967. # although called exclude, this file is actually a globbing file list
  968. # duplicity accepts some globbing patterns, even including ones here
  969. # here is an example, this incl. only 'dir/bar' except it's subfolder 'foo'
  970. # - dir/bar/foo
  971. # + dir/bar
  972. # - **
  973. # for more details see duplicity manpage, section File Selection
  974. # http://duplicity.nongnu.org/duplicity.1.html#sect9
  975.  
  976. EOF
  977.  
  978. # Hints on first usage
  979. cat <<EOF
  980.  
  981. Congratulations. You just created the profile '$FTPLCFG'.
  982. The initial config file has been created as
  983. '$CONF'.
  984. You should now adjust this config file to your needs.
  985.  
  986. $(hint_profile)
  987.  
  988. EOF
  989. fi
  990.  
  991. }
  992.  
  993. # used in usage AND create_config
  994. function hint_profile {
  995. cat <<EOF
  996. IMPORTANT:
  997.   Copy the _whole_ profile folder after the first backup to a safe place.
  998.   It contains everything needed to restore your backups. You will need
  999.   it if you have to restore the backup from another system (e.g. after a
  1000.   system crash). Keep access to these files restricted as they contain
  1001.   _all_ informations (gpg data, ftp data) to access and modify your backups.
  1002.  
  1003.   Repeat this step after _all_ configuration changes. Some configuration
  1004.   options are crucial for restoration.
  1005.  
  1006. EOF
  1007. }
  1008.  
  1009. function separator {
  1010. echo "--- $@ ---"
  1011. }
  1012.  
  1013. function inform {
  1014. echo -e "\nINFO:\n\n$@\n"
  1015. }
  1016.  
  1017. function warning {
  1018. echo -e "\nWARNING:\n\n$@\n"
  1019. }
  1020.  
  1021. function warning_oldhome {
  1022. local old=$1 new=$2
  1023. warning " ftplicity changed name to duply since you created your profiles.
  1024. Please rename the old folder
  1025. '$old'
  1026. to
  1027. '$new'
  1028. and this warning will disappear.
  1029. If you decide not to do so profiles will _only_ work from the old location."
  1030. }
  1031.  
  1032. function error_print {
  1033. echo -e "$@" >&2
  1034. }
  1035.  
  1036. function error {
  1037. error_print "\nSorry. A fatal ERROR occured:\n\n$@\n"
  1038. exit -1
  1039. }
  1040.  
  1041. function error_gpg {
  1042. [ -n "$2" ] && local hint="\n $2\n\n "
  1043.  
  1044. error "$1
  1045.  
  1046. Hint${hint:+s}:
  1047. ${hint}Maybe you have not created a gpg key yet (e.g. gpg --gen-key)?
  1048. Don't forget the used _password_ as you will need it.
  1049. When done enter the 8 digit id & the password in the profile conf file.
  1050.  
  1051. The key id can be found doing a 'gpg --list-keys'. In the example output
  1052. below the key id would be FFFFFFFF for the public key.
  1053.  
  1054. pub 1024D/FFFFFFFF 2007-12-17
  1055. uid duplicity
  1056. sub 2048g/899FE27F 2007-12-17
  1057. "
  1058. }
  1059.  
  1060. function error_gpg_key {
  1061. local KEY_ID="$1"
  1062. local KIND="$2"
  1063. error_gpg "${KIND} gpg key '${KEY_ID}' cannot be found." \
  1064. "Doublecheck if the above key is listed by 'gpg --list-keys' or available
  1065. as gpg key file '$(basename "$(gpg_keyfile "${KEY_ID}")")' in the profile folder.
  1066. If not you can put it there and $ME_NAME will autoimport it on the next run.
  1067. Alternatively import it manually as the user you plan to run $ME_NAME with."
  1068. }
  1069.  
  1070. function error_gpg_test {
  1071. [ -n "$2" ] && local hint="\n $2\n\n "
  1072.  
  1073. error "$1
  1074.  
  1075. Hint${hint:+s}:
  1076. ${hint}This error means that gpg is probably misconfigured or not working
  1077. correctly. The error message above should help to solve the problem.
  1078. However, if for some reason $ME_NAME should misinterpret the situation you
  1079. can define GPG_TEST='disabled' in the conf file to bypass the test.
  1080. Please do not forget to report the bug in order to resolve the problem
  1081. in future versions of $ME_NAME.
  1082. "
  1083. }
  1084.  
  1085. function error_path {
  1086. error "$@
  1087. PATH='$PATH'
  1088. "
  1089. }
  1090.  
  1091. function error_to_string {
  1092. [ -n "$1" ] && [ "$1" -eq 0 ] && echo "OK" || echo "FAILED 'code $1'"
  1093. }
  1094.  
  1095. function duplicity_version_get {
  1096. # nothing to do, just print
  1097. var_isset DUPL_VERSION && return
  1098.  
  1099. local DUPL_VERSION_OUT DUPL_VERSION_AWK PYTHON_BIN CMD='duplicity'
  1100. # only run with a user specific python if configured (running by default
  1101. # breaks homebrew as they place a shell wrapper for duplicity in path)
  1102. PYTHON_BIN="$(python_binary)" &&\
  1103. CMD="$(qw "$PYTHON_BIN") $(which $CMD)"
  1104. CMD="$CMD --version 2>&1"
  1105. DUPL_VERSION_OUT=$(eval "$CMD")
  1106. DUPL_VERSION=`echo $DUPL_VERSION_OUT | awk '/^duplicity /{print $2; exit;}'`
  1107. #DUPL_VERSION='0.7.03' #'0.6.08b' #,0.4.4.RC4,0.6.08b
  1108. DUPL_VERSION_VALUE=0
  1109. DUPL_VERSION_AWK=$(awk -v v="$DUPL_VERSION" 'BEGIN{
  1110. if (match(v,/[^\.0-9]+[0-9]*$/)){
  1111. rest=substr(v,RSTART,RLENGTH);v=substr(v,0,RSTART-1);}
  1112. if (pos=match(rest,/RC([0-9]+)$/)) rc=substr(rest,pos+2)
  1113. split(v,f,"[. ]"); if(f[1]f[2]f[3]~/^[0-9]+$/) vvalue=f[1]*10000+f[2]*100+f[3]; else vvalue=0
  1114. print "#"v"_"rest"("rc"):"f[1]"-"f[2]"-"f[3]
  1115. print "DUPL_VERSION_VALUE=\047"vvalue"\047"
  1116. print "DUPL_VERSION_RC=\047"rc"\047"
  1117. print "DUPL_VERSION_SUFFIX=\047"rest"\047"
  1118. }')
  1119. eval "$DUPL_VERSION_AWK"
  1120. #echo -e ",$DUPL_VERSION,$DUPL_VERSION_VALUE,$DUPL_VERSION_RC,$DUPL_VERSION_SUFFIX,"
  1121.  
  1122. # doublecheck findings and report error
  1123. if [ $DUPL_VERSION_VALUE -eq 0 ]; then
  1124. inform "duplicity version check failed (please report, this is a bug)
  1125. the command
  1126. $CMD
  1127. resulted in
  1128. $DUPL_VERSION_OUT
  1129. "
  1130. elif [ $DUPL_VERSION_VALUE -le 404 ] && [ ${DUPL_VERSION_RC:-4} -lt 4 ]; then
  1131. error "The installed version $DUPL_VERSION is incompatible with $ME_NAME v$ME_VERSION.
  1132. You should upgrade your version of duplicity to at least v0.4.4RC4 or
  1133. use the older ftplicity version 1.1.1 from $ME_WEBSITE."
  1134. fi
  1135. }
  1136.  
  1137. function duplicity_version_ge {
  1138. [ "$DUPL_VERSION_VALUE" -ge "$1" ]
  1139. }
  1140.  
  1141. function duplicity_version_lt {
  1142.  ! duplicity_version_ge "$1"
  1143. }
  1144.  
  1145. # parse interpreter from duplicity shebang
  1146. function duplicity_python_binary_parse {
  1147. # cached result
  1148. ( var_isset 'PYTHON' || var_isset 'DUPL_PYTHON_BIN' ) && return
  1149.  
  1150. # parse it or warn
  1151. local DUPL_BIN=$(which duplicity)
  1152. DUPL_PYTHON_BIN=$(awk 'NR==1&&/^#!/{sub(/^#!( *\/usr\/bin\/env *)?/,""); print}' < "$DUPL_BIN")
  1153. if ! echo "$DUPL_PYTHON_BIN" | grep -q -i 'python'; then
  1154. warning "Could not parse the python interpreter used from duplicity ($DUPL_BIN). Result was '$DUPL_PYTHON_BIN'.
  1155. Will assume it is '$DEFAULT_PYTHON'."
  1156. DUPL_PYTHON_BIN="$DEFAULT_PYTHON"
  1157. fi
  1158. }
  1159.  
  1160. function run_script { # run pre/post scripts
  1161. local ERR=0
  1162. local SCRIPT="$1"
  1163. if [ ! -z "$PREVIEW" ] ; then
  1164. echo "$([ ! -x "$SCRIPT" ] && echo ". ")$SCRIPT"
  1165. elif [ -r "$SCRIPT" ] ; then
  1166. echo -n "Running '$SCRIPT' "
  1167. if [ -x "$SCRIPT" ]; then
  1168. OUT=$("$SCRIPT" 2>&1)
  1169. ERR=$?
  1170. else
  1171. OUT=$(. "$SCRIPT" 2>&1)
  1172. ERR=$?
  1173. fi
  1174. [ $ERR -eq "0" ] && echo "- OK" || echo "- FAILED (code $ERR)"
  1175. echo -en ${OUT:+"Output: $OUT\n"} ;
  1176. else
  1177. echo "Skipping n/a script '$SCRIPT'."
  1178. fi
  1179. return $ERR
  1180. }
  1181.  
  1182. function run_cmd {
  1183. # run or print escaped cmd string
  1184. local CMD_ERR=0
  1185. if [ -n "$PREVIEW" ]; then
  1186. CMD_OUT=$( echo "$@ 2>&1" )
  1187. CMD_MSG="-- Run cmd -- $CMD_MSG --\n$CMD_OUT"
  1188. elif [ -n "$CMD_DISABLED" ]; then
  1189. CMD_MSG="$CMD_MSG (DISABLED) - $CMD_DISABLED"
  1190. else
  1191. echo -n -e "$CMD_MSG"
  1192. CMD_OUT=` eval "$@" 2>&1 `
  1193. CMD_ERR=$?
  1194. if [ "$CMD_ERR" = "0" ]; then
  1195. CMD_MSG=" (OK)"
  1196. else
  1197. CMD_MSG=" (FAILED)"
  1198. fi
  1199. fi
  1200. echo -e "$CMD_MSG"
  1201. # reset
  1202. unset CMD_DISABLED CMD_MSG
  1203. return $CMD_ERR
  1204. }
  1205.  
  1206. function qw { quotewrap "$@"; }
  1207.  
  1208. function quotewrap {
  1209. local param="$@"
  1210. # quote strings having non word chars (e.g. spaces)
  1211. if echo "$param" | awk '/[^A-Za-z0-9_\.\-\/]/{exit 0}{exit 1}'; then
  1212. echo "$param" | awk '{\
  1213. gsub(/[\047]/,"\047\\\047\047",$0);\
  1214. gsub(/[\042]/,"\047\\\042\047",$0);\
  1215. print "\047"$0"\047"}'
  1216. return
  1217. fi
  1218. echo $param
  1219. }
  1220.  
  1221. function duplicity_params_global {
  1222. # already done? return
  1223. var_isset 'DUPL_PARAMS_GLOBAL' && return
  1224. local DUPL_ARG_ENC
  1225.  
  1226. # use key only if set in config, else leave it to symmetric encryption
  1227. if gpg_disabled; then
  1228. local DUPL_PARAM_ENC='--no-encryption'
  1229. else
  1230. local DUPL_PARAM_ENC=$(gpg_prefix_keyset '--encrypt-key' "${GPG_KEYS_ENC_ARRAY[@]}")
  1231. gpg_signing && local DUPL_PARAM_SIGN=$(gpg_prefix_keyset '--sign-key' "$GPG_KEY_SIGN")
  1232. # interpret password settings
  1233. var_isset 'GPG_PW' && DUPL_ARG_ENC="PASSPHRASE=$(qw "${GPG_PW}")"
  1234. var_isset 'GPG_PW_SIGN' && DUPL_ARG_ENC="${DUPL_ARG_ENC} SIGN_PASSPHRASE=$(qw "${GPG_PW_SIGN}")"
  1235. fi
  1236.  
  1237. local GPG_OPTS=${GPG_OPTS:+"--gpg-options $(qw "${GPG_OPTS}")"}
  1238.  
  1239. # set name for dupl archive folder, since 0.6.0
  1240. if duplicity_version_ge 601; then
  1241. local DUPL_ARCHDIR=''
  1242. if var_isset 'ARCH_DIR'; then
  1243. DUPL_ARCHDIR="--archive-dir $(qw "${ARCH_DIR}")"
  1244. # reuse erronously duply_ prefixed folders from bug #117
  1245. if [ -d "$ARCH_DIR/duply_${PROFILE}" ]; then
  1246. DUPL_ARCHDIR="${DUPL_ARCHDIR} --name $(qw "duply_${PROFILE}")"
  1247. else
  1248. DUPL_ARCHDIR="${DUPL_ARCHDIR} --name $(qw "${PROFILE}")"
  1249. fi
  1250. else
  1251. DUPL_ARCHDIR="--name $(qw "duply_${PROFILE}")"
  1252. fi
  1253. fi
  1254.  
  1255. DUPL_PARAMS_GLOBAL="${DUPL_ARCHDIR} ${DUPL_PARAM_ENC} \
  1256. ${DUPL_PARAM_SIGN} --verbosity '${VERBOSITY:-4}' \
  1257. ${GPG_OPTS}"
  1258.  
  1259. DUPL_VARS_GLOBAL="TMPDIR='$TEMP_DIR' \
  1260. ${DUPL_ARG_ENC}"
  1261. }
  1262.  
  1263. # function to filter the DUPL_PARAMS var from user conf
  1264. function duplicity_params_conf {
  1265. # reuse cmd var from main loop
  1266. ## in/exclude parameters are currently not supported on restores
  1267. if [ "$cmd" = "fetch" ] || [ "$cmd" = "restore" ] || [ "$cmd" = "status" ]; then
  1268. # filter exclude params from fetch/restore/status
  1269. eval "stripXcludes $DUPL_PARAMS"
  1270. return
  1271. fi
  1272.  
  1273. echo "$DUPL_PARAMS"
  1274. }
  1275.  
  1276. # strip in/exclude parameters from param string
  1277. function stripXcludes {
  1278. local STRIPNEXT OUT;
  1279. for p in "$@"; do
  1280. if [ -n "$STRIPNEXT" ]; then
  1281. unset STRIPNEXT
  1282. # strip the value of previous parameter
  1283. continue
  1284. elif echo "$p" | awk '/^\-\-(in|ex)clude(\-[a-zA-Z]+)?$/{exit 0;}{exit 1;}'; then
  1285. # strips eg. --include /foo/bar
  1286. STRIPNEXT="yes"
  1287. continue
  1288. elif echo "$p" | awk '/^\-\-(in|ex)clude(\-[a-zA-Z]+)?=/{exit 0;}{exit 1;}'; then
  1289. # strips eg. --include=/foo/bar
  1290. continue
  1291. fi
  1292.  
  1293. OUT="$OUT $(qw "$p")"
  1294. done
  1295. echo "$OUT"
  1296. }
  1297.  
  1298. function duplify { # the actual wrapper function
  1299. local PARAMSNOW DUPL_CMD DUPL_CMD_PARAMS
  1300.  
  1301. # put command (with params) first in duplicity parameters
  1302. for param in "$@" ; do
  1303. # split cmd from params (everything before splitchar --)
  1304. if [ "$param" == "--" ] ; then
  1305. PARAMSNOW=1
  1306. else
  1307. # wrap in quotes to protect from spaces
  1308. [ ! $PARAMSNOW ] && \
  1309. DUPL_CMD="$DUPL_CMD $(qw "$param")" \
  1310. || \
  1311. DUPL_CMD_PARAMS="$DUPL_CMD_PARAMS $(qw "$param")"
  1312. fi
  1313. done
  1314.  
  1315. # init global duplicity parameters same for all tasks
  1316. duplicity_params_global
  1317.  
  1318. local RUN=eval BIN=duplicity DUPL_BIN PYTHON_BIN
  1319. # run in cmd line preview mode if requested
  1320. var_isset 'PREVIEW' && RUN=echo
  1321. # try to resolve duplicity path for usage with python interpreter
  1322. DUPL_BIN=$(which "$BIN") || DUPL_BIN="$BIN"
  1323. # only run with a user specific python if configured (running by default
  1324. # breaks homebrew as they place a shell wrapper for duplicity in path)
  1325. PYTHON_BIN="$(python_binary)" &&\
  1326. BIN="$(qw "$PYTHON_BIN") $(qw "$DUPL_BIN")"
  1327.  
  1328. $RUN "${DUPL_VARS_GLOBAL} ${BACKEND_PARAMS}\
  1329. ${DUPL_PRECMD} $BIN $DUPL_CMD $DUPL_PARAMS_GLOBAL $(duplicity_params_conf)\
  1330. $GPG_USEAGENT $(gpg_custom_binary) $DUPL_CMD_PARAMS"
  1331.  
  1332. local ERR=$?
  1333. return $ERR
  1334. }
  1335.  
  1336. function secureconf { # secure the configuration dir
  1337. #PERMS=$(ls -la $(dirname $CONFDIR) | grep -e " $(basename $CONFDIR)\$" | awk '{print $1}')
  1338. local PERMS="$(ls -la "$CONFDIR/." | awk 'NR==2{print $1}')"
  1339. if [ "${PERMS/#drwx------*/OK}" != 'OK' ] ; then
  1340. chmod u+rwX,go= "$CONFDIR"; local ERR=$?
  1341. warning "The profile's folder
  1342. '$CONFDIR'
  1343. permissions are not safe ($PERMS). Secure them now. - ($(error_to_string $ERR))"
  1344. fi
  1345. }
  1346.  
  1347. # params are $1=timeformatstring (default like date output), $2=epoch seconds since 1.1.1970 (default now)
  1348. function date_fix {
  1349. local DEFAULTFORMAT='%a %b %d %H:%M:%S %Z %Y'
  1350. local date
  1351. #[ "$1" == "%N" ] && return #test the no nsec test below
  1352. # gnu date with -d @epoch
  1353. date=$(date ${2:+-d @$2} ${1:++"$1"} 2> /dev/null) && \
  1354. echo $date && return
  1355. # date bsd,osx with -r epoch
  1356. date=$(date ${2:+-r $2} ${1:++"$1"} 2> /dev/null) && \
  1357. echo $date && return
  1358. # date busybox with -d epoch -D %s
  1359. date=$(date ${2:+-d $2 -D %s} ${1:++"$1"} 2> /dev/null) && \
  1360. echo $date && return
  1361. ## some date commands do not support giving a time w/o setting it systemwide (irix,solaris,others?)
  1362. # python fallback
  1363. date=$("$(python_binary)" -c "import time;print time.strftime('${1:-$DEFAULTFORMAT}',time.localtime(${2}))" 2> /dev/null) && \
  1364. echo $date && return
  1365. # awk fallback
  1366. date=$(awk "BEGIN{print strftime(\"${1:-$DEFAULTFORMAT}\"${2:+,$2})}" 2> /dev/null) && \
  1367. echo $date && return
  1368. # perl fallback
  1369. date=$(perl -e "use POSIX qw(strftime);\$date = strftime(\"${1:-$DEFAULTFORMAT}\",localtime(${2}));print \"\$date\n\";" 2> /dev/null) && \
  1370. echo $date && return
  1371. # error
  1372. echo "ERROR"
  1373. return 1
  1374. }
  1375.  
  1376. function nsecs {
  1377. local NSECS
  1378. # test if date supports nanosecond output
  1379. if ! var_isset NSECS_DISABLED; then
  1380. NSECS=$(date_fix %N 2> /dev/null | head -1 |grep -e "^[[:digit:]]\{9\}$")
  1381. [ -n "$NSECS" ] && NSECS_DISABLED=0 || NSECS_DISABLED=1
  1382. fi
  1383.  
  1384. # add 9 digits, not all date(s) deliver nsecs eg. busybox date
  1385. if [ "$NSECS_DISABLED" == "1" ]; then
  1386. date_fix %s000000000
  1387. else
  1388. date_fix %s%N
  1389. fi
  1390. }
  1391.  
  1392. function nsecs_to_sec {
  1393. echo $(($1/1000000000)).$(printf "%03d" $(($1/1000000%1000)) )
  1394. }
  1395.  
  1396. function datefull_from_nsecs {
  1397. date_from_nsecs $1 '%F %T'
  1398. }
  1399.  
  1400. function date_from_nsecs {
  1401. local FORMAT=${2:-%T}
  1402. local TIME=$(nsecs_to_sec $1)
  1403. local SECS=${TIME%.*}
  1404. local DATE=$(date_fix "%T" ${SECS:-0})
  1405. echo $DATE.${TIME#*.}
  1406. }
  1407.  
  1408. function var_isset {
  1409. if [ -z "$1" ]; then
  1410. echo "ERROR: function var_isset needs a string as parameter"
  1411. elif eval "[ \"\${$1}\" == 'not_set' ]" || eval "[ \"\${$1-not_set}\" != 'not_set' ]"; then
  1412. return 0
  1413. fi
  1414. return 1
  1415. }
  1416.  
  1417. function is_condition {
  1418. local CMD=$(tolower "$@")
  1419. [ "$CMD" == 'and' ] || [ "$CMD" == 'or' ]
  1420. }
  1421.  
  1422. function url_encode {
  1423. # utilize python, silently do nothing on error - because no python no duplicity
  1424. OUT=$("$(python_binary)" -c "
  1425. try: import urllib.request as urllib
  1426. except ImportError: import urllib
  1427. print(urllib.${2}quote('$1'));
  1428. " 2>/dev/null ); ERR=$?
  1429. [ "$ERR" -eq 0 ] && echo $OUT || echo $1
  1430. }
  1431.  
  1432. function url_decode {
  1433. # reuse function above with a simple string param hack
  1434. url_encode "$1" "un"
  1435. }
  1436.  
  1437. function toupper {
  1438. echo "$@"|awk '$0=toupper($0)'
  1439. }
  1440.  
  1441. function tolower {
  1442. echo "$@"|awk '$0=tolower($0)'
  1443. }
  1444.  
  1445. function isnumber {
  1446. case "$*" in
  1447. ''|*[!0-9]*) return 1;;
  1448. *) return 0;;
  1449. esac
  1450. }
  1451.  
  1452. #function tmp_space {
  1453. #
  1454. # if ! isnumber $VOLSIZE; then
  1455. # inform "failed to determine free space (please report, this is a bug)"
  1456. # return
  1457. # fi
  1458. #
  1459. # get free temp space
  1460. # TEMP_FREE="$(df -P -k "$TEMP_DIR" 2>/dev/null | awk 'END{pos=(NF-2);if(pos>0) print $pos;}')"
  1461. # # check for free space or FAIL
  1462. # if [ $((${TEMP_FREE:-0}-${VOLSIZE:-0}*1024)) -lt 0-lt 0 ]; then
  1463. # error "Temporary file space '$TEMP_DIR' free space is smaller ($((TEMP_FREE/1024))MB)
  1464. #than one duplicity volume (${VOLSIZE}MB).
  1465. #
  1466. # Hint: Free space or change TEMP_DIR setting."
  1467. #fi
  1468. #
  1469. #}
  1470.  
  1471. function gpg_disabled {
  1472. echo "${GPG_KEY}" | grep -iq -e '^disabled$'
  1473. }
  1474.  
  1475. # usage: join SEPARATOR "entry1" "entry2"
  1476. function join {
  1477. local SEP="$1" ENTRY OUT; shift;
  1478. for ENTRY in "$@"; do
  1479. ENTRY=${ENTRY//$SEP/\\$SEP}
  1480. [ -z "$OUT" ] && OUT=$ENTRY || OUT="$OUT$SEP$ENTRY"
  1481. done
  1482. echo $OUT
  1483. }
  1484.  
  1485. function gpg_signing {
  1486. echo ${GPG_KEY_SIGN} | grep -v -q -e '^disabled$'
  1487. }
  1488.  
  1489. # parameter key id, key_type
  1490. function gpg_keyfile {
  1491. local GPG_KEY=$(gpg_key_legalize $1) TYPE="$2"
  1492. local KEYFILE="${KEYFILE//.asc/${GPG_KEY:+.$GPG_KEY}.asc}"
  1493. echo "${KEYFILE//.asc/${TYPE:+.$(tolower $TYPE)}.asc}"
  1494. }
  1495.  
  1496. # parameter key id
  1497. function gpg_import {
  1498. local i FILE FOUND=0 KEY_ID="$1" KEY_TYPE="$2" KEY_FP="" ERR=0
  1499. # create a list of legacy key file names and current naming scheme
  1500. # we always import pub and sec if they are avail in conf folder
  1501. local KEYFILES=( "$CONFDIR/gpgkey" $(gpg_keyfile "$KEY_ID") \
  1502. $(gpg_keyfile "$KEY_ID" PUB) $(gpg_keyfile "$KEY_ID" SEC))
  1503.  
  1504. # Try autoimport from existing old gpgkey files
  1505. # and new gpgkey.XXX.asc files (since v1.4.2)
  1506. # and even newer gpgkey.XXX.[pub|sec].asc
  1507. for (( i = 0 ; i < ${#KEYFILES[@]} ; i++ )); do
  1508. FILE=${KEYFILES[$i]}
  1509. if [ -f "$FILE" ]; then
  1510. FOUND=1
  1511.  
  1512. CMD_MSG="Import keyfile '$FILE' to keyring"
  1513. run_cmd gpg $GPG_OPTS --batch --import $(qw "$FILE")
  1514. if [ "$?" != "0" ]; then
  1515. warning "Import failed.${CMD_OUT:+\n$CMD_OUT}"
  1516. ERR=1
  1517. # continue with next
  1518. continue
  1519. fi
  1520. fi
  1521. done
  1522.  
  1523. if [ "$FOUND" -eq 0 ]; then
  1524. warning "No keyfile for '$KEY_ID' found in profile\n'$CONFDIR'."
  1525. fi
  1526.  
  1527. # try to set trust automagically
  1528. CMD_MSG="Autoset trust of key '$KEY_ID' to ultimate"
  1529. run_cmd echo $(gpg_fingerprint "$KEY_ID"):6: \| gpg $GPG_OPTS --import-ownertrust --batch --logger-fd 1
  1530. if [ "$?" = "0" ] && [ -z "$PREVIEW" ]; then
  1531. # success on all levels, we're done
  1532. return $ERR
  1533. fi
  1534.  
  1535. # failover: user has to set trust manually
  1536. echo -e "For $ME_NAME to work you have to set the trust level
  1537. with the command \"trust\" to \"ultimate\" (5) now.
  1538. Exit the edit mode of gpg with \"quit\"."
  1539. CMD_MSG="Running gpg to manually edit key '$KEY_ID'"
  1540. run_cmd sleep 5\; gpg $GPG_OPTS --edit-key $(qw "$KEY_ID")
  1541.  
  1542. return $ERR
  1543. }
  1544.  
  1545. # see 'How to specify a user ID' on gpg manpage
  1546. function gpg_fingerprint {
  1547. local PRINT=$(gpg $GPG_OPTS --fingerprint "$1" 2>&1|awk -F= 'NR==2{gsub(/ /,"",$2);$2=toupper($2); if ( $2 ~ /^[A-F0-9]+$/ && length($2) == 40 ) print $2; else exit 1}') \
  1548. && [ -n "$PRINT" ] && echo $PRINT && return 0
  1549. return 1
  1550. }
  1551.  
  1552. function gpg_export_if_needed {
  1553. local SUCCESS FILE KEY_TYPE
  1554. local TMPFILE="$TEMP_DIR/${ME_NAME}.$$.$(date_fix %s).gpgexp"
  1555. for KEY_ID in "$@"; do
  1556. # check if already exported, do it if not
  1557. for KEY_TYPE in PUB SEC; do
  1558. FILE="$(gpg_keyfile "$KEY_ID" $KEY_TYPE)"
  1559. if [ ! -f "$FILE" ] && eval gpg_$(tolower $KEY_TYPE)_avail \"$KEY_ID\"; then
  1560. # exporting
  1561. CMD_MSG="Backup $KEY_TYPE key '$KEY_ID' to profile."
  1562. # gpg2.1 insists on passphrase here, gpg2.0- happily exports w/o it
  1563. # we pipe an empty string when GPG_PW is not set to avoid gpg silently waiting for input
  1564. run_cmd $(gpg_pass_pipein GPG_PW_SIGN GPG_PW) gpg $GPG_OPTS $GPG_USEAGENT $(gpg_param_passwd GPG_PW_SIGN GPG_PW) --armor --export"$(test "SEC" = "$KEY_TYPE" && echo -secret-keys)" $(qw "$KEY_ID") '>>' $(qw "$TMPFILE")
  1565. CMD_ERR=$?
  1566.  
  1567. if [ "$CMD_ERR" = "0" ]; then
  1568. CMD_MSG="Write file '"$(basename "$FILE")"'"
  1569. run_cmd mv $(qw "$TMPFILE") $(qw "$FILE")
  1570. fi
  1571.  
  1572. if [ "$CMD_ERR" != "0" ]; then
  1573. warning "Backup failed.${CMD_OUT:+\n$CMD_OUT}"
  1574. else
  1575. SUCCESS=1
  1576. fi
  1577.  
  1578. # cleanup
  1579. rm $(qw "$TMPFILE") 1>/dev/null 2>&1
  1580. fi
  1581. done
  1582. done
  1583.  
  1584. [ -n "$SUCCESS" ] && inform "$ME_NAME exported new keys to your profile.
  1585. You should backup your changed profile folder now and store it in a safe place."
  1586. }
  1587.  
  1588. # replace all non-alnum chars with underscore (for file operations)
  1589. function gpg_key_legalize {
  1590. echo $* | awk '{gsub(/[^a-zA-Z0-9]/,"_",$0); print}'
  1591. }
  1592.  
  1593. function gpg_key_cache {
  1594. local RES
  1595. local MODE=$1
  1596. shift
  1597. local PREFIX="GPG_KEY"
  1598. local SUFFIX=$(gpg_key_legalize "$@")
  1599. local KEYID="$*"
  1600. local CACHE="${PREFIX}_${MODE}_${SUFFIX}"
  1601. if [ "$MODE" = "RESET" ]; then
  1602. eval unset ${PREFIX}_PUB_$SUFFIX ${PREFIX}_SEC_$SUFFIX
  1603. return 255
  1604. elif ! var_isset "$CACHE"; then
  1605. if [ "$MODE" = "PUB" ]; then
  1606. RES=$(gpg $GPG_OPTS --list-key "$KEYID" > /dev/null 2>&1; echo -n $?)
  1607. elif [ "$MODE" = "SEC" ]; then
  1608. RES=$(gpg $GPG_OPTS --list-secret-key "$KEYID" > /dev/null 2>&1; echo -n $?)
  1609. else
  1610. return 255
  1611. fi
  1612. eval $CACHE=$RES
  1613. fi
  1614. eval return \$$CACHE
  1615. }
  1616.  
  1617. function gpg_pub_avail {
  1618. gpg_key_cache PUB "$@"
  1619. }
  1620.  
  1621. function gpg_sec_avail {
  1622. gpg_key_cache SEC "$@"
  1623. }
  1624.  
  1625. function gpg_key_format {
  1626. echo $1 | grep -q '^[0-9a-fA-F]\{8\}$'
  1627. }
  1628.  
  1629. # splits a comma separated line into lines, respects escaped commas
  1630. function gpg_split_keyset {
  1631. local LIST
  1632. LIST=$(echo "$@" | awk '{ gsub(/,/,"\n",$0); gsub(/\\\n/,",",$0); print $0 }')
  1633. echo -e "$LIST"
  1634. }
  1635.  
  1636. function gpg_prefix_keyset {
  1637. local PREFIX="$1" OUT=""
  1638. shift
  1639. for KEY_ID in "$@"; do
  1640. OUT="${OUT} $PREFIX $(qw ${KEY_ID})"
  1641. done
  1642. echo $OUT
  1643. }
  1644.  
  1645. # grep a variable from conf text file (currently not used)
  1646. function gpg_passwd {
  1647. [ -r "$CONF" ] && \
  1648. awk '/^[ \t]*GPG_PW[ \t=]/{\
  1649. sub(/^[ \t]*GPG_PW[ \t]*=*/,"",$0);\
  1650. gsub(/^[ \t]*[\047"]|[\047"][ \t]*$/,"",$0);\
  1651. print $0; exit}' "$CONF"
  1652. }
  1653.  
  1654. # return success if at least one secret key is available
  1655. function gpg_key_decryptable {
  1656. local KEY_ID
  1657. for KEY_ID in "${GPG_KEYS_ENC_ARRAY[@]}"; do
  1658. gpg_sec_avail "$KEY_ID" && return 0
  1659. done
  1660. return 1
  1661. }
  1662.  
  1663. function gpg_symmetric {
  1664. [ -z "${GPG_KEY}${GPG_KEYS_ENC_ARRAY}" ]
  1665. }
  1666.  
  1667. # checks for max two params if they are set, typically GPG_PW & GPG_PW_SIGN
  1668. function gpg_param_passwd {
  1669. var_isset GPG_USEAGENT && exit 1
  1670.  
  1671. if ( [ -n "$1" ] && var_isset "$1" ) || ( [ -n "$2" ] && var_isset "$2" ); then
  1672. echo "--passphrase-fd 0 --batch"
  1673. fi
  1674. }
  1675.  
  1676. # select the earlist defined and create an "echo <value> |" string
  1677. function gpg_pass_pipein {
  1678. var_isset GPG_USEAGENT && exit 1
  1679.  
  1680. for var in "$@"
  1681. do
  1682. if var_isset "$var"; then
  1683. echo "echo $(qw $(eval echo \$$var)) |"
  1684. return 0
  1685. fi
  1686. done
  1687.  
  1688. return 1
  1689. }
  1690.  
  1691. # checks if gpg-agent is available, returns error code
  1692. # 0 on success
  1693. # 1 if GPG_AGENT_INFO is not set (unused, should probably be merged w/ 3)
  1694. # 2 if GPG_AGENT_INFO is stale
  1695. # 3 cannot connect to gpg-agent
  1696. function gpg_agent_avail {
  1697. # GPG_AGENT_INFO is deprecated in gpg2.1,
  1698. # first try to connect to a possibly running agent here
  1699. local ERR=3
  1700. gpg-agent > /dev/null 2>&1 && return 0
  1701.  
  1702. # detect stale pid in legacy GPG_AGENT_INFO env var
  1703. if var_isset GPG_AGENT_INFO; then
  1704. # check if a pid matching process is running at all
  1705. local GPG_AGENT_PID=$(echo $GPG_AGENT_INFO|awk -F: '{print $2}')
  1706. if isnumber "$GPG_AGENT_PID"; then
  1707. ps -p "$GPG_AGENT_PID" > /dev/null 2>&1 || ERR=2
  1708. fi
  1709. fi
  1710.  
  1711. return $ERR
  1712. }
  1713.  
  1714. function gpg_custom_binary {
  1715. var_isset GPG && [ "$GPG" != "$DEFAULT_GPG" ] &&\
  1716. echo "--gpg-binary $(qw "$GPG")"
  1717. }
  1718.  
  1719. function gpg_binary {
  1720. local BIN
  1721. var_isset GPG && BIN="$GPG" || BIN="$DEFAULT_GPG"
  1722. echo "$BIN"
  1723. }
  1724.  
  1725. function gpg_avail {
  1726. lookup "$(gpg_binary)"
  1727. }
  1728.  
  1729. # enforce the use of our selected gpg binary
  1730. function gpg {
  1731. command "$(gpg_binary)" "$@"
  1732. }
  1733. export -f gpg
  1734.  
  1735. # start of script #######################################################################
  1736.  
  1737. # confidentiality first, all we create is only readable by us
  1738. umask 077
  1739.  
  1740. # check if ftplicity is there & executable
  1741. [ -n "$ME_LONG" ] && [ -x "$ME_LONG" ] || error "$ME missing. Executable & available in path? ($ME_LONG)"
  1742.  
  1743. if [ ${#@} -eq 1 ]; then
  1744. cmd="${1}"
  1745. else
  1746. FTPLCFG="${1}" ; cmd="${2}"
  1747. fi
  1748.  
  1749. # deal with command before profile validation calls
  1750. # show requested version
  1751. # OR requested usage info
  1752. # OR create a profile
  1753. # OR fall through
  1754. ##if [ ${#@} -le 2 ]; then
  1755. case "$cmd" in
  1756. changelog)
  1757. changelog
  1758. exit 0
  1759. ;;
  1760. create)
  1761. set_config
  1762. if [ -d "$CONFDIR" ]; then
  1763. error "The profile '$FTPLCFG' already exists in
  1764. '$CONFDIR'.
  1765.  
  1766. Hint:
  1767. If you _really_ want to create a new profile by this name you will
  1768. have to manually delete the existing profile folder first."
  1769. exit 1
  1770. else
  1771. create_config
  1772. exit 0
  1773. fi
  1774. ;;
  1775. txt2man)
  1776. set_config
  1777. usage_txt2man
  1778. exit 0
  1779. ;;
  1780. usage|-help|--help|-h|-H)
  1781. set_config
  1782. usage_info
  1783. exit 0
  1784. ;;
  1785. version|-version|--version|-v|-V)
  1786. # profile can override GPG/PYTHON, so import it if it was given
  1787. var_isset FTPLCFG && {
  1788. set_config
  1789. [ -r "$CONF" ] && . "$CONF" || warning "Cannot import config '$CONF'."
  1790. }
  1791. version_info_using
  1792. exit 0
  1793. ;;
  1794. # fallthrough.. we got a command that needs an existing profile
  1795. *)
  1796. # if we reach here, user either forgot profile or chose wrong profileless command
  1797. if [ ${#@} -le 1 ]; then
  1798. error "\
  1799. Missing or wrong parameters.
  1800. Only the commands
  1801. changelog, create, usage, txt2man, version
  1802. can be called without selecting an existing profile first.
  1803. Your command was '$cmd'.
  1804.  
  1805. Hint: Run '$ME usage' to get help."
  1806. fi
  1807. esac
  1808.  
  1809.  
  1810. # Hello world
  1811. echo "Start $ME v$ME_VERSION, time is $(date_fix '%F %T')."
  1812.  
  1813. # check system environment
  1814.  
  1815. # is duplicity avail
  1816. lookup duplicity || error_path "duplicity missing. installed und available in path?"
  1817. # init, exec duplicity version check info
  1818. duplicity_python_binary_parse
  1819. duplicity_version_get
  1820.  
  1821. # check for certain important helper programs
  1822. for f in awk grep "$(python_binary)"; do
  1823. lookup "$f" || \
  1824. error_path "$f missing. installed und available in path?"
  1825. done
  1826.  
  1827. ### read configuration
  1828. set_config
  1829. # check validity
  1830. if [ ! -d "$CONFDIR" ]; then
  1831. error "Selected profile '$FTPLCFG' does not resolve to a profile folder in
  1832. '$CONFDIR'.
  1833.  
  1834. Hints:
  1835. Select one of the available profiles: $(for d in "$(dirname "$CONFDIR")"/*/; do [ -e "$d" ] || [ -L "$d" ] || continue; printf "$sep'$(basename "$d")'"; sep=",";done)
  1836. Use '$ME <name> create' to create a new profile.
  1837. Use '$ME usage' to get usage help."
  1838. elif [ ! -x "$CONFDIR" ]; then
  1839. error "\
  1840. Profile folder in '$CONFDIR' cannot be accessed.
  1841.  
  1842. Hint:
  1843. Check the filesystem permissions and set directory accessible e.g. 'chmod 700'."
  1844. elif [ ! -f "$CONF" ] ; then
  1845. error "'$CONF' not found."
  1846. elif [ ! -r "$CONF" ] ; then
  1847. error "'$CONF' not readable."
  1848. else
  1849. . "$CONF"
  1850. #KEYFILE="${KEYFILE//.asc/${GPG_KEY:+.$GPG_KEY}.asc}"
  1851. TEMP_DIR=${TEMP_DIR:-'/tmp'}
  1852. # backward compatibility: old TARGET_PW overrides silently new TARGET_PASS if set
  1853. if var_isset 'TARGET_PW'; then
  1854. TARGET_PASS="${TARGET_PW}"
  1855. fi
  1856. fi
  1857. echo "Using profile '$CONFDIR'."
  1858.  
  1859. # secure config dir, if needed w/ warning
  1860. secureconf
  1861.  
  1862. # split TARGET in handy variables
  1863. TARGET_SPLIT_URL=$(echo "$TARGET" | awk '{ \
  1864. target=$0; match(target,/^([^\/:]+):\/\//); \
  1865. prot=substr(target,RSTART,RLENGTH);\
  1866. rest=substr(target,RSTART+RLENGTH); \
  1867. if (credsavail=match(rest,/^[^@]*@/)){\
  1868. creds=substr(rest,RSTART,RLENGTH-1);\
  1869. credcount=split(creds,cred,":");\
  1870. rest=substr(rest,RLENGTH+1);\
  1871. # split creds with regexp\
  1872. match(creds,/^([^:]+)/);\
  1873. user=substr(creds,RSTART,RLENGTH);\
  1874. pass=substr(creds,RSTART+1+RLENGTH);\
  1875. };\
  1876. # filter quotes or escape them\
  1877. gsub(/[\047\042]/,"",prot);\
  1878. gsub(/[\047\042]/,"",rest);\
  1879. gsub(/[\047]/,"\047\\\047\047",creds);\
  1880. print "TARGET_URL_PROT=\047"prot"\047\n"\
  1881. "TARGET_URL_HOSTPATH=\047"rest"\047\n"\
  1882. "TARGET_URL_CREDS=\047"creds"\047\n";\
  1883. if(user){\
  1884. gsub(/[\047]/,"\047\\\047\047",user);\
  1885. print "TARGET_URL_USER=\047"user"\047\n"}\
  1886. if(pass){\
  1887. gsub(/[\047]/,"\047\\\047\047",pass);\
  1888. print "TARGET_URL_PASS=$(url_decode \047"pass"\047)\n"}\
  1889. }')
  1890. eval "${TARGET_SPLIT_URL}"
  1891.  
  1892. # fetch commmand from parameters ########################################################
  1893. # Hint: cmds is also used to check if authentification info sufficient in the next step
  1894. cmds="$2"; shift 2
  1895.  
  1896. # translate backup to batch command
  1897. cmds=${cmds//backup/pre_bkp_post}
  1898.  
  1899. # complain if command(s) missing
  1900. [ -z $cmds ] && error " No command given.
  1901.  
  1902. Hint:
  1903. Use '$ME usage' to get usage help."
  1904.  
  1905. # process params
  1906. for param in "$@"; do
  1907. #echo !$param!
  1908. case "$param" in
  1909. # enable ftplicity preview mode
  1910. '--preview')
  1911. PREVIEW=1
  1912.  ;;
  1913. # interpret duplicity disable encr switch
  1914. '--disable-encryption')
  1915. GPG_KEY='disabled'
  1916.  ;;
  1917. *)
  1918. if [ `echo "$param" | grep -e "^-"` ] || \
  1919. [ `echo "$last_param" | grep -e "^-"` ] ; then
  1920. # forward parameter[/option pairs] to duplicity
  1921. dupl_opts["${#dupl_opts[@]}"]=${param}
  1922. else
  1923. # anything else must be a parameter (eg. for fetch, ...)
  1924. ftpl_pars["${#ftpl_pars[@]}"]=${param}
  1925. fi
  1926. last_param=${param}
  1927.  ;;
  1928. esac
  1929. done
  1930.  
  1931. # plausibility check config - VARS & KEY ################################################
  1932. # check if src, trg, trg pw
  1933. # auth info sufficient
  1934. # gpg key, gpg pwd (might be empty) set in config
  1935. # OR key in local gpg db
  1936. # OR key can be imported from keyfile
  1937. # OR fail
  1938. if [ -z "$SOURCE" ] || [ "$SOURCE" == "${DEFAULT_SOURCE}" ]; then
  1939. error " Source Path (setting SOURCE) not set or still default value in conf file
  1940. '$CONF'."
  1941.  
  1942. elif [ -z "$TARGET" ] || [ "$TARGET" == "${DEFAULT_TARGET}" ]; then
  1943. error " Backup Target (setting TARGET) not set or still default value in conf file
  1944. '$CONF'."
  1945.  
  1946. elif var_isset 'TARGET_USER' && var_isset 'TARGET_URL_USER' && \
  1947. [ "${TARGET_USER}" != "${TARGET_URL_USER}" ]; then
  1948. error " TARGET_USER ('${TARGET_USER}') _and_ user in TARGET url ('${TARGET_URL_USER}')
  1949. are configured with different values. There can be only one.
  1950.  
  1951. Hint: Remove conflicting setting."
  1952.  
  1953. elif var_isset 'TARGET_PASS' && var_isset 'TARGET_URL_PASS' && \
  1954. [ "${TARGET_PASS}" != "${TARGET_URL_PASS}" ]; then
  1955. error " TARGET_PASS ('${TARGET_PASS}') _and_ password in TARGET url ('${TARGET_URL_PASS}')
  1956. are configured with different values. There can be only one.
  1957.  
  1958. Hint: Remove conflicting setting."
  1959. fi
  1960.  
  1961. # GPG config plausibility check1 (disabled check) #############################
  1962. if gpg_disabled; then
  1963.  : # encryption disabled, all is well
  1964. elif [ -z "${GPG_KEY}${GPG_KEYS_ENC}${GPG_KEY_SIGN}" ] && ! var_isset 'GPG_PW'; then
  1965. warning "GPG_KEY, GPG_KEYS_ENC, GPG_KEY_SIGN and GPG_PW are empty/not set in conf file
  1966. '$CONF'.
  1967. Will disable encryption for duplicity now.
  1968.  
  1969. Hint:
  1970. If you really want to use _no_ encryption you can disable this warning by
  1971. setting GPG_KEY='disabled' in conf file."
  1972. GPG_KEY='disabled'
  1973. fi
  1974.  
  1975. # GPG availability check (now we know if gpg is really needed)#################
  1976. if ! gpg_disabled; then
  1977. gpg_avail || error_path "gpg '$(gpg_binary)' missing. installed und available in path?"
  1978. fi
  1979.  
  1980. # Output versions info ########################################################
  1981. using_info
  1982.  
  1983. # GPG create key settings, config check2 (needs gpg) ##########################
  1984. if gpg_disabled; then
  1985.  : # the following tests are not necessary
  1986. else
  1987.  
  1988. # we test this early as any invocation gpg2.1+ starts gpg-agent automatically
  1989. GPG_AGENT_ERR=$(gpg_agent_avail ; echo $?)
  1990.  
  1991. # enc key still default?
  1992. if [ "$GPG_KEY" == "${DEFAULT_GPG_KEY}" ]; then
  1993. error_gpg "Encryption Key GPG_KEY still default in conf file
  1994. '$CONF'."
  1995. fi
  1996.  
  1997. # create array of gpg encr keys, for further processing
  1998. OIFS="$IFS" IFS=$'\n'
  1999. GPG_KEYS_ENC_ARRAY=( $( gpg_split_keyset ${GPG_KEY},${GPG_KEYS_ENC} ) )
  2000. IFS="$OIFS"
  2001.  
  2002. # pw set?
  2003. # symmetric needs one, always
  2004. if gpg_symmetric && ( [ -z "$GPG_PW" ] || [ "$GPG_PW" == "${DEFAULT_GPG_PW}" ] ) \
  2005.  ; then
  2006. error_gpg "Encryption passphrase GPG_PW (needed for symmetric encryption)
  2007. is empty/not set or still default value in conf file
  2008. '$CONF'."
  2009. fi
  2010. # this is a technicality, we can only pump one pass via pipe into gpg
  2011. # but symmetric already always needs one for encryption
  2012. if gpg_symmetric && var_isset GPG_PW && var_isset GPG_PW_SIGN &&\
  2013. [ -n "$GPG_PW_SIGN" ] && [ "$GPG_PW" != "$GPG_PW_SIGN" ]; then
  2014. error_gpg "GPG_PW _and_ GPG_PW_SIGN are defined but not identical in config
  2015. '$CONF'.
  2016. This is unfortunately impossible. For details see duplicity manpage,
  2017. section 'A Note On Symmetric Encryption And Signing'.
  2018.  
  2019. Tip: Separate signing keys may have empty passwords e.g. GPG_PW_SIGN=''.
  2020. Tip2: Use gpg-agent."
  2021. fi
  2022.  
  2023. # check gpg encr public keys availability
  2024. for (( i = 0 ; i < ${#GPG_KEYS_ENC_ARRAY[@]} ; i++ )); do
  2025. KEY_ID="${GPG_KEYS_ENC_ARRAY[$i]}"
  2026. # test availability, try to import, retest
  2027. if ! gpg_pub_avail "${KEY_ID}"; then
  2028. echo "Encryption public key '${KEY_ID}' not found."
  2029. gpg_import "${KEY_ID}" PUB
  2030. gpg_key_cache RESET "${KEY_ID}"
  2031. gpg_pub_avail "${KEY_ID}" || error_gpg_key "${KEY_ID}" "Public"
  2032. fi
  2033. done
  2034.  
  2035. # gpg secret sign key availability
  2036. # if none set, autoset first encryption key as sign key
  2037. if ! gpg_signing; then
  2038. echo "Signing disabled per configuration."
  2039. # try first key, if one set
  2040. elif ! var_isset 'GPG_KEY_SIGN'; then
  2041. KEY_ID="${GPG_KEYS_ENC_ARRAY[0]}"
  2042. if [ -z "${KEY_ID}" ]; then
  2043. echo "Signing disabled. No GPG_KEY entries in config."
  2044. GPG_KEY_SIGN='disabled'
  2045. else
  2046. # use avail OR try import OR fail
  2047. if gpg_sec_avail "${KEY_ID}"; then
  2048. GPG_KEY_SIGN="${KEY_ID}"
  2049. else
  2050. gpg_import "${KEY_ID}" SEC
  2051. gpg_key_cache RESET "${KEY_ID}"
  2052. if gpg_sec_avail "${KEY_ID}"; then
  2053. GPG_KEY_SIGN="${KEY_ID}"
  2054. fi
  2055. fi
  2056.  
  2057. # interpret sign key setting
  2058. if var_isset 'GPG_KEY_SIGN'; then
  2059. echo "Autoset found secret key of first GPG_KEY entry '${KEY_ID}' for signing."
  2060. else
  2061. echo "Signing disabled. First GPG_KEY entry's '${KEY_ID}' private key is missing."
  2062. GPG_KEY_SIGN='disabled'
  2063. fi
  2064. fi
  2065. else
  2066. KEY_ID="${GPG_KEY_SIGN}"
  2067. if ! gpg_sec_avail "${KEY_ID}"; then
  2068. inform "Secret signing key defined in setting GPG_KEY_SIGN='${KEY_ID}' not found.\nTry to import."
  2069. gpg_import "${KEY_ID}" SEC
  2070. gpg_key_cache RESET "${KEY_ID}"
  2071. gpg_sec_avail "${KEY_ID}" || error_gpg_key "${KEY_ID}" "Private"
  2072. fi
  2073. fi
  2074.  
  2075. # using GPG_AGENT_ERR set early above, try to autoenable gpg-agent or issue some warnings
  2076. # key enc can deal without, but might profit from gpg-agent
  2077. # if GPG_PW is not set alltogether
  2078. # if signing key is different from first (main) enc key (we can only pipe one pass into gpg)
  2079. if ! gpg_symmetric && \
  2080. ( ! var_isset GPG_PW || \
  2081. ( gpg_signing && ! var_isset GPG_PW_SIGN && [ "$GPG_KEY_SIGN" != "${GPG_KEYS_ENC_ARRAY[0]}" ] ) ); then
  2082.  
  2083. if [ "$GPG_AGENT_ERR" -eq 1 ]; then
  2084. warning "Cannot use gpg-agent. GPG_AGENT_INFO not set."
  2085. elif [ "$GPG_AGENT_ERR" -eq 2 ]; then
  2086. warning "Cannot use gpg-agent! GPG_AGENT_INFO contains stale pid."
  2087. elif [ "$GPG_AGENT_ERR" -eq 3 ]; then
  2088. warning "No running gpg-agent found although GPG_PW or GPG_PW_SIGN (enc != sign key) not set."
  2089. else
  2090. echo "Enable gpg-agent usage. Running gpg-agent instance found and GPG_PW or GPG_PW_SIGN (enc != sign key) not set."
  2091. GPG_USEAGENT="--use-agent"
  2092. fi
  2093. fi
  2094.  
  2095. # end GPG config plausibility check2
  2096. fi
  2097.  
  2098. # config plausibility check - SPACE ###########################################
  2099.  
  2100. # is tmp is a folder and writable
  2101. CMD_MSG="Checking TEMP_DIR '${TEMP_DIR}' is a folder and writable"
  2102. run_cmd test -d $(qw "$TEMP_DIR") '&&' test -w $(qw "$TEMP_DIR")
  2103. if [ "$?" != "0" ]; then
  2104. error "Temporary file space '$TEMP_DIR' is not a directory or writable."
  2105. fi
  2106.  
  2107.  
  2108. # get volsize, default duplicity volume size is 25MB since v0.5.07
  2109. VOLSIZE=${VOLSIZE:-25}
  2110. # double if asynch is on
  2111. echo $@ $DUPL_PARAMS | grep -q -e '--asynchronous-upload' && FACTOR=2 || FACTOR=1
  2112.  
  2113. # TODO: check for enough (async= upload space and WARN only
  2114. # use function tmp_space
  2115. #echo TODO: reimplent tmp space check
  2116.  
  2117.  
  2118. # test - GPG SANITY #####################################################################
  2119. # if encryption is disabled, skip this whole section
  2120. if gpg_disabled; then
  2121. echo -e "Test - En/Decryption skipped. (GPG disabled)"
  2122. elif [ "$GPG_TEST" = "disabled" ]; then
  2123. echo -e "Test - En/Decryption skipped. (Testing disabled)"
  2124. else
  2125.  
  2126. GPG_TEST="$TEMP_DIR/${ME_NAME}.$$.$(date_fix %s)"
  2127. function cleanup_gpgtest {
  2128. echo -en "Cleanup - Delete '${GPG_TEST}_*'"
  2129. rm "${GPG_TEST}"_* 2>/dev/null && echo "(OK)" || echo "(FAILED)"
  2130. }
  2131.  
  2132. # signing enabled?
  2133. if gpg_signing; then
  2134. CMD_PARAM_SIGN="--sign --default-key $(qw ${GPG_KEY_SIGN})"
  2135. CMD_MSG_SIGN="Sign with '${GPG_KEY_SIGN}'"
  2136. fi
  2137.  
  2138. # using keys
  2139. if [ ${#GPG_KEYS_ENC_ARRAY[@]} -gt 0 ]; then
  2140.  
  2141. for KEY_ID in "${GPG_KEYS_ENC_ARRAY[@]}"; do
  2142. CMD_PARAMS="$CMD_PARAMS -r $(qw ${KEY_ID})"
  2143. done
  2144. # check encrypting
  2145. CMD_MSG="Test - Encrypt to '$(join "','" "${GPG_KEYS_ENC_ARRAY[@]}")'${CMD_MSG_SIGN:+ & $CMD_MSG_SIGN}"
  2146. run_cmd $(gpg_pass_pipein GPG_PW_SIGN GPG_PW) gpg $CMD_PARAM_SIGN $(gpg_param_passwd GPG_PW_SIGN GPG_PW) $CMD_PARAMS $GPG_USEAGENT --status-fd 1 $GPG_OPTS -o $(qw "${GPG_TEST}_ENC") -e $(qw "$ME_LONG")
  2147. CMD_ERR=$?
  2148.  
  2149. if [ "$CMD_ERR" != "0" ]; then
  2150. KEY_NOTRUST=$(echo "$CMD_OUT"|awk '/^\[GNUPG:\] INV_RECP 10/ { print $4 }')
  2151. [ -n "$KEY_NOTRUST" ] && HINT="Key '${KEY_NOTRUST}' seems to be untrusted. If you really trust this key try to
  2152. 'gpg --edit-key "$KEY_NOTRUST"' and raise the trust level to ultimate. If you
  2153. can trust all of your keys set GPG_OPTS='--trust-model always' in conf file."
  2154. error_gpg_test "Encryption failed (Code $CMD_ERR).${CMD_OUT:+\n$CMD_OUT}" "$HINT"
  2155. fi
  2156.  
  2157. # check decrypting
  2158. CMD_MSG="Test - Decrypt"
  2159. gpg_key_decryptable || CMD_DISABLED="No matching secret key available."
  2160. run_cmd $(gpg_pass_pipein GPG_PW) gpg $(gpg_param_passwd GPG_PW) $GPG_OPTS -o $(qw "${GPG_TEST}_DEC") $GPG_USEAGENT -d $(qw "${GPG_TEST}_ENC")
  2161. CMD_ERR=$?
  2162.  
  2163. if [ "$CMD_ERR" != "0" ]; then
  2164. error_gpg_test "Decryption failed.${CMD_OUT:+\n$CMD_OUT}"
  2165. fi
  2166.  
  2167. # symmetric only
  2168. else
  2169. # check encrypting
  2170. CMD_MSG="Test - Encryption with passphrase${CMD_MSG_SIGN:+ & $CMD_MSG_SIGN}"
  2171. run_cmd $(gpg_pass_pipein GPG_PW) gpg $GPG_OPTS $CMD_PARAM_SIGN --passphrase-fd 0 -o $(qw "${GPG_TEST}_ENC") --batch -c $(qw "$ME_LONG")
  2172. CMD_ERR=$?
  2173. if [ "$CMD_ERR" != "0" ]; then
  2174. error_gpg_test "Encryption failed.${CMD_OUT:+\n$CMD_OUT}"
  2175. fi
  2176.  
  2177. # check decrypting
  2178. CMD_MSG="Test - Decryption with passphrase"
  2179. run_cmd $(gpg_pass_pipein GPG_PW) gpg $GPG_OPTS --passphrase-fd 0 -o $(qw "${GPG_TEST}_DEC") --batch -d $(qw "${GPG_TEST}_ENC")
  2180. CMD_ERR=$?
  2181. if [ "$CMD_ERR" != "0" ]; then
  2182. error_gpg_test "Decryption failed.${CMD_OUT:+\n$CMD_OUT}"
  2183. fi
  2184. fi
  2185.  
  2186. # compare original w/ decryptginal
  2187. CMD_MSG="Test - Compare"
  2188. [ -r "${GPG_TEST}_DEC" ] || CMD_DISABLED="File not found. Nothing to compare."
  2189. run_cmd "test \"\$(cat '$ME_LONG')\" = \"\$(cat '${GPG_TEST}_DEC')\""
  2190. CMD_ERR=$?
  2191. if [ "$CMD_ERR" = "0" ]; then
  2192. cleanup_gpgtest
  2193. else
  2194. error_gpg_test "Comparision failed.${CMD_OUT:+\n$CMD_OUT}"
  2195. fi
  2196.  
  2197. fi # end disabled
  2198.  
  2199. ## an empty line
  2200. #echo
  2201.  
  2202. # Exclude file is needed, create it if necessary
  2203. [ -f "$EXCLUDE" ] || touch "$EXCLUDE"
  2204.  
  2205. # export only used keys, if bkp not already exists ######################################
  2206. gpg_export_if_needed "${GPG_KEYS_ENC_ARRAY[@]}" "$(gpg_signing && echo $GPG_KEY_SIGN)"
  2207.  
  2208.  
  2209. # command execution #####################################################################
  2210.  
  2211. # urldecode url vars into plain text
  2212. var_isset 'TARGET_URL_USER' && TARGET_URL_USER="$(url_decode "$TARGET_URL_USER")"
  2213. var_isset 'TARGET_URL_PASS' && TARGET_URL_PASS="$(url_decode "$TARGET_URL_PASS")"
  2214.  
  2215. # defined TARGET_USER&PASS vars replace their URL pendants
  2216. # (double defs already dealt with)
  2217. var_isset 'TARGET_USER' && TARGET_URL_USER="$TARGET_USER"
  2218. var_isset 'TARGET_PASS' && TARGET_URL_PASS="$TARGET_PASS"
  2219.  
  2220. TARGET_URL_PROT_lowercase="$(tolower "${TARGET_URL_PROT%%:*}")"
  2221.  
  2222. # issue some warnings
  2223. case "$TARGET_URL_PROT_lowercase" in
  2224. 'cf+http')
  2225. # info on missing AUTH_URL
  2226. if ! var_isset 'CLOUDFILES_AUTHURL'; then
  2227. inform "No CLOUDFILES_AUTHURL exported (in conf).
  2228. Will use default which is probably rackspace."
  2229. fi
  2230.  ;;
  2231. 'swift')
  2232. # info on possibly missing AUTH_URL
  2233. var_isset 'SWIFT_AUTHURL' ||\
  2234. warning "\
  2235. Swift will probably fail because the conf var SWIFT_AUTHURL was not exported!"
  2236.  ;;
  2237. 'rsync')
  2238. # everything in url (this backend does not support pass in env var)
  2239. # this is obsolete from version 0.6.10 (buggy), hopefully fixed in 0.6.11
  2240. # print warning older version is detected
  2241. duplicity_version_lt 610 &&
  2242. warning "\
  2243. Duplicity version '$DUPL_VERSION' does not support providing the password as
  2244. env var for rsync backend. For security reasons you should consider to
  2245. update to a version greater than '0.6.10' of duplicity."
  2246.  ;;
  2247. esac
  2248.  
  2249.  
  2250. # for all protocols we put username in url and pass into env var
  2251. # for sec�rity reasons, we url_encode username to protect special chars
  2252. # first sortout backends with special ways to handle password
  2253. case "$TARGET_URL_PROT_lowercase" in
  2254. 'imap'|'imaps')
  2255. var_isset 'TARGET_URL_PASS' && BACKEND_PARAMS="IMAP_PASSWORD=$(qw "${TARGET_URL_PASS}")"
  2256.  ;;
  2257. *)
  2258. # rest uses FTP_PASS var
  2259. var_isset 'TARGET_URL_PASS' && \
  2260. BACKEND_PARAMS="FTP_PASSWORD=$(qw "${TARGET_URL_PASS}")"
  2261.  ;;
  2262. esac
  2263. # insert url encoded username into target url if needed
  2264. if var_isset 'TARGET_URL_USER' && [ "$TARGET_URL_PROT_lowercase" != "file" ]; then
  2265. BACKEND_URL="${TARGET_URL_PROT}$(url_encode "${TARGET_URL_USER}")@${TARGET_URL_HOSTPATH}"
  2266. else
  2267. BACKEND_URL="$TARGET"
  2268. fi
  2269.  
  2270.  
  2271. # protect eval from special chars in url (e.g. open ')' in password,
  2272. # spaces in path, quotes) happens above in duplify() via quotewrap()
  2273. SOURCE="$SOURCE"
  2274. BACKEND_URL="$BACKEND_URL"
  2275. EXCLUDE="$EXCLUDE"
  2276. # since 0.7.03 --exclude-globbing-filelist is deprecated
  2277. EXCLUDE_PARAM="--exclude$(duplicity_version_lt 703 && echo -globbing)-filelist"
  2278.  
  2279. # replace magic separators to condition command equivalents (+=and,-=or)
  2280. cmds=$(awk -v cmds="$cmds" "BEGIN{ gsub(/\+/,\"_and_\",cmds); gsub(/\-/,\"_or_\",cmds); print cmds}")
  2281. # convert cmds to array, lowercase for safety
  2282. CMDS=( $(awk "BEGIN{ cmds=tolower(\"$cmds\"); gsub(/_/,\" \",cmds); print cmds }") )
  2283.  
  2284. unset FTPL_ERR
  2285.  
  2286. # run cmds
  2287. for cmd in ${CMDS[*]};
  2288. do
  2289.  
  2290. ## init
  2291. # raise index in cmd array for pre/post param
  2292. var_isset 'CMD_NO' && CMD_NO=$((++CMD_NO)) || CMD_NO=0
  2293.  
  2294. # deal with condition "commands"
  2295. unset SKIP_NOW
  2296. if var_isset 'CMD_SKIP' && [ $CMD_SKIP -gt 0 ]; then
  2297. echo -e "\n--- Skipping command $(toupper $cmd) ! ---"
  2298. CMD_SKIP=$(($CMD_SKIP - 1))
  2299. SKIP_NOW="yes"
  2300. elif [ "$cmd" == 'and' ] && [ "$CMD_ERR" -ne "0" ]; then
  2301. CMD_SKIP=1
  2302. SKIP_NOW="yes"
  2303. elif [ "$cmd" == 'or' ] && [ "$CMD_ERR" -eq "0" ]; then
  2304. CMD_SKIP=1
  2305. SKIP_NOW="yes"
  2306. elif [ "$cmd" == 'and' ] || [ "$cmd" == 'or' ]; then
  2307. unset 'CMD_SKIP';
  2308. SKIP_NOW="yes"
  2309. fi
  2310.  
  2311. # sum up how many commands we skip and actually skip
  2312. if [ -n "$SKIP_NOW" ]; then
  2313. CMD_SKIPPED=$((${CMD_SKIPPED-0} + 1))
  2314. continue
  2315. fi
  2316.  
  2317. unset CMD_VALUE CMD_NEXT CMD_PREV CND_NEXT CND_PREV
  2318.  
  2319. # get next cmd,cnd vars
  2320. nextno=$(( $CMD_NO + 1 ))
  2321. while ! var_isset 'CMD_NEXT'
  2322. do
  2323. if [ "$nextno" -lt "${#CMDS[@]}" ]; then
  2324. CMD_VALUE=${CMDS[$nextno]}
  2325. is_condition "$CMD_VALUE" && CND_NEXT="$CMD_VALUE" || CMD_NEXT="$CMD_VALUE"
  2326. else
  2327. CMD_NEXT='END'
  2328. fi
  2329. nextno=$(($nextno+1))
  2330. done
  2331.  
  2332. # get prev cnd, cnd are skipped pseudocmds
  2333. prevno=$(( $CMD_NO - 1 ))
  2334. [ "$prevno" -ge 0 ] && is_condition "${CMDS[$prevno]}" && CND_PREV=${CMDS[$prevno]}
  2335.  
  2336. # get prev cmd command minus skipped commands, only executed
  2337. prevno=$(( $CMD_NO - ${CMD_SKIPPED-0} - 1 )); unset CMD_SKIPPED
  2338. while ! var_isset 'CMD_PREV'
  2339. do
  2340. if [ "$prevno" -ge 0 ]; then
  2341. CMD_VALUE=${CMDS[$prevno]}
  2342. is_condition "$CMD_VALUE" || CMD_PREV="$CMD_VALUE"
  2343. else
  2344. CMD_PREV='START'
  2345. fi
  2346. prevno=$(($prevno-1))
  2347. done
  2348.  
  2349. # save start time
  2350. RUN_START=$(nsecs)
  2351.  
  2352. # export some useful env vars for external scripts/programs to use
  2353. export PROFILE CONFDIR SOURCE TARGET_URL_PROT TARGET_URL_HOSTPATH \
  2354. TARGET_URL_USER TARGET_URL_PASS \
  2355. GPG_KEYS_ENC=$(join "\n" "${GPG_KEYS_ENC_ARRAY[@]}") GPG_KEY_SIGN \
  2356. GPG_PW CMD_PREV CMD_NEXT CMD_ERR CND_PREV CND_NEXT\
  2357. RUN_START
  2358.  
  2359. # user info
  2360. echo; separator "Start running command $(toupper $cmd) at $(date_from_nsecs $RUN_START)"
  2361.  
  2362. case "$(tolower $cmd)" in
  2363. 'pre'|'post')
  2364. if [ "$cmd" == 'pre' ]; then
  2365. script=$PRE
  2366. else
  2367. script=$POST
  2368. fi
  2369. # script execution in a subshell, protect us from failures/var overwrites
  2370. ( run_script "$script" )
  2371.  ;;
  2372. 'bkp')
  2373. duplify -- "${dupl_opts[@]}" $EXCLUDE_PARAM "$EXCLUDE" \
  2374. "$SOURCE" "$BACKEND_URL"
  2375.  ;;
  2376. 'incr')
  2377. duplify incr -- "${dupl_opts[@]}" $EXCLUDE_PARAM "$EXCLUDE" \
  2378. "$SOURCE" "$BACKEND_URL"
  2379.  ;;
  2380. 'full')
  2381. duplify full -- "${dupl_opts[@]}" $EXCLUDE_PARAM "$EXCLUDE" \
  2382. "$SOURCE" "$BACKEND_URL"
  2383.  ;;
  2384. 'verify')
  2385. TIME="${ftpl_pars[0]:+"-t ${ftpl_pars[0]}"}"
  2386. duplify verify -- $TIME "${dupl_opts[@]}" $EXCLUDE_PARAM "$EXCLUDE" \
  2387. "$BACKEND_URL" "$SOURCE"
  2388.  ;;
  2389. 'verifypath')
  2390. TIME="${ftpl_pars[2]:+"-t ${ftpl_pars[2]}"}"
  2391. IN_PATH="${ftpl_pars[0]}"; OUT_PATH="${ftpl_pars[1]}";
  2392. ( [ -z "$IN_PATH" ] || [ -z "$OUT_PATH" ] ) && error " Missing parameter <rel_bkp_path> or <local_path> for verifyPath.
  2393.  
  2394. Hint:
  2395. Syntax is -> $ME <profile> verifyPath <rel_bkp_path> <local_path> [<age>]"
  2396.  
  2397. duplify verify -- $TIME "${dupl_opts[@]}" $EXCLUDE_PARAM "$EXCLUDE" \
  2398. --file-to-restore "$IN_PATH" "$BACKEND_URL" "$OUT_PATH"
  2399.  ;;
  2400. 'list')
  2401. # time param exists since 0.5.10+
  2402. TIME="${ftpl_pars[0]:+"-t ${ftpl_pars[0]}"}"
  2403. duplify list-current-files -- $TIME "${dupl_opts[@]}" "$BACKEND_URL"
  2404.  ;;
  2405. 'cleanup')
  2406. duplify cleanup -- "${dupl_opts[@]}" "$BACKEND_URL"
  2407.  ;;
  2408. 'purge')
  2409. MAX_AGE=${ftpl_pars[0]:-$MAX_AGE}
  2410. [ -z "$MAX_AGE" ] && error " Missing parameter <max_age>. Can be set in profile or as command line parameter."
  2411.  
  2412. duplify remove-older-than "${MAX_AGE}" \
  2413. -- "${dupl_opts[@]}" "$BACKEND_URL"
  2414.  ;;
  2415. 'purgefull')
  2416. MAX_FULL_BACKUPS=${ftpl_pars[0]:-$MAX_FULL_BACKUPS}
  2417. [ -z "$MAX_FULL_BACKUPS" ] && error " Missing parameter <max_full_backups>. Can be set in profile or as command line parameter."
  2418.  
  2419. duplify remove-all-but-n-full "${MAX_FULL_BACKUPS}" \
  2420. -- "${dupl_opts[@]}" "$BACKEND_URL"
  2421.  ;;
  2422. 'purgeincr')
  2423. MAX_FULLS_WITH_INCRS=${ftpl_pars[0]:-$MAX_FULLS_WITH_INCRS}
  2424. [ -z "$MAX_FULLS_WITH_INCRS" ] && error " Missing parameter <max_fulls_with_incrs>. Can be set in profile or as command line parameter."
  2425.  
  2426. duplify remove-all-inc-of-but-n-full "${MAX_FULLS_WITH_INCRS}" \
  2427. -- "${dupl_opts[@]}" "$BACKEND_URL"
  2428.  ;;
  2429. 'restore')
  2430. OUT_PATH="${ftpl_pars[0]}"; TIME="${ftpl_pars[1]:-now}";
  2431. [ -z "$OUT_PATH" ] && error " Missing parameter target_path for restore.
  2432.  
  2433. Hint:
  2434. Syntax is -> $ME <profile> restore <target_path> [<age>]"
  2435.  
  2436. duplify -- -t "$TIME" "${dupl_opts[@]}" "$BACKEND_URL" "$OUT_PATH"
  2437.  ;;
  2438. 'fetch')
  2439. IN_PATH="${ftpl_pars[0]}"; OUT_PATH="${ftpl_pars[1]}";
  2440. TIME="${ftpl_pars[2]:-now}";
  2441. ( [ -z "$IN_PATH" ] || [ -z "$OUT_PATH" ] ) && error " Missing parameter <src_path> or <target_path> for fetch.
  2442.  
  2443. Hint:
  2444. Syntax is -> $ME <profile> fetch <src_path> <target_path> [<age>]"
  2445.  
  2446. # duplicity 0.4.7 doesnt like cmd restore in combination with --file-to-restore
  2447. duplify -- --restore-time "$TIME" "${dupl_opts[@]}" \
  2448. --file-to-restore "$IN_PATH" "$BACKEND_URL" "$OUT_PATH"
  2449.  ;;
  2450. 'status')
  2451. duplify collection-status -- "${dupl_opts[@]}" "$BACKEND_URL"
  2452.  ;;
  2453. *)
  2454. error " Unknown command '$cmd'.
  2455.  
  2456. Hint:
  2457. Use '$ME usage' to get usage help."
  2458.  ;;
  2459. esac
  2460.  
  2461. CMD_ERR=$?
  2462. RUN_END=$(nsecs)
  2463. RUNTIME=$(( $RUN_END - $RUN_START ))
  2464.  
  2465. # print message on error; set error code
  2466. if [ "$CMD_ERR" -ne 0 ]; then
  2467. error_print "$(datefull_from_nsecs $RUN_END) Task '$(echo $cmd|awk '$0=toupper($0)')' failed with exit code '$CMD_ERR'."
  2468. FTPL_ERR=1
  2469. fi
  2470.  
  2471. separator "Finished state $(error_to_string $CMD_ERR) at $(date_from_nsecs $RUN_END) - \
  2472. Runtime $(printf "%02d:%02d:%02d.%03d" $((RUNTIME/1000000000/60/60)) $((RUNTIME/1000000000/60%60)) $((RUNTIME/1000000000%60)) $((RUNTIME/1000000%1000)) )"
  2473.  
  2474. done
  2475.  
  2476. exit ${FTPL_ERR}
  2477.