How to Use SCP in a Cron Job for Backups?
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
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!
Reply to this answer
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
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
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
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.
rural mailbox route: Right! Also, on security, consider using SFTP instead of SCP if that's feasible, as itโs generally more secure.