After googling for an hour, I could not find an example of what I am looking for:
- On an Ubuntu machine that basically functions as ‘master’ for all my devices running SyncThing,
- when syncing (to this machine) of a specific folder (or device) is completed,
- run a command copying the contents of the specific folder to a different location (basically: backup that folder, but not other folders of that device).
- continuously check for new syncs (new events of completed sync) and run the command.
I understand SyncThing has a REST API that has an event called FolderCompletion. I have seen 2 scripts that use it, but no action seems to be triggered in those scripts.
As I have no experience with APIs and do not know any scripting language (I can read some, using common sense and I know rsync, cp, mv commands)… I am looking for friendly help to use this information to continuously check for the folder/device completion and run a CP command.
I can create a systemd service file myself, to run that script as a daemon.
I can create the command to copy myself.
What I do not know is how to write the script itself (.sh or .py?) to continuously check FolderCompletion events (for at least a specific SyncThing device, preferably a specific folder) and run my command.
If all you want to do is run linux utils, I’d prefer shell scripts/bash over python. If you want to learn something about a more versatile programming language in the process, use python.
What do those scripts do? Essentially you need a part that determines whether the folder is finished (that’s going to be the hard part), the rest is super simple: Just call
mv source dest and you’re done.
In general this seems like a generic scripting question, where you might get better answers in different places. For the Syncthing specific part you’re at the right place, so ideally show what you tried and what doesn’t work or where you’re stuck, thus it’s more likely you can get targeted help. There’s also been a thread not too long ago with someone doing extensive scripting for I believe a similar purpose - I couldn’t find it in a quick search though. Maybe worth searching for.
By the way, it’s Syncthing
Thanks, definitely getting over my head. But then I found a script on Github that watches for folder changes and prints the changes. I tested the script as is, it works:
For my usecase, I thought all I have to do is:
- Change line 35 and line 73, replacing
- Then instead of the
print command on line 60, use an
rsync command to copy files that don’t exist yet or are older at the backup location.
- Then, find a way to do this only for all folders with “Photos” in its name (those folders are called “Tom’s Photos”, Annie’s Photos").
- And turn it into a systemd service.
Still running in to some errors with (1).
I have got step 1 working with lots of trial and error! My fork.
I modified the script to:
- Only act on
FolderCompletion ItemFinished *.
- Get relevant information for ItemFinished (instead of ItemStarted).
- Line 64: Check if this event is a file update & if it was a successful operation.
- Write to logfile instead of printing:
2020-10-29 18:21:46,813 INFO 2020-10-29T18:13:05.879846929+01:00 ItemFinished update Zilexa's docs /mnt/pool/Users/Local/Zilexa/Mytestdoc (another copy).pdf
2020-10-29 18:21:56,824 DEBUG Starting new HTTP connection (1): 192.168.88.10:8384
I do need your help for 2 more things:
- Could you verify if my line 66 correctly checks if error=null? If I actually use “null” it does not work. With None it does, but I cannot test an actual error…
- I am having trouble passing the information to my
cp command. I simply do not know how to do that. For example I tried (line 69):
cp -u --preserve=timestamps "file_path" /mnt/pool/Collections/Pictures/Test/
mv "file_path" "folder_path"/archive
But that gives syntax errors. I tried without quotations “” but that didn’t help.
These variables do print successfully in my logfile (folder_path, file_path, folder_label).
Can you assist here?
BTW I switched to ItemFinished, that’s the only way to backup properly. With FolderFinished, the backup action could still be ongoing while more files are updated. Now I can run a
cp && mv command for each updated item.
Probably, depends on what the library does but I would expect json null to translate to python
The lines are not correctly indented. Python cares about this, use an editor that takes care of this for you. Also you want to use the values in the map from the event, not put strings there.
Honestly, both of these question can be answered by yourself with a bit of research (as in searching/reading docs) and experimentation. I unfortunately don’t have time to assist you to learn python. There are likely other venues dedicated to asking python questions, but regardless of where you ask always include what you tried so far and why you are stuck.
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.