Carl Sala
Carl Sala

Reputation: 51

rsync command works but not as a cronjob

This command works if I input in shell in order to backup locally a database file from a remote server with the date appended to each new backup file so they are all different:

rsync -avz -Iu --backup [email protected]:/home/database.sqlite /home/ProdBackups/dbBackup_(date +\%Y\%m\%d\%H\%M\%S).sqlite

But when I try running it as a cronjob inside crontab:

* * * * * rsync -avz -Iu --backup [email protected]:/home/database.sqlite /home/ProdBackups/dbBackup_(date +\%Y\%m\%d\%H\%M\%S).sqlite

I get this error:

Syntax error: "(" unexpected

How can I fix this?

Upvotes: 0

Views: 666

Answers (1)

dash-o
dash-o

Reputation: 14491

To get the date to be replaced with formatted date, you need to force execution with '$' around the parenthesis.

* * * * * rsync -avz -Iu --backup [email protected]:/home/database.sqlite /home/ProdBackups/dbBackup_$(date +\%Y\%m\%d\%H\%M\%S).sqlite

As an alternative, consider creating a small wrapper script backup.sh, and then execute the backup.sh scripts from the cron

# ~/cron/backup.sh
timestamp=$(date '+%Y%m%d%H%M%S')
rsync -avz -Iu --backup [email protected]:/home/database.sqlite /home/ProdBackups/dbBackup_$timestamp.sqlite

And the cron will be:

* * * * * $HOME/cron/backup.sh

The advantage is that you can test the backup script, and then execute it as-is from the cron, without having to worry about quoting the date format, etc.

Upvotes: 1

Related Questions