Hello guys, I need to create a daily backup for files and save them on another server
I use

rsync -av --progress --inplace --rsh='ssh -p22' /var/www/html/product root@37.101.11.144:/var/www/html/backup/

but this ask me for password , I want to create a cron job to copy files automatically at midnight.
how can I baypass the password ?

Dani AI

Generated

As suggested, the right approach is SSH key authentication so the job can run unattended. Create a dedicated, non-root backup account on the destination and install a public key for that account. Clarifying : plain SSH keys do not expire by themselves — rotate keys as an operational policy if expiry is required. For cron, either use a key without a passphrase (less secure) or arrange a persistent ssh-agent; cron will not prompt for a passphrase.

Generate and install a keypair, then verify permissions and connectivity:

ssh-keygen -t ed25519 -f ~/.ssh/backup_ed25519 -C "backup key"
# if ed25519 is unavailable, fallback:
# ssh-keygen -t rsa -b 4096 -f ~/.ssh/backup_rsa -C "backup key"

ssh-copy-id -i ~/.ssh/backup_ed25519.pub -p 22 backup@remotehost

# manual install if ssh-copy-id missing:
cat ~/.ssh/backup_ed25519.pub | ssh -p22 backup@remotehost 'mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys'

ssh -i ~/.ssh/backup_ed25519 -p 22 backup@remotehost 'chmod 700 ~/.ssh && chmod 600 ~/.ssh/authorized_keys'
ssh -i ~/.ssh/backup_ed25519 -p 22 backup@remotehost 'echo OK'

Put the rsync invocation in a small script and call it from cron with full paths and logging:

0 0 * * * /usr/local/bin/backup-to-remote.sh >> /var/log/backup.log 2>&1

Example script skeleton:

#!/bin/sh
PATH=/usr/bin:/bin
/usr/bin/rsync -az -e "/usr/bin/ssh -i /home/backupuser/.ssh/backup_ed25519 -p 22" /path/to/source/ backup@remotehost:/path/to/dest/

Security notes: avoid using root over SSH; restrict the key in the remote ~/.ssh/authorized_keys with options like from="SOURCE_IP", no-pty,no-agent-forwarding,no-X11-forwarding,no-port-forwarding, or a small forced-command wrapper that validates SSH_ORIGINAL_COMMAND. Always test with rsync's dry-run before scheduling.

Recommended Answers

All 3 Replies

Just a direction here. instead of password, there might be some setting to use public key and private key by which the authorization works. try to setup that. Caution: these keys will have an expiry date I guess. if it is the case, then periodically you need to change these keys for the process to work.


its exactly what I want :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.