syncthing-versioning.7 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. .\" Man page generated from reStructuredText.
  2. .
  3. .TH "SYNCTHING-VERSIONING" "7" "Mar 11, 2021" "v1" "Syncthing"
  4. .SH NAME
  5. syncthing-versioning \- Keep automatic backups of deleted files by other nodes
  6. .
  7. .nr rst2man-indent-level 0
  8. .
  9. .de1 rstReportMargin
  10. \\$1 \\n[an-margin]
  11. level \\n[rst2man-indent-level]
  12. level margin: \\n[rst2man-indent\\n[rst2man-indent-level]]
  13. -
  14. \\n[rst2man-indent0]
  15. \\n[rst2man-indent1]
  16. \\n[rst2man-indent2]
  17. ..
  18. .de1 INDENT
  19. .\" .rstReportMargin pre:
  20. . RS \\$1
  21. . nr rst2man-indent\\n[rst2man-indent-level] \\n[an-margin]
  22. . nr rst2man-indent-level +1
  23. .\" .rstReportMargin post:
  24. ..
  25. .de UNINDENT
  26. . RE
  27. .\" indent \\n[an-margin]
  28. .\" old: \\n[rst2man-indent\\n[rst2man-indent-level]]
  29. .nr rst2man-indent-level -1
  30. .\" new: \\n[rst2man-indent\\n[rst2man-indent-level]]
  31. .in \\n[rst2man-indent\\n[rst2man-indent-level]]u
  32. ..
  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 most recent version is kept every 30 seconds.
  92. .TP
  93. .B 1 Day
  94. For the first day, the most recent version is kept every hour.
  95. .TP
  96. .B 30 Days
  97. For the first 30 days, the most recent version is kept every day.
  98. .TP
  99. .B Until Maximum Age
  100. Until maximum age, the most recent version is kept every week.
  101. .TP
  102. .B Maximum Age
  103. The maximum time to keep a version in days. For example, to keep replaced or
  104. deleted files in the “.stversions” folder for an entire year, use 365. If
  105. only for 10 days, use 10.
  106. \fBNote: Set to 0 to keep versions forever.\fP
  107. .UNINDENT
  108. .SH EXTERNAL FILE VERSIONING
  109. .sp
  110. This versioning method delegates the decision on what to do to an
  111. external command (e.g. a program or a command line script). Just prior
  112. to a file being replaced, the command will be executed. The file needs
  113. to be removed from the folder in the process, or otherwise Syncthing
  114. will report an error. The command can use the following templated
  115. arguments:
  116. .INDENT 0.0
  117. .TP
  118. .B %FOLDER_PATH%
  119. Path to the folder
  120. .TP
  121. .B %FILE_PATH%
  122. Path to the file within the folder
  123. .UNINDENT
  124. .sp
  125. Note that the former expands to the path of the actual Syncthing folder,
  126. and the latter to the path inside that folder. For instance, if you use
  127. the default \fBSync\fP folder in Windows, and the full path to the file is
  128. \fBC:\eUsers\eUser\eSync\eFamily photos\eIMG_2021\-03\-01.jpg\fP, then the
  129. \fB%FOLDER_PATH%\fP will be \fBC:\eUsers\eUser\eSync\fP, and the
  130. \fB%FILE_PATH%\fP will be \fBFamily photos\eIMG_2021\-03\-01.jpg\fP\&.
  131. .SS Example for Unixes
  132. .sp
  133. Let’s say I want to keep the latest version of each file as they are replaced
  134. or removed; essentially I want a “trash can”\-like behavior. For this, I create
  135. the following script and store it as \fB/Users/jb/bin/onlylatest.sh\fP (i.e. the
  136. \fBbin\fP directory in my home directory):
  137. .INDENT 0.0
  138. .INDENT 3.5
  139. .sp
  140. .nf
  141. .ft C
  142. #!/bin/sh
  143. set \-eu
  144. # Where I want my versions stored
  145. versionspath=~/.trashcan
  146. # The parameters we get from Syncthing
  147. folderpath="$1"
  148. filepath="$2"
  149. # First ensure the dir where we need to store the file exists
  150. outpath=$(dirname "$versionspath/$filepath")
  151. mkdir \-p "$outpath"
  152. # Then move the file there
  153. mv \-f "$folderpath/$filepath" "$versionspath/$filepath"
  154. .ft P
  155. .fi
  156. .UNINDENT
  157. .UNINDENT
  158. .sp
  159. I must ensure that the script has execute permissions (\fBchmod 755
  160. onlylatest.sh\fP), then configure Syncthing with command \fB/Users/jb/bin/onlylatest.sh %FOLDER_PATH% %FILE_PATH%\fP
  161. .sp
  162. Let’s assume I have a folder “default” in ~/Sync, and that within that folder
  163. there is a file \fBdocs/letter.txt\fP that is being replaced or deleted. The
  164. script will be called as if I ran this from the command line:
  165. .INDENT 0.0
  166. .INDENT 3.5
  167. .sp
  168. .nf
  169. .ft C
  170. $ /Users/jb/bin/onlylatest.sh /Users/jb/Sync docs/letter.txt
  171. .ft P
  172. .fi
  173. .UNINDENT
  174. .UNINDENT
  175. .sp
  176. The script will then move the file in question to
  177. \fB~/.trashcan/docs/letter.txt\fP, replacing any previous version of that letter
  178. that may already have been there.
  179. .SS Examples for Windows
  180. .SS Move to a given folder using the command prompt (CMD)
  181. .sp
  182. On Windows we can use a batch script to perform the same “trash can”\-like
  183. behavior as mentioned above. I created the following script and saved it as
  184. \fBC:\eUsers\emfrnd\eScripts\eonlylatest.bat\fP\&.
  185. .INDENT 0.0
  186. .INDENT 3.5
  187. .sp
  188. .nf
  189. .ft C
  190. @echo off
  191. rem Enable UTF\-8 encoding to deal with multilingual folder and file names
  192. chcp 65001
  193. rem We need command extensions for md to create intermediate folders in one go
  194. setlocal EnableExtensions
  195. rem Where I want my versions stored
  196. set "VERSIONS_PATH=%USERPROFILE%\e.trashcan"
  197. rem The parameters we get from Syncthing, \(aq~\(aq removes quotes if any
  198. set "FOLDER_PATH=%~1"
  199. set "FILE_PATH=%~2"
  200. rem First ensure the dir where we need to store the file exists
  201. for %%F in ("%VERSIONS_PATH%\e%FILE_PATH%") do set "OUTPUT_PATH=%%~dpF"
  202. if not exist "%OUTPUT_PATH%" md "%OUTPUT_PATH%" || exit /B
  203. rem Finally move the file, overwrite existing file if any
  204. move /Y "%FOLDER_PATH%\e%FILE_PATH%" "%VERSIONS_PATH%\e%FILE_PATH%"
  205. .ft P
  206. .fi
  207. .UNINDENT
  208. .UNINDENT
  209. .sp
  210. Finally, I set \fB"C:\eUsers\emfrnd\eScripts\eonlylatest.bat" "%FOLDER_PATH%"
  211. "%FILE_PATH%"\fP as the command name in Syncthing.
  212. .SS Move to the Recycle Bin using PowerShell
  213. .sp
  214. We can use PowerShell to send files directly to the Recycle Bin, which
  215. mimics the behaviour of deleting them using the Windows Explorer.
  216. Firstly, create the following script and save it in your preferred
  217. location, e.g. \fBC:\eUsers\eUser\eScripts\eSendToRecycleBin.ps1\fP\&.
  218. .INDENT 0.0
  219. .INDENT 3.5
  220. .sp
  221. .nf
  222. .ft C
  223. # PowerShell has no native method to recycle files, so we use Visual
  224. # Basic to perform the operation. If succeeded, we also include the
  225. # recycled file in the Syncthing\(aqs DEBUG output.
  226. Add\-Type \-AssemblyName Microsoft.VisualBasic
  227. [Microsoft.VisualBasic.FileIO.FileSystem]::DeleteFile($args,\(aqOnlyErrorDialogs\(aq,\(aqSendToRecycleBin\(aq)
  228. if ($?) {
  229. Write\-Output ("Recycled " + $args + ".")
  230. }
  231. .ft P
  232. .fi
  233. .UNINDENT
  234. .UNINDENT
  235. .sp
  236. Alternatively, the script can be expanded to send only deleted files to
  237. the Recycle Bin, and permanently delete modified ones, which makes it
  238. more consistent with how the Explorer works.
  239. .INDENT 0.0
  240. .INDENT 3.5
  241. .sp
  242. .nf
  243. .ft C
  244. # PowerShell has no native method to recycle files, so we use Visual
  245. # Basic to perform the operation.
  246. Add\-Type \-AssemblyName Microsoft.VisualBasic
  247. # We need to test if a Syncthing .tmp file exists. If it does, we assume
  248. # a modification and delete the existing file. If if does not, we assume
  249. # a deletion and recycle the current file. If succeeded, we also include
  250. # the deleted/recycled file in the Syncthing\(aqs DEBUG output.
  251. if (Test\-Path \-LiteralPath ((Split\-Path \-Path $args) + "\e~syncthing~" + (Split\-Path \-Path $args \-Leaf) + ".tmp")) {
  252. [Microsoft.VisualBasic.FileIO.FileSystem]::DeleteFile($args,\(aqOnlyErrorDialogs\(aq,\(aqDeletePermanently\(aq)
  253. if ($?) {
  254. Write\-Output ("Deleted " + $args + ".")
  255. }
  256. } else {
  257. [Microsoft.VisualBasic.FileIO.FileSystem]::DeleteFile($args,\(aqOnlyErrorDialogs\(aq,\(aqSendToRecycleBin\(aq)
  258. if ($?) {
  259. Write\-Output ("Recycled " + $args + ".")
  260. }
  261. }
  262. .ft P
  263. .fi
  264. .UNINDENT
  265. .UNINDENT
  266. .sp
  267. Finally, we set the command name in Syncthing to \fBpowershell.exe
  268. \-ExecutionPolicy Bypass \-File "C:\eUsers\eUser\eScripts\eSendToRecycleBin.ps1"
  269. "%FOLDER_PATH%\e%FILE_PATH%"\fP\&.
  270. .sp
  271. The only caveat that you should be aware of is that if your Syncthing
  272. folder is located on a portable storage, such as a USB stick, or if you
  273. have the Recycle Bin disabled, then the script will end up deleting all
  274. files permanently.
  275. .SH AUTHOR
  276. The Syncthing Authors
  277. .SH COPYRIGHT
  278. 2014-2019, The Syncthing Authors
  279. .\" Generated by docutils manpage writer.
  280. .