Ubuntu – Permissions of shell script: how to write into /var

permissionsscripts

I am writing a backup script. The script is located in /usr/local/backup-scripts
The files should be stored to /var/backup/subdirectory
I want to run the script using cron job or systemd-timer.

I have no idea how to set the permissions correctly. Of course for testing I can sudo run the script. But what when the cron job or systemd runs the script? Do they (they=cron or systemd) have su-rights?

Unfortunately I am not that familiar with the permission in Linux so I have no idea if I have to chown the script to a special user (maybe sys???) or what the "right" way is.
Appreciate every help, cheers Stefan

Best Answer

System user and cron

A simple and secure way would be to create a backup-user as system user (no home folder). Then give permissions to the backup-user for the backup location:

$ adduser --system --no-create-home backup-user
$ chown -R backup-user /var/backup/subdirectory

Also you need to specify the user in the cron job.

Backup tool

While writing own backup shell scripts is fun for a little while, sooner or later it becomes hard to mange all the corner cases. So if you get tired of writing shell scripts, checkout bacula. It is definitely more work to setup, but it offers more features then self written scripts.

Related Question