Restore Bash Script

I have a backup directory with a tar file in that directory (/home/username/userhome/backup/userbackup-${currentdate}.tar.gz). I would like a create a script that:

  1. creates the restore directory if it does not exist
  2. displays the contents of the backup directory containing tar files of previous backups
  3. asks the user to enter the name of the tar file to restore
  4. uses the tar command to restore the file to the new restore directory and log file

So far my script has

#!/bin/bash
mkdir restore -p
ls /home/username/userhome/backup
echo "Enter the file name to be restored"
read $filename
tar xvf "/home/username/userhome/restore/$filename" &>/home/username/userhome/restore/restore.log

I am a complete newbie so any help will be greatly appreciated.

1

1 Answer

Continuing from the comment, one thing you always want to do when writing a script is to validate each step along the way. For instance, if you cannot create the new restore directory, you don't want to loop and then attempt to extract a tar.gz file to the directory that doesn't exits.

Virtually all commands return 0 on success or a nonzero error on failure. You can use this to your advantage to check if the command succeeded and if it didn't you simply exit. A quick way to do that is:

command || exit 1

In your case creating the restore directory that could be:

mkdir -p restore || exit 1

You can add additional error messages if you like, but most time the error generated by the failure will be sufficient to tell you what went wrong.

Whenever you are operating on a fixed base directory using subdirectories of that base, it is best to create a variable for that base directory you can use in your script. For example:

#!/bin/bash

userdir="${1:-/home/username/userhome}"
budir="$userdir"/backup
restore="$userdir"/restore

Here userdir is the base directory and you have additional variables for the backup directory and restore directory. This makes it convenient to reference or operate on files in any of the directories. Note also how userdir can be set from the first command line argument or uses /home/username/userhome by default if no argument is given.

You can create restore and change to that directory, validating each step as follows:

mkdir -p restore || exit 1
cd "$restore" || exit 1

For the menu, let select create the menu for you (now if you have hundreds of .tar.gz files, you may need to write a custom pager, but for a few dozen files, select will be fine). You can generate the menu and restore the selected file with:

select choice in "$budir"/*.tar.gz; do
  tar -xvf "$choice" &>"$restore/restore.log"
  break
done

Putting it altogether, you would have:

#!/bin/bash

userdir="${1:-/home/username/userhome}"
budir="$userdir"/backup
restore="$userdir"/restore

mkdir -p restore || exit 1
cd "$restore" || exit 1

select choice in "$budir"/*.tar.gz; do
  tar -xvf "$choice" &>"$restore/restore.log"
  break
done

Example Use/Output

Say I have a couple of .tar.gz files in a directory, e.g.

$ tree /home/david/tmpd/backup
backup
├── v.tar.gz
└── x.tar.gz

Then to create a restore directory under the tmpd directory I can run the script as:

$ bash ~/scr/tmp/restore.sh /home/david/tmpd
1) /home/david/tmpd/backup/v.tar.gz
2) /home/david/tmpd/backup/x.tar.gz
#? 2

By choosing 2 above the x.tar.gz file is restored under the restore directory, e.g.

$ ls -al restore/
total 4
drwxr-xr-x 3 david david 80 Oct 29 01:00 .
drwxr-xr-x 4 david david 80 Oct 29 01:00 ..
drwxr-xr-x 3 david david 60 Oct 29 01:00 home
-rw-r--r-- 1 david david 57 Oct 29 01:00 restore.log

So the restore directory was successfully created, the restore.log was created and the .tar.gz file was restored under the restore directory.

The contents of restore.log are

$ cat restore/restore.log
home/david/scr/utl/xscrd.sh
home/david/scr/utl/xzdate.sh

(which were the two sample files I added to the x.tar.gz file)

Look things over and let me know if you have further questions.

2

Your Answer

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

James H. Sterling

James H. Sterling

Environmental Science & Climate Journalist

James Sterling reports on renewable energy developments, climate policy, ecological conservation, and green tech innovations around the globe.

Share this article
Twitter Facebook Pinterest