How to Use SCP in a Cron Job for Backups?

November 23, 2025 5 answers
Asked by buf+256k
Hey everyone! I'm trying to set up a cron job to automatically back up data from one server to another. Essentially, I've created a bash script that zips a directory's contents and then uses SCP with a passphraseless SSH key to copy the zipped file to a remote server. While this setup works great when I run it manually in the terminal, I ran into issues when scheduling it with cron. The SCP command fails to authenticate, and I discovered that the cron environment lacks certain elements like SSH agent. Can anyone explain why I need the SSH agent with a passphraseless key? I'm not keen on hardcoding credentials, which I resorted to using sshpass for. Is there a reliable way to run SCP in a cron job that remains functional even after a server restart?

Answers

feather taxonomy
You need to provide more details about the error you're encountering. Make sure to check the logs and redirect stdout and stderr when setting up your cron job. Also, remember that cron runs in a limited environment - you must use absolute paths for your commands and set any necessary environment variables, as your usual login profile wonโ€™t apply. You can simulate a cron-like environment to test things out too!

rural mailbox route: Right! Also, on security, consider using SFTP instead of SCP if that's feasible, as itโ€™s generally more secure.

Reply to this answer
saffron!sparingly
It sounds like you're running into classic cron job issues. First, make sure you're executing the script as the user that has access to the SSH key. If the output from cron isn't helpful, try redirecting it or checking the userโ€™s mail. As an alternative to SCP, consider using `rsync`, which can be much more efficient for backups. You might also like `rsnapshot` if you'd prefer a more automated solution.

17 degrees warmer: Good point! For file transfers, `rclone` is another great option that can simplify what youโ€™re trying to accomplish.

Reply to this answer
MARGIN.
One extra thought is to use a systemd timer instead of cron, depending on what distro you're on. It gives you more control over dependencies and execution, which might make things easier overall if you can leverage it!
Reply to this answer
irq7.mask
Here's a simple example script you can use for your cron job. Ensure the SSH key is properly set up: `SSH_KEY="/home/youruser/.ssh/id_rsa"` Then use the SCP command like this: `scp -i $SSH_KEY /path/to/local/file.zip user@remote-server:/path/to/remote/destination/` Don't forget to log both success and failure to keep track of operations better!
Reply to this answer
copperhead trail
Always keep security in mind! Having your cron job with SSH access means if the server you're backing up gets compromised, so do your backups. Maybe look into key permissions and ensure they're set securely (e.g., `chmod 400 your_key_file`).
Reply to this answer

Add your answer

Answers are reviewed before publication.

Related Discussions