0

I have a remote server and a local server. I am trying to automatically transfer a nightly backup on the remote server to my local one. SSH keys are setup, so no password is required.

Using the following command, it works manually:

scp -r -P {REMOTEPORT} user@serveripaddress:/home/remoteuser/directory /home/localuser/directory

I would like to set a cronjob so the previous action runs, but I am new to writing actual scripts. Is there a simple script that would make this happen?

Eric
  • 37

1 Answers1

3

You could write a simple script, let's say backup.sh :

#!/bin/bash
/usr/bin/scp -r -P {REMOTEPORT} user@serveripaddress:/home/remoteuser/directory /home/localuser/directory

Make it executable with chmod +x backup.sh

Then, edit crontab (crontab -e) and set it up :

0 0 * * * /path/to/backup.sh > /var/log/backup.log 2>&1

Also, you could run it directly from cron (crontab -e) :

0 0 * * * /usr/bin/scp -r -P {REMOTEPORT} user@serveripaddress:/home/remoteuser/directory /home/localuser/directory > /var/log/backup.log 2>&1

ps : if you run into troubles, for sure we can help, but i strongly recommend reading this for debugging : Why is my crontab not working, and how can I troubleshoot it?

krisFR
  • 13,690