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?
5 Answers
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`).
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!
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!
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.
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!
Right! Also, on security, consider using SFTP instead of SCP if that's feasible, as it’s generally more secure.

Good point! For file transfers, `rclone` is another great option that can simplify what you’re trying to accomplish.