0

I'm trying to use an expect script to send borg backups to a remote server and prune old ones. The script to send the backups works just fine. However the script to prune the archive times out before any of the backups are pruned. I have confirmed manually running the command does work. Any thoughts as to why it is timing out?

#!/usr/bin/expect

set folder [exec bash -c "ls -td /home/.snapshots/* | head -n 1"];
set otp [exec oathtool --totp -b KEY];

spawn borg prune --keep-hourly 5 --keep-daily 7 --keep-weekly 4 --keep-monthly 3 --keep-yearly 1 --prefix='home' ssh://user@host:PORT/ARCHIVE

expect "Enter passphrase for key '/root/.ssh/id_ed25519': "
send -- "PASSWORD\r"

expect "Verification code: "
send -- "$otp\r"

expect "Enter passphrase for key ssh://user@host:PORT/ARCHIVE: "
send -- "PASSWORD\r"

expect eof
wait
  • Why are you using `expect` to answer SSH password prompts? You really shouldn't do that. – John Kugelman Jan 22 '18 at 18:33
  • When I find any issue I use autoexpect to generate script and then modify it to simplify. – Shashwat Kumar Jan 22 '18 at 18:37
  • @JohnKugelman Do you know a better way to send borg backups to a remote archive? I'm seriously open to suggestions. – Phillip Brantley Jan 22 '18 at 18:38
  • 1
    Configure SSH well and it won't have any prompts. Its prompts are intended for human consumption only. For instance, [get rid of the passphrase](https://stackoverflow.com/questions/112396/how-do-i-remove-the-passphrase-for-the-ssh-key-without-having-to-create-a-new-ke) on the private key being used to connect. Filesystem permissions should be sufficient to keep the key safe from prying eyes. (There's no use in having a passphrase that's listed elsewhere in an expect script.) – John Kugelman Jan 22 '18 at 18:47

1 Answers1

1

The default expect timeout is 10 seconds, if your pruning operation runs for more than 10 seconds and does not yield any output, expect will timeout.

You should set your timeout at the beginning of your script to 2 minutes for example:

#!/usr/bin/expect

set folder [exec bash -c "ls -td /home/.snapshots/* | head -n 1"];
set otp [exec oathtool --totp -b KEY];
set timeout 120
[ ... ]

You may also set your expect script to DEBUG to be able to see why it fails in the future as so:

#!/usr/bin/expect -d
AnythingIsFine
  • 1,777
  • 13
  • 11