1

I am trying to run a backup script foo.sh via cron. The code is as follows:

#!/bin/bash
export PASSPHRASE=password123
duplicity ../learningbash file://../../../media/kingston
unset PASSPHRASE

Then I add dthe following line in "crontab -e":

58 07  * * * /home/ashish/learningbash/foo.sh

It runs perfectly when I run it from the terminal using ./foo.sh. But it does not run from the cron. Also, if I edit "foo.sh" to the following code, the first line executes perfectly from the cron. But the backup script doesn't run.

#!/bin/bash
touch hello.txt

export PASSPHRASE=bacteria99
duplicity ../learningbash file://../../../media/kingston
unset PASSPHRASE

2 Answers 2

0

That's because you have used relative paths in your script:

duplicity ../learningbash file://../../../media/kingston

This is working from terminal because you were in the correct directory where you should be to interpret the relative paths correctly.

cron of an individual user sets the user's home directory as PWD.

To solve the issue, use absolute path e.g.:

duplicity /bar/learningbash file:///foo/bar/media/kingston
1
  • @AshishNeupane Glad I could help :)
    – heemayl
    Feb 9, 2016 at 16:19
0

I ended up installing expect, and setting up trusted root ssh logins on localhost to make this work. cron calls duplicity.expect, which contains:

#!/usr/bin/expect -f
# run duplicity backups script with a tty
set timeout 7200
spawn ssh -tt localhost /opt/backups/bin/duplicity.sh
expect {
  timeout {
    puts "Connection timed out"
  }
  eof {
    puts "EOF reached"
  }
  "yes/no" {
    send "yes\r"
    exp_continue
  }
}

duplicity.sh contains the actual duplicity backup commands to run, and the PASSPHRASE env variable that GPG will use to encrypt them all.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .