Backup automation with Crontab and Rsync work on local machine but doesn't on remote machine via ssh

I'm trying to make an automatic backup system that would sync 2 folders every 5 minutes with Rsync and Crontab both locally and remotely

Both Machine is running

Ubuntu Focal 20.04

Folder source and destination

My script are saved in the folder

/etc/local/<file-here>

Source: /home/maurice/dev/

Local Destination: /media/maurice/SSD/dev/

Here is the script

Local Script:

#!/bin/sh
rsync -avz /home/maurice/dev/ /media/maurice/SSD/dev/

Remote Script:

#!/bin/sh
rsync -e 'ssh -p 22' -avzp /home/maurice/dev/ maurice-Laptop.local:/home/maurice/dev/

Here is my crontab File

#Local Backup Script
*/5 * * * * maurice /etc/local/rsyncLocal_script
#Remote Backup Script
*/5 * * * * maurice /etc/local/rsync_script

Here is what work and what doesn't

Working

1.Local Backup work flawlessly

2.if i run the remote script manually it does work

3.i can ssh in both the local and remote computer without the need of a password the ssh key of both pc are added to /home/maurice/.ssh/authorized_keys

Not Working

1.Automation of the remote script

What I tried

  1. Change the Remote scriptFrom
#!/bin/sh
rsync -e 'ssh -p 22' -avzp /home/maurice/dev/ maurice-Laptop.local:/home/maurice/dev/

To

#!/bin/sh
rsync -e 'ssh -p 22' -avzp /home/maurice/dev/ maurice@ipaddres:/home/maurice/dev/

But after that i really don't know what to do else

Thanks,

2

1 Answer

The problem here is that you are using the seven field layout for crontab entries in a file that expects only six. (See man 5 crontab)

Remove the entries from root's crontab and put them into maurice's crontab:

#Local Backup Script
*/5 * * * * /etc/local/rsyncLocal_script
#Remote Backup Script
*/5 * * * * /etc/local/rsync_script

There's no need to put these in a root crontab only for cron to then change its permissions to run as maurice. Just run them as maurice in the first place.

If that really isn't possible you need to use a system crontab (as distinct from root's crontab). For example, create a snippet file /etc/cron.d/mybackups and put the lines - including maurice -there.

In both cases you should consider capturing the output of the command. (If you'd had this you would have seen an error of the style maurice: command not found.) One option is to append error logging:

*/5 * * * * /etc/local/rsyncLocal_script >/tmp/rsyncLocal.log 2>&1

Personally, since both scripts run every five minutes, I'd put both commands into a single script,

#!/bin/bash
#
# Backup locally
rsync -av /home/maurice/dev/ /media/maurice/SSD/dev/
# Backup remotely (if the server is available)
rsync -avz -e 'ssh -o ConnectTimeout=10' /home/maurice/dev/ maurice-Laptop.local:/home/maurice/dev/
4

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like