syncthing-versioning.7 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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 14, 2022" "v1.21.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. .SH TRASH CAN FILE VERSIONING
  51. .sp
  52. This versioning strategy emulates the common “trash can” approach. When a file
  53. is deleted or replaced due to a change on a remote device, it is moved to
  54. the trash can in the \fB\&.stversions\fP folder. If a file with the same name was
  55. already in the trash can it is replaced.
  56. .sp
  57. A configuration option is available to clean the trash can from files older
  58. than a specified number of days. If this is set to a positive number of days,
  59. files will be removed when they have been in the trash can that long. Setting
  60. this to zero prevents any files from being removed from the trash can
  61. automatically.
  62. .SH SIMPLE FILE VERSIONING
  63. .sp
  64. With “Simple File Versioning” files are moved to the \fB\&.stversions\fP folder
  65. (inside your shared folder) when replaced or deleted on a remote device. This
  66. option also takes a value in an input titled “Keep Versions” which tells
  67. Syncthing how many old versions of the file it should keep. For example, if
  68. you set this value to 5, if a file is replaced 5 times on a remote device, you
  69. will see 5 time\-stamped versions on that file in the “.stversions” folder on
  70. the other devices sharing the same folder.
  71. .SH STAGGERED FILE VERSIONING
  72. .sp
  73. With “Staggered File Versioning” files are also moved to a different folder
  74. when replaced or deleted on a remote device (just like “Simple File
  75. Versioning”), however, versions are automatically deleted if they are older
  76. than the maximum age or exceed the number of files allowed in an interval.
  77. .sp
  78. With this versioning method it’s possible to specify where the versions are
  79. stored, with the default being the \fB\&.stversions\fP folder inside the normal
  80. folder path. If you set a custom version path, please ensure that it’s on the
  81. same partition or filesystem as the regular folder path, as moving files there
  82. may otherwise fail. You can use an absolute path (this is recommended) or a
  83. relative path. Relative paths are interpreted relative to Syncthing’s current
  84. or startup directory.
  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 “.stversions” folder for an entire year, use 365. If
  106. only for 10 days, use 10.
  107. \fBNote: Set to 0 to keep versions forever.\fP
  108. .UNINDENT
  109. .sp
  110. This means that there is only one version in each interval and as files age they
  111. will be deleted unless when the interval they are entering is empty. By keeping
  112. the oldest versions this versioning scheme preserves the file if it is
  113. overwritten.
  114. .sp
  115. For more info, check the \fI\%unit test file\fP <\fBhttps://github.com/syncthing/syncthing/blob/main/lib/versioner/staggered_test.go#L32\fP>
  116. that shows which versions are deleted for a specific run.
  117. .SH EXTERNAL FILE VERSIONING
  118. .sp
  119. This versioning method delegates the decision on what to do to an
  120. external command (e.g. a program or a command line script). Just prior
  121. to a file being replaced, the command will be executed. The file needs
  122. to be removed from the folder in the process, or otherwise Syncthing
  123. will report an error. The command can use the following templated
  124. arguments:
  125. .INDENT 0.0
  126. .TP
  127. .B %FOLDER_PATH%
  128. Path to the folder
  129. .TP
  130. .B %FILE_PATH%
  131. Path to the file within the folder
  132. .UNINDENT
  133. .sp
  134. Note that the former expands to the path of the actual Syncthing folder,
  135. and the latter to the path inside that folder. For instance, if you use
  136. the default \fBSync\fP folder in Windows, and the full path to the file is
  137. \fBC:\eUsers\eUser\eSync\eFamily photos\eIMG_2021\-03\-01.jpg\fP, then the
  138. \fB%FOLDER_PATH%\fP will be \fBC:\eUsers\eUser\eSync\fP, and the
  139. \fB%FILE_PATH%\fP will be \fBFamily photos\eIMG_2021\-03\-01.jpg\fP\&.
  140. .SS Example for Unixes
  141. .sp
  142. Let’s say I want to keep the latest version of each file as they are replaced
  143. or removed; essentially I want a “trash can”\-like behavior. For this, I create
  144. the following script and store it as \fB/Users/jb/bin/onlylatest.sh\fP (i.e. the
  145. \fBbin\fP directory in my home directory):
  146. .INDENT 0.0
  147. .INDENT 3.5
  148. .sp
  149. .nf
  150. .ft C
  151. #!/bin/sh
  152. set \-eu
  153. # Where I want my versions stored
  154. versionspath=~/.trashcan
  155. # The parameters we get from Syncthing
  156. folderpath="$1"
  157. filepath="$2"
  158. # First ensure the dir where we need to store the file exists
  159. outpath=$(dirname "$versionspath/$filepath")
  160. mkdir \-p "$outpath"
  161. # Then move the file there
  162. mv \-f "$folderpath/$filepath" "$versionspath/$filepath"
  163. .ft P
  164. .fi
  165. .UNINDENT
  166. .UNINDENT
  167. .sp
  168. I must ensure that the script has execute permissions (\fBchmod 755
  169. onlylatest.sh\fP), then configure Syncthing with command \fB/Users/jb/bin/onlylatest.sh %FOLDER_PATH% %FILE_PATH%\fP
  170. .sp
  171. Let’s assume I have a folder “default” in ~/Sync, and that within that folder
  172. there is a file \fBdocs/letter.txt\fP that is being replaced or deleted. The
  173. script will be called as if I ran this from the command line:
  174. .INDENT 0.0
  175. .INDENT 3.5
  176. .sp
  177. .nf
  178. .ft C
  179. $ /Users/jb/bin/onlylatest.sh /Users/jb/Sync docs/letter.txt
  180. .ft P
  181. .fi
  182. .UNINDENT
  183. .UNINDENT
  184. .sp
  185. The script will then move the file in question to
  186. \fB~/.trashcan/docs/letter.txt\fP, replacing any previous version of that letter
  187. that may already have been there.
  188. .SS Examples for Windows
  189. .SS Move to a given folder using the command prompt (CMD)
  190. .sp
  191. On Windows we can use a batch script to perform the same “trash can”\-like
  192. behavior as mentioned above. I created the following script and saved it as
  193. \fBC:\eUsers\emfrnd\eScripts\eonlylatest.bat\fP\&.
  194. .INDENT 0.0
  195. .INDENT 3.5
  196. .sp
  197. .nf
  198. .ft C
  199. @echo off
  200. rem Enable UTF\-8 encoding to deal with multilingual folder and file names
  201. chcp 65001
  202. rem We need command extensions for md to create intermediate folders in one go
  203. setlocal enableextensions
  204. rem Where I want my versions stored
  205. set "versions_path=%USERPROFILE%\e.trashcan"
  206. rem The parameters we get from Syncthing, \(aq~\(aq removes quotes if any
  207. set "folder_path=%~1"
  208. set "file_path=%~2"
  209. rem First ensure the dir where we need to store the file exists
  210. for %%f in ("%versions_path%\e%file_path%") do set "output_path=%%~dpf"
  211. if not exist "%output_path%" md "%output_path%" || exit /b
  212. rem Finally move the file, overwrite existing file if any
  213. move /y "%folder_path%\e%file_path%" "%versions_path%\e%file_path%"
  214. .ft P
  215. .fi
  216. .UNINDENT
  217. .UNINDENT
  218. .sp
  219. Finally, I set \fB"C:\eUsers\emfrnd\eScripts\eonlylatest.bat" "%FOLDER_PATH%"
  220. "%FILE_PATH%"\fP as the command name in Syncthing.
  221. .SS Move to the Recycle Bin using PowerShell
  222. .sp
  223. We can use PowerShell to send files directly to the Recycle Bin, which
  224. mimics the behaviour of deleting them using the Windows Explorer.
  225. Firstly, create the following script and save it in your preferred
  226. location, e.g. \fBC:\eUsers\eUser\eScripts\eSendToRecycleBin.ps1\fP\&.
  227. .INDENT 0.0
  228. .INDENT 3.5
  229. .sp
  230. .nf
  231. .ft C
  232. # PowerShell has no native method to recycle files, so we use Visual
  233. # Basic to perform the operation. If succeeded, we also include the
  234. # recycled file in the Syncthing\(aqs DEBUG output.
  235. Add\-Type \-AssemblyName Microsoft.VisualBasic
  236. [Microsoft.VisualBasic.FileIO.FileSystem]::DeleteFile($args,\(aqOnlyErrorDialogs\(aq,\(aqSendToRecycleBin\(aq)
  237. if ($?) {
  238. Write\-Output ("Recycled " + $args + ".")
  239. }
  240. .ft P
  241. .fi
  242. .UNINDENT
  243. .UNINDENT
  244. .sp
  245. Alternatively, the script can be expanded to send only deleted files to
  246. the Recycle Bin, and permanently delete modified ones, which makes it
  247. more consistent with how the Explorer works.
  248. .INDENT 0.0
  249. .INDENT 3.5
  250. .sp
  251. .nf
  252. .ft C
  253. # PowerShell has no native method to recycle files, so we use Visual
  254. # Basic to perform the operation.
  255. Add\-Type \-AssemblyName Microsoft.VisualBasic
  256. # We need to test if a Syncthing .tmp file exists. If it does, we assume
  257. # a modification and delete the existing file. If if does not, we assume
  258. # a deletion and recycle the current file. If succeeded, we also include
  259. # the deleted/recycled file in the Syncthing\(aqs DEBUG output.
  260. if (Test\-Path \-LiteralPath ((Split\-Path \-Path $args) + "\e~syncthing~" + (Split\-Path \-Path $args \-Leaf) + ".tmp")) {
  261. [Microsoft.VisualBasic.FileIO.FileSystem]::DeleteFile($args,\(aqOnlyErrorDialogs\(aq,\(aqDeletePermanently\(aq)
  262. if ($?) {
  263. Write\-Output ("Deleted " + $args + ".")
  264. }
  265. } else {
  266. [Microsoft.VisualBasic.FileIO.FileSystem]::DeleteFile($args,\(aqOnlyErrorDialogs\(aq,\(aqSendToRecycleBin\(aq)
  267. if ($?) {
  268. Write\-Output ("Recycled " + $args + ".")
  269. }
  270. }
  271. .ft P
  272. .fi
  273. .UNINDENT
  274. .UNINDENT
  275. .sp
  276. Finally, we set the command name in Syncthing to \fBpowershell.exe
  277. \-ExecutionPolicy Bypass \-File "C:\eUsers\eUser\eScripts\eSendToRecycleBin.ps1"
  278. "%FOLDER_PATH%\e%FILE_PATH%"\fP\&.
  279. .sp
  280. The only caveat that you should be aware of is that if your Syncthing
  281. folder is located on a portable storage, such as a USB stick, or if you
  282. have the Recycle Bin disabled, then the script will end up deleting all
  283. files permanently.
  284. .SH AUTHOR
  285. The Syncthing Authors
  286. .SH COPYRIGHT
  287. 2014-2019, The Syncthing Authors
  288. .\" Generated by docutils manpage writer.
  289. .