0

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 script From
#!/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
    Is this your crontab? Or root's crontab? Or a system crontab? Commented Jun 21, 2021 at 16:12
  • This is a root crontab Commented Jun 21, 2021 at 18:02

1 Answer 1

0

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
  • Thanks for the help how should i add the entries from root's crontab to maurice's crontab: Commented Jun 21, 2021 at 20:08
  • i also got this from /var/mail/root ssh: connect to host <ip-address-here> port 22: No route to host rsync: connection unexpectedly closed (0 bytes received so far) [sender] rsync error: unexplained error (code 255) at io.c(235) [sender=3.1.3] Commented Jun 21, 2021 at 20:15
  • "how should i add the entries from root's crontab to maurice's crontab" How did you add them to root's? Commented Jun 21, 2021 at 21:08
  • Your SSH error says your client doesn't know how to get to the remote server. That's nothing to do with crontab and everything to do with networking. You need to fix that first Commented Jun 21, 2021 at 21:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .