Systemd Service Creation (not having much luck)

I’ve been running kopia via an @reboot command for a few weeks now and it’s been great… but ibviously now i want to set it up as a systemd service…

I used the below service config :

[Unit]
After=network.target

[Service]
user=root
ExecStart=/usr/bin/kopia server --insecure --address=0.0.0.0:51515 --server-username=user --server-password=password

[Install]
WantedBy=default.target

When i run this service, it’s not picking up the config from when i was running it from cron… i figured no big deal, ill just connect it back to the repo but when entering my password i get the below error :

kopia INTERNAL: internal server error: connect error: error opening repository: error connecting: unable to set up caching: unable to determine cache directory: neither $XDG_CACHE_HOME nor $HOME are defined

Can someone assist me in either resolving the error above, or correcting my service file to actually load the same config i was using when running from cron / cmdline?

I am running my Kopia server like this:

[Unit]
Description=Kopia Server
After=syslog.target
After=network.target

[Service]
Type=oneshot
ExecStart=/bin/bash /kopia/repos/startKopia.sh
ExecStop=/bin/bash /kopia/repos/stopKopia.sh
RemainAfterExit=true
StandardOutput=journal

[Install]
WantedBy=multi-user.target

Since the script doesn’t stay around, I am using oneshot as type and I do have all necessary bits in my start/stop scripts.

I don’t know why $HOME is not defined in your case but you can set environment variables in systemd service files:

[Service]
Environment="HOME=/root"

I personally avoid running services as root when it isn’t necessary. That’s why I’m running kopia server as normal user and connect to this server when I need to do backups as root on the same system. This is the service file I’m currently using:

[Unit]
Description=Kopia Server
After=syslog.target
After=network.target
OnFailure=systemd-failure-email@%n.service

[Service]
Type=simple
User=kopia
Group=kopia
Restart=always
RestartSec=5
ExecStart=kopia server start --no-ui --tls-cert-file /path/to/cert --tls-key-file /path/to/key --address :50505

[Install]
WantedBy=multi-user.target