Quantcast
Channel: Syncthing Community Forum - Latest topics
Viewing all articles
Browse latest Browse all 6194

Windows one direction one shot syncing task scheduler

$
0
0

Hello. New here. Will try to make this short. My windows scripting skills are very limited. Below scripts were created using samples I found online but need improvement.

I have two instances of syncthing installed.

a) client - windows pc b) server - freenas box

Goal: To periodically (once a day?) mirror files from the window pc to the nas. The whole process should be automated.

I’ve broken this down into two parts.

  1. Launch syncthing process on the windows pc at bootup and/or login
  2. Perform sync at scheduled time(s).

Initial launch at boot/login

Using windows task scheduler with triggers at startup, at logon, work station unlock, the following script will run.

start.bat

@echo off
SETLOCAL EnableExtensions
set EXE=syncthing.exe
set LOG=E:\syncthing\log.txt
set home=E:\syncthing
set PROGRAM="E:\syncthing\syncthing.exe"

rem syncthing start loop

FOR /F %%x IN ('tasklist /NH /FI "IMAGENAME eq %EXE%"') DO IF %%x == %EXE% goto FOUND
echo  %date:~4,10% %time% STARTING SyncThing Process  >> %LOG%
start "SyncThing" %PROGRAM% -no-browser -no-console -paused -home=%home% > NULL
goto FIN
:FOUND
echo  %date:~4,10% %time% SyncThing already runing. >> %LOG%
:FIN

The script checks to see if syncthing is running. If it is, it does nothing and exits. This gets the job done, but I’d to add another check to make sure syncthing is responsive (responds to the a ping via api?). If output is pong, then exit, otherwise kill and restart the process.

If process it not running, it starts it in a paused state.

The next part is the actual mirror process initiated by task scheduler. Trigger will be a specified time.

During testing I discovered if both sides are not paused then erroneous log entries get created with one side trying to communicate with the other. Pausing both ends resolves this.

The logic is unpause both sides, wait a minute, perform mirror, pause both. Although this will be used on local lan only, https used in case some other program happens to be sniffing network :).

Any suggestions for improving this task or implementing differently? Variations of this batch file will be used for specific folders (some only need to get sync once a week) using optional parameters - POST /rest/db/scan — Syncthing v1 documentation .

sync.bat

@echo off
SETLOCAL EnableExtensions
rem define variables
set LOG=E:\syncthing\log.txt
set local_apikey={API KEY}
set remote_apikey={API KEY}
set local_deviceid={DEVICE ID}
set remote_deviceid={DEVICE ID}

rem Unpause source and destination services

echo  %date:~4,10% %time% Unpause local and remote servers >> %LOG%
curl --insecure -X POST -H X-API-Key:%local_apikey% https://localhost:8384/rest/system/resume
curl --insecure -X POST -H X-API-Key:%remote_apikey% https://192.168.1.11:8384/rest/system/resume?device="%remote_deviceid%"

Rem perform sync

timeout 60 /nobreak > NUL
echo  %date:~4,10% %time% Perform sync  >> %LOG%
curl --insecure -X POST -H X-API-Key:%local_apikey% https://localhost:8384/rest/db/scan
timeout 5 /nobreak > NUL

echo  %date:~4,10% %time% Pause local and remote servers >> %LOG%

rem Pause source and destination services
curl --insecure -X POST -H X-API-Key:%local_apikey% https://localhost:8384/rest/system/pause
curl --insecure -X POST -H X-API-Key:%remote_apikey% https://192.168.1.11:8384/rest/system/pause?device="%remote_deviceid%"

Thank you for your input.

2 posts - 2 participants

Read full topic


Viewing all articles
Browse latest Browse all 6194

Trending Articles