Search code examples
linuxcrondebianlampsystemd

systemd: tar (backup) issue backup.service


The following should make a tar file with the date and after making the .tar.gz file, check wheather there is a file older than 30 days, if yes, remove it. Here is what I get when executing it in systemd. it works flawlessly when directly entered to the commandline:

executing: journalctl -u backup.service

Jun 12 14:42:39 Debian-84-jessie-64-LAMP systemd[1]: Starting Backing up folders (/var/www/)...
Jun 12 14:42:39 Debian-84-jessie-64-LAMP systemd[1]: Started Backing up folders (/var/www/).
Jun 12 14:42:39 Debian-84-jessie-64-LAMP tar[27620]: /bin/tar: Von den Optionen „-Acdtrux“, „--delete“ oder „--test-label“ ist j
Jun 12 14:42:39 Debian-84-jessie-64-LAMP tar[27620]: „/bin/tar --help“ oder „/bin/tar --usage“ gibt weitere Informationen.
Jun 12 14:42:39 Debian-84-jessie-64-LAMP systemd[1]: backup.service: main process exited, code=exited, status=2/INVALIDARGUMENT
Jun 12 14:42:39 Debian-84-jessie-64-LAMP systemd[1]: Unit backup.service entered failed state.

backup.service

[Unit]
Description=Backing up folders (/var/www/)

[Service]
WorkingDirectory=/backup/www/
ExecStart=/bin/tar -czpf "backup_$(date '+%y-%m-%d').tar.gz" /var/www/ && find /backup/www/ -maxdepth 0 -name "backup_*.*" -mtime +30

backup.timer

[Unit]
Description=Make Backup of /var/www/

[Timer]
OnCalendar=weekly
Persistent=true

[Install]
WantedBy=timers.target

Solution

  • You've asked a variation of the FAQ Why do things behave differently under systemd?.

    In that answer to that FAQ, you'll notice that system has a much more restrictive command line syntax, as documented in COMMAND LINES in man systemd.service.

    Keep in mind you can have multiple ExecStart= lines, so you don't need to use the && syntax, you can just add an extra ExecStart= line.

    Your text says your command would remove files older than 30 days, but it does not attempt to do this. In you what you posted, the find command is run, but no remove command.