syncthing-versioning.7 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. .\" Man page generated from reStructuredText.
  2. .
  3. .
  4. .nr rst2man-indent-level 0
  5. .
  6. .de1 rstReportMargin
  7. \\$1 \\n[an-margin]
  8. level \\n[rst2man-indent-level]
  9. level margin: \\n[rst2man-indent\\n[rst2man-indent-level]]
  10. -
  11. \\n[rst2man-indent0]
  12. \\n[rst2man-indent1]
  13. \\n[rst2man-indent2]
  14. ..
  15. .de1 INDENT
  16. .\" .rstReportMargin pre:
  17. . RS \\$1
  18. . nr rst2man-indent\\n[rst2man-indent-level] \\n[an-margin]
  19. . nr rst2man-indent-level +1
  20. .\" .rstReportMargin post:
  21. ..
  22. .de UNINDENT
  23. . RE
  24. .\" indent \\n[an-margin]
  25. .\" old: \\n[rst2man-indent\\n[rst2man-indent-level]]
  26. .nr rst2man-indent-level -1
  27. .\" new: \\n[rst2man-indent\\n[rst2man-indent-level]]
  28. .in \\n[rst2man-indent\\n[rst2man-indent-level]]u
  29. ..
  30. .TH "SYNCTHING-VERSIONING" "7" "Sep 17, 2023" "v1.24.0" "Syncthing"
  31. .SH NAME
  32. syncthing-versioning \- Keep automatic backups of deleted files by other nodes
  33. .sp
  34. Syncthing supports archiving the old version of a file when it is deleted or
  35. replaced with a newer version from the cluster. This is called “file
  36. versioning” and uses one of the available \fIversioning strategies\fP described
  37. below. File versioning is configured per folder, on a per\-device basis, and
  38. defaults to “no file versioning”, i.e. no old copies of files are kept.
  39. .sp
  40. \fBNOTE:\fP
  41. .INDENT 0.0
  42. .INDENT 3.5
  43. Versioning applies to changes received \fIfrom other devices\fP\&. That is, if
  44. Alice has versioning turned on and Bob changes a file, the old version
  45. will be archived on Alice’s computer when that change is synced from
  46. Bob. If Alice changes a file locally on her own computer Syncthing will
  47. not and can not archive the old version.
  48. .UNINDENT
  49. .UNINDENT
  50. .sp
  51. The applicable configuration options for each versioning strategy are described
  52. below. For most of them it’s possible to specify where the versions are stored,
  53. with the default being the \fB\&.stversions\fP folder inside the shared folder path.
  54. If you set a custom version path, please ensure that it’s on the same partition
  55. or filesystem as the regular folder path, as moving files there may otherwise
  56. fail.
  57. .SH TRASH CAN FILE VERSIONING
  58. .sp
  59. This versioning strategy emulates the common “trash can” approach. When a file
  60. is deleted or replaced due to a change on a remote device, it is moved to
  61. the trash can in the \fB\&.stversions\fP folder. If a file with the same name was
  62. already in the trash can it is replaced.
  63. .sp
  64. A \fI\%configuration option\fP is
  65. available to clean the trash can from files older than a specified number of
  66. days. If this is set to a positive number of days, files will be removed when
  67. they have been in the trash can that long. Setting this to zero prevents any
  68. files from being removed from the trash can automatically.
  69. .SH SIMPLE FILE VERSIONING
  70. .sp
  71. With “Simple File Versioning” files are moved to the \fB\&.stversions\fP folder when
  72. replaced or deleted on a remote device. In addition to the
  73. \fI\%cleanoutDays\fP option, this strategy also takes a
  74. value in an input titled “Keep Versions” which tells Syncthing how many old
  75. versions of the file it should \fI\%keep\fP\&. For
  76. example, if you set this value to 5, if a file is replaced 5 times on a remote
  77. device, you will see 5 time\-stamped versions on that file in the \fB\&.stversions\fP
  78. folder on the other devices sharing the same folder.
  79. .SH STAGGERED FILE VERSIONING
  80. .sp
  81. With “Staggered File Versioning” files are also moved to the \fB\&.stversions\fP
  82. folder when replaced or deleted on a remote device (just like “Simple File
  83. Versioning”), however, versions are automatically deleted if they are older than
  84. the maximum age or exceed the number of files allowed in an interval.
  85. .sp
  86. The following intervals are used and they each have a maximum number of files
  87. that will be kept for each.
  88. .INDENT 0.0
  89. .TP
  90. .B 1 Hour
  91. For the first hour, the oldest version in every 30\-seconds interval is
  92. kept.
  93. .TP
  94. .B 1 Day
  95. For the first day, the oldest version in every hour is kept.
  96. .TP
  97. .B 30 Days
  98. For the first 30 days, the oldest version in every day is kept.
  99. .TP
  100. .B Until Maximum Age
  101. Until maximum age, the oldest version in every week is kept.
  102. .TP
  103. .B Maximum Age
  104. The maximum time to keep a version in days. For example, to keep replaced or
  105. deleted files in the \fB\&.stversions\fP folder for an entire year, use 365. If
  106. only for 10 days, use 10. Corresponds to the
  107. \fI\%maxAge\fP option.
  108. \fBNote: Set to 0 to keep versions forever.\fP
  109. .UNINDENT
  110. .sp
  111. This means that there is only one version in each interval and as files age they
  112. will be deleted unless when the interval they are entering is empty. By keeping
  113. the oldest versions this versioning scheme preserves the file if it is
  114. overwritten.
  115. .sp
  116. For more info, check the \fI\%unit test file\fP <\fBhttps://github.com/syncthing/syncthing/blob/main/lib/versioner/staggered_test.go#L32\fP>
  117. that shows which versions are deleted for a specific run.
  118. .SH EXTERNAL FILE VERSIONING
  119. .sp
  120. This versioning strategy delegates the decision on what to do to an
  121. \fI\%external command\fP (e.g. a program or a
  122. command line script). Just prior to a file being replaced, the command will be
  123. executed. The file needs to be removed from the folder in the process, or
  124. otherwise Syncthing will report an error. The command can use the following
  125. templated arguments:
  126. .INDENT 0.0
  127. .TP
  128. .B %FOLDER_PATH%
  129. Path to the folder
  130. .TP
  131. .B %FILE_PATH%
  132. Path to the file within the folder
  133. .UNINDENT
  134. .sp
  135. Note that the former expands to the path of the actual Syncthing folder,
  136. and the latter to the path inside that folder. For instance, if you use
  137. the default \fBSync\fP folder in Windows, and the full path to the file is
  138. \fBC:\eUsers\eUser\eSync\eFamily photos\eIMG_2021\-03\-01.jpg\fP, then the
  139. \fB%FOLDER_PATH%\fP will be \fBC:\eUsers\eUser\eSync\fP, and the
  140. \fB%FILE_PATH%\fP will be \fBFamily photos\eIMG_2021\-03\-01.jpg\fP\&.
  141. .SS Example for Unixes
  142. .sp
  143. Let’s say I want to keep the latest version of each file as they are replaced
  144. or removed; essentially I want a “trash can”\-like behavior. For this, I create
  145. the following script and store it as \fB/Users/jb/bin/onlylatest.sh\fP (i.e. the
  146. \fBbin\fP directory in my home directory):
  147. .INDENT 0.0
  148. .INDENT 3.5
  149. .sp
  150. .nf
  151. .ft C
  152. #!/bin/sh
  153. set \-eu
  154. # Where I want my versions stored
  155. versionspath=~/.trashcan
  156. # The parameters we get from Syncthing
  157. folderpath=\(dq$1\(dq
  158. filepath=\(dq$2\(dq
  159. # First ensure the dir where we need to store the file exists
  160. outpath=$(dirname \(dq$versionspath/$filepath\(dq)
  161. mkdir \-p \(dq$outpath\(dq
  162. # Then move the file there
  163. mv \-f \(dq$folderpath/$filepath\(dq \(dq$versionspath/$filepath\(dq
  164. .ft P
  165. .fi
  166. .UNINDENT
  167. .UNINDENT
  168. .sp
  169. I must ensure that the script has execute permissions (\fBchmod 755
  170. onlylatest.sh\fP), then configure Syncthing with command \fB/Users/jb/bin/onlylatest.sh %FOLDER_PATH% %FILE_PATH%\fP
  171. .sp
  172. Let’s assume I have a folder “default” in ~/Sync, and that within that folder
  173. there is a file \fBdocs/letter.txt\fP that is being replaced or deleted. The
  174. script will be called as if I ran this from the command line:
  175. .INDENT 0.0
  176. .INDENT 3.5
  177. .sp
  178. .nf
  179. .ft C
  180. $ /Users/jb/bin/onlylatest.sh /Users/jb/Sync docs/letter.txt
  181. .ft P
  182. .fi
  183. .UNINDENT
  184. .UNINDENT
  185. .sp
  186. The script will then move the file in question to
  187. \fB~/.trashcan/docs/letter.txt\fP, replacing any previous version of that letter
  188. that may already have been there.
  189. .SS Examples for Windows
  190. .SS Move to a given folder using the command prompt (CMD)
  191. .sp
  192. On Windows we can use a batch script to perform the same “trash can”\-like
  193. behavior as mentioned above. I created the following script and saved it as
  194. \fBC:\eUsers\emfrnd\eScripts\eonlylatest.bat\fP\&.
  195. .INDENT 0.0
  196. .INDENT 3.5
  197. .sp
  198. .nf
  199. .ft C
  200. @echo off
  201. rem Enable UTF\-8 encoding to deal with multilingual folder and file names
  202. chcp 65001
  203. rem We need command extensions for md to create intermediate folders in one go
  204. setlocal enableextensions
  205. rem Where I want my versions stored
  206. set \(dqversions_path=%USERPROFILE%\e.trashcan\(dq
  207. rem The parameters we get from Syncthing, \(aq~\(aq removes quotes if any
  208. set \(dqfolder_path=%~1\(dq
  209. set \(dqfile_path=%~2\(dq
  210. rem First ensure the dir where we need to store the file exists
  211. for %%f in (\(dq%versions_path%\e%file_path%\(dq) do set \(dqoutput_path=%%~dpf\(dq
  212. if not exist \(dq%output_path%\(dq md \(dq%output_path%\(dq || exit /b
  213. rem Finally move the file, overwrite existing file if any
  214. move /y \(dq%folder_path%\e%file_path%\(dq \(dq%versions_path%\e%file_path%\(dq
  215. .ft P
  216. .fi
  217. .UNINDENT
  218. .UNINDENT
  219. .sp
  220. Finally, I set \fB\(dqC:\eUsers\emfrnd\eScripts\eonlylatest.bat\(dq \(dq%FOLDER_PATH%\(dq
  221. \(dq%FILE_PATH%\(dq\fP as the command name in Syncthing.
  222. .SS Move to the Recycle Bin using PowerShell
  223. .sp
  224. We can use PowerShell to send files directly to the Recycle Bin, which
  225. mimics the behaviour of deleting them using the Windows Explorer.
  226. Firstly, create the following script and save it in your preferred
  227. location, e.g. \fBC:\eUsers\eUser\eScripts\eSendToRecycleBin.ps1\fP\&.
  228. .INDENT 0.0
  229. .INDENT 3.5
  230. .sp
  231. .nf
  232. .ft C
  233. # PowerShell has no native method to recycle files, so we use Visual
  234. # Basic to perform the operation. If succeeded, we also include the
  235. # recycled file in the Syncthing\(aqs DEBUG output.
  236. Add\-Type \-AssemblyName Microsoft.VisualBasic
  237. [Microsoft.VisualBasic.FileIO.FileSystem]::DeleteFile($args,\(aqOnlyErrorDialogs\(aq,\(aqSendToRecycleBin\(aq)
  238. if ($?) {
  239. Write\-Output (\(dqRecycled \(dq + $args + \(dq.\(dq)
  240. }
  241. .ft P
  242. .fi
  243. .UNINDENT
  244. .UNINDENT
  245. .sp
  246. Alternatively, the script can be expanded to send only deleted files to
  247. the Recycle Bin, and permanently delete modified ones, which makes it
  248. more consistent with how the Explorer works.
  249. .INDENT 0.0
  250. .INDENT 3.5
  251. .sp
  252. .nf
  253. .ft C
  254. # PowerShell has no native method to recycle files, so we use Visual
  255. # Basic to perform the operation.
  256. Add\-Type \-AssemblyName Microsoft.VisualBasic
  257. # We need to test if a Syncthing .tmp file exists. If it does, we assume
  258. # a modification and delete the existing file. If if does not, we assume
  259. # a deletion and recycle the current file. If succeeded, we also include
  260. # the deleted/recycled file in the Syncthing\(aqs DEBUG output.
  261. if (Test\-Path \-LiteralPath ((Split\-Path \-Path $args) + \(dq\e~syncthing~\(dq + (Split\-Path \-Path $args \-Leaf) + \(dq.tmp\(dq)) {
  262. [Microsoft.VisualBasic.FileIO.FileSystem]::DeleteFile($args,\(aqOnlyErrorDialogs\(aq,\(aqDeletePermanently\(aq)
  263. if ($?) {
  264. Write\-Output (\(dqDeleted \(dq + $args + \(dq.\(dq)
  265. }
  266. } else {
  267. [Microsoft.VisualBasic.FileIO.FileSystem]::DeleteFile($args,\(aqOnlyErrorDialogs\(aq,\(aqSendToRecycleBin\(aq)
  268. if ($?) {
  269. Write\-Output (\(dqRecycled \(dq + $args + \(dq.\(dq)
  270. }
  271. }
  272. .ft P
  273. .fi
  274. .UNINDENT
  275. .UNINDENT
  276. .sp
  277. Finally, we set the command name in Syncthing to \fBpowershell.exe
  278. \-ExecutionPolicy Bypass \-File \(dqC:\eUsers\eUser\eScripts\eSendToRecycleBin.ps1\(dq
  279. \(dq%FOLDER_PATH%\e%FILE_PATH%\(dq\fP\&.
  280. .sp
  281. The only caveat that you should be aware of is that if your Syncthing
  282. folder is located on a portable storage, such as a USB stick, or if you
  283. have the Recycle Bin disabled, then the script will end up deleting all
  284. files permanently.
  285. .SH CONFIGURATION PARAMETER REFERENCE
  286. .sp
  287. The versioning settings are grouped into their own section of each folder in the
  288. \fI\%configuration file\fP\&. The following shows an
  289. example of such a section in the XML:
  290. .INDENT 0.0
  291. .INDENT 3.5
  292. .sp
  293. .nf
  294. .ft C
  295. <folder id=\(dq...\(dq>
  296. <versioning type=\(dqsimple\(dq>
  297. <cleanupIntervalS>3600</cleanupIntervalS>
  298. <fsPath></fsPath>
  299. <fsType>basic</fsType>
  300. <param key=\(dqcleanoutDays\(dq val=\(dq0\(dq></param>
  301. <param key=\(dqkeep\(dq val=\(dq5\(dq></param>
  302. </versioning>
  303. </folder>
  304. .ft P
  305. .fi
  306. .UNINDENT
  307. .UNINDENT
  308. .INDENT 0.0
  309. .TP
  310. .B versioning.type
  311. Selects one of the versioning strategies: \fBtrashcan\fP, \fBsimple\fP,
  312. \fBstaggered\fP, \fBexternal\fP or leave empty to disable versioning completely.
  313. .UNINDENT
  314. .INDENT 0.0
  315. .TP
  316. .B versioning.fsPath
  317. Overrides the path where old versions of files are stored and defaults to
  318. \fB\&.stversions\fP if left empty. An absolute or relative path can be
  319. specified. The latter is interpreted relative to the shared folder path, if
  320. the \fI\%fsType\fP is configured as \fBbasic\fP\&. Ignored
  321. for the \fBexternal\fP versioning strategy.
  322. .sp
  323. This option used to be stored under the keys \fBfsPath\fP or \fBversionsPath\fP
  324. in the \fI\%params\fP element.
  325. .UNINDENT
  326. .INDENT 0.0
  327. .TP
  328. .B versioning.fsType
  329. The internal file system implementation used to access this versions folder.
  330. Only applies if \fI\%fsPath\fP is also set non\-empty,
  331. otherwise the \fI\%filesystemType\fP from the folder element is used
  332. instead. Refer to that option description for possible values. Ignored for
  333. the \fBexternal\fP versioning strategy.
  334. .sp
  335. This option used to be stored under the key \fBfsType\fP in the
  336. \fI\%params\fP element.
  337. .UNINDENT
  338. .INDENT 0.0
  339. .TP
  340. .B versioning.cleanupIntervalS
  341. The interval, in seconds, for running cleanup in the versions folder. Zero
  342. to disable periodic cleaning. Limited to one year (31536000 seconds).
  343. Ignored for the \fBexternal\fP versioning strategy.
  344. .sp
  345. This option used to be stored under the key \fBcleanInterval\fP in the
  346. \fI\%params\fP element.
  347. .UNINDENT
  348. .INDENT 0.0
  349. .TP
  350. .B versioning.params
  351. Each versioning strategy can have configuration parameters specific to its
  352. implementation under this element.
  353. .UNINDENT
  354. .INDENT 0.0
  355. .TP
  356. .B versioning.params.cleanoutDays
  357. The number of days to keep files in the versions folder. Zero means to keep
  358. forever. Older elements encountered during cleanup are removed.
  359. .UNINDENT
  360. .INDENT 0.0
  361. .TP
  362. .B versioning.params.keep
  363. The number of old versions to keep, per file.
  364. .UNINDENT
  365. .INDENT 0.0
  366. .TP
  367. .B versioning.params.maxAge
  368. The maximum time to keep a version, in seconds. Zero means to keep forever.
  369. .UNINDENT
  370. .INDENT 0.0
  371. .TP
  372. .B versioning.params.command
  373. External command to execute for storing a file version about to be replaced
  374. or deleted. If the path to the application contains spaces, it should be
  375. quoted.
  376. .UNINDENT
  377. .SH AUTHOR
  378. The Syncthing Authors
  379. .SH COPYRIGHT
  380. 2014-2019, The Syncthing Authors
  381. .\" Generated by docutils manpage writer.
  382. .