0

I'm trying to create two scripts. One called backup, the other called cancel_backup. When backup is ran, it checks if a directory is available, if so it begins the backup. If not, it tries again every 30 seconds until the directory is found.

If cancel_backup is ran, the backup script should terminate.

Here's my code so far:

#/bin/bash!

backup_location='/media/tom/EXTERNAL/Backups/Areca'  

while [ ! -d $backup_location ]
do
  if [ ! $CANCEL_BACKUP ]
  then
    notify-send --icon=/home/tom/icons/dialog-warning.png "Backup Cancelled." "Backup has been cancelled by the user."
    exit 0
  else
    notify-send --icon=/home/tom/icons/dialog-error.png "Backup Directory Inaccessible" "External backup directory is inaccessible. Retrying in 30 seconds."
    sleep 30
  fi
done

notify-send --icon=/home/tom/icons/dialog-info.png "Beginning Backup" "External backup directory located, beginning backup."

I've attempted to use export, however the variable is still not accessible to any other scripts unless they I use source within the above script.

I need to be able to run the scripts independently, but still update $CANCEL_BACKUP from a different script.

2
  • 1
    Child processes cannot change variables of their parents, that's why you need to source. Maybe decide based on exit codes of your scripts? Commented Jan 12, 2018 at 2:01
  • 1
    Environment variables are only inherited by children, there's no way to propagate variables the other way. Use a file as a flag instead of a variable. Commented Jan 12, 2018 at 2:12

1 Answer 1

2

Use a file as the flag rather than a variable.

cancel_file=/tmp/cancel_backup

if [ ! -f $cancel_file ]

The cancel_backup script can then do touch /tmp/cancel_backup. And you remove the file when you want to enable backups again.

/tmp might not be the best place for the file, since some other user could create the file there. Better would be a directory used specifically for the application, which only the application user has access to. The above was just a simple example.

Sign up to request clarification or add additional context in comments.

6 Comments

I might argue about the location of the file in /tmp, but the rest is what I'd suggest doing. Given backup_location='/media/tom/EXTERNAL/Backups/Areca' in the main script, I'd suggest cancel_file='/media/tom/EXTERNAL/Backups/cancel_backup as a more appropriate location.
@JonathanLeffler Umm..., just curious, but what would you suggest? LSB would suggest /tmp for general temporary files, and it would avoid the system /run directory (or /var/run or /lib/run on some distros), but perhaps /usr/local/run setgid to a group for those that have privilege to run backup? (I'm thinking like a pid-file)
@DavidC.Rankin: I added a suggestion. The fixed name in /tmp is a security liability on a multi-user system (a rarity, but it is worth remembering them). Placing the fixed name near where the operations are occurring seems appropriate to me. Also, it isn't a temporary file; it is a control file. If it must go under /tmp, I'd think about how to make it safer — and less likely to interfere with other people also trying to run their own backups. Options include a file in a sub-directory under /tmp with appropriate permissions, but it can't be a fixed name, which complicates things.
@JonathanLeffler I suspect /media/tom is a removable device (that's why the directory might or might not be avaiable), so it's probably not the right place for the flag. Or maybe it is -- if the device isn't mounted, the cancel file is not relevant.
I gotcha and concur. The run-control is what I was thinking. If only a single user runs the file, then the current directory is fine, but if it is run from a bin directory (either the users personal collection where more than one needs to be able to run it or /usr/local/bin,) then a local run/ex_backup dir could be created or a pid-file like file in a safer location.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.