# Skript wiederholt als Service ausführen

Als Tutorial wird hier eine Anleitung beschrieben anhand des Falls für die Synchronisation eines Ordners mit der Nextcloud.
Für die Synchronisation mit der Nextcloud soll der Command <code>nextcloudcmd</code> periodisch ausgeführt werden.
Anleitung Orientiert sich am Fall mit [restic](https://blog.bithive.space/post/automatic-backups-with-restic/).

## Skript erstellen
Im Verzeichnis <code>/etc/nextcloud</code> wird das folgende Skript <code>auto_sync.sh</code> erstellt (USER und PASSWORT entsprechend anpassen):

```bash
#!/bin/bash
nextcloudcmd /opt/nextcloud/ https://<USER>:<PASSWORT>@nx22569.your-storageshare.de/
```

Das Skript wird ausführbar gemacht:

```
chmod +x auto_sync.sh
```

## Timer erstellen

<code>/etc/systemd/system/nextcloud-sync.timer</code> erstellen:

```
[Unit]
Description=Nextcloud Sync every 30 seconds

[Timer]
OnCalendar=*-*-* *:*:00,30
Persistent=true

[Install]
WantedBy=timers.target
```

<code>/etc/systemd/system/nextcloud-sync.service</code> erstellen:

```
[Unit]
Description=Nextcloud Sync with nextcloudcmd
[Service]
Type=simple
Nice=10
ExecStart=/etc/nextcloud/auto_sync.sh
```

## Timer starten
Timer muss einmalig gestartet werden mit 

```
systemctl enable nextcloud-sync.timer --now
```