0

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
  • Suggest you change to the directory first. mkdir -p restore will create the restore directory in whatever directory is the current directory when the script is called. Also suggest mkdir -p restore || exit 1 so you exit at that point if you don't have permissions to create the directory. When you cd to the directory holding the .tar.gz files, suggest you use select choice in *.tar.gz; do ... done . The select command will create a menu for you to select a file from. It will display a numbered list and you choose the number next to the filename you want, choice holds the name. Commented Oct 29, 2021 at 5:48

1 Answer 1

3

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.

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

2 Comments

That is fantastic, David. I very much appreciate you taking the time to answer my question in such detail, just the help I needed. Thank you!
Glad to help. Good luck with your scripting!

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.