I want to fully automate SSH rsync using cron and expect.
rsync expect
Create a script like this.
#!/usr/bin/expect
# log_file /var/log/expect.log
set PW "password"
set Prompt "\[#$%>\]"
# set RemoteHost [lindex $argv 0]
set timeout 5
spawn rsync -av -e "ssh" -r "/mnt/c/ContaCam/Logicool HD Pro Webcam C920/2018/" [email protected]/root/lib/contacam
# spawn env LANG=C /usr/bin/ssh ${RemoteHost}
expect {
-glob "(yes/no)?" {
send "yes\n"
exp_continue
}
-glob "password:" {
send -- "${PW}\n"
}
}
expect {
-glob "${Prompt}" {
interact
exit 0
}
}
Please modify set PW "password" and spawn rsync -av -e "ssh" -r "/mnt/c/ContaCam/Logicool HD Pro Webcam C920/2018/" [email protected]/root/lib/contacam.
Let's try running the created script.
yuis@DESKTOP-UHU8FSH:/mnt/c/pg$ /mnt/c/pg/expect/rsync_ssh
spawn rsync -av -e ssh -r /mnt/c/ContaCam/Logicool HD Pro Webcam C920/2018/ [email protected]:/root/lib/contacam
[email protected]'s password:
sending incremental file list
created directory /root/lib/contacam
./
01/
01/31/
01/31/確認済み/
01/31/確認済み/det_2018_01_31_08_23_53.gif
01/31/確認済み/det_2018_01_31_08_23_53.mp4
01/31/確認済み/det_2018_01_31_08_24_12.gif
01/31/確認済み/det_2018_01_31_08_24_12.mp4
01/31/確認済み/det_2018_01_31_08_26_38.gif
01/31/確認済み/det_2018_01_31_08_26_38.mp4
01/31/確認済み/det_2018_01_31_08_28_57.gif
01/31/確認済み/det_2018_01_31_08_28_57.mp4
01/31/確認済み/rec_2018_01_31_16_17_52.mp4
sent 1,986,489 bytes received 247 bytes 1,324,490.67 bytes/sec
total size is 1,985,304 speedup is 1.00
Yes, it automated the password input properly.
Next, let's register this in cron.
rsync cron
Registering this in cron should probably work.
*/10 * * * * /mnt/c/pg/expect/rsync_ssh
However, cron in bash on windows has too many bugs to be usable.
So, I'd like to use a method of looping in a shell script. I hope Microsoft fixes the cron bugs eventually, so let's endure until then.
First, for testing.
#!/bin/bash
while [ 1=0 ]
do
sleep 3
echo "===================================================================="
df -h
done
Now, this one too.
#!/bin/bash
while [ 1=0 ]
do
sleep 3
echo "===================================================================="
/mnt/c/pg/expect/rsync_ssh
done
Did you get output like this?
$ . /mnt/c/pg/dev.sh
====================================================================
spawn rsync -av -e ssh -r /mnt/c/ContaCam/Logicool HD Pro Webcam C920/2018/ [email protected]:/root/lib/contacam
[email protected]'s password:
sending incremental file list
sent 334 bytes received 15 bytes 698.00 bytes/sec
total size is 1,985,304 speedup is 5,688.55
====================================================================
spawn rsync -av -e ssh -r /mnt/c/ContaCam/Logicool HD Pro Webcam C920/2018/ [email protected]:/root/lib/contacam
[email protected]'s password:
sending incremental file list
sent 334 bytes received 15 bytes 698.00 bytes/sec
total size is 1,985,304 speedup is 5,688.55
====================================================================
spawn rsync -av -e ssh -r /mnt/c/ContaCam/Logicool HD Pro Webcam C920/2018/ [email protected]:/root/lib/contacam
[email protected]'s password:
sending incremental file list
sent 334 bytes received 15 bytes 698.00 bytes/sec
total size is 1,985,304 speedup is 5,688.55
Now, let's make it execute every 5 minutes.
#!/bin/bash
while [ 1=0 ]
do
sleep 300
echo "===================================================================="
/mnt/c/pg/expect/rsync_ssh
done
That's all done.