Rsync remote files to local comp & delete transfered files
linux
sudo mrdir -P /var/apps/BACKUPS/transfer_in_progress sudo mrdir -P /var/apps/BACKUPS/to_be_transfered
given you add files to /var/apps/BACKUPS/to_be_transfered
#!/usr/bin/env sh
HOST=fedora@111.222.333.444
if ssh $HOST "find /var/apps/BACKUPS/to_be_transfered/ -type f -print -quit | grep -q ."; then
echo "The remote directory contains files. Executing the local command..."
ssh $HOST "rsync -rt --remove-source-files /var/apps/BACKUPS/to_be_transfered/ /var/apps/BACKUPS/in_progres"
rsync -rt --remove-source-files "$HOST:/var/apps/BACKUPS/in_progres/*" /media/t/bckup/hetz/2025/
else
echo "The remote directory is empty or only contains nested empty directories."
ficrontab -e
30 2 * * * cd /dir/where/the_sh_script/is/located && script.sh >> script.log
(execute at 2:30 in the morning every day)
ssh key in different location
Same script but SSH key is in different folder (e.g in encrypted drive). Best approach is to configure ssh config .ssh/config
cat .ssh/config
Host 111.222.333.444
User root
IdentityFile /media/t/bckup/hetz/ssh/pi_ed25519And same script will work