Shell Script

Hi everyone. I'm new and was looking for some help?

I need to create a shell script that has a menu and offers the following
back up (Where the user can specify just where they wish it to go)
restore (user can choose previous backup that they want to restore)

I need to insure the backups have the date of the backup in them. I also need to store the log files generated by these files in an appropriate as well as generate them onto the screen for the user to see. The script I want to create has to be able to tell the user what's going on at each stage, before asking the user if they want to quit or backup/restore again.

I'm in year five (Although this problem has nothing to do with school it is part of a work placement I am undertaking where they have asked me to do this) I am merely stating that I did not learn any of this in school but I think the work placement I am on is under the impression that I did and I don't want to make the school out to be liars.

I have created the menu with the users choice the part that I am having a problem with is the part where the users have to specify where they want their backup to go or where they want to restore it from and with not getting this bit done I am also having problems with generating the date for them and the log files.

The company have asked me to do this so they can implement for a few of their users, such as the work placement students. If you can help thank you.

Hi,

Can you post your code, so that we can help from your code.

while true
do
clear
## Menu
printf "Choose an option
	a) Backup
	b) Restore
	\n"
read ANSWER
	case $ANSWER in
 
	a) tar cf home.tar 
	b) tar xf home.tar 
	

I don't have the exact code on me as I'm building it on one of the computers in the company and didn't bring it home with me cause I don't have the operating system. But the code above is basically the code I was using and it was doing the backup/restore but wouldn't let me choose where to restore or backup to.

I can get the code to run and back up but the problem I can't seem to get around is I have to backup the folders where the person using the program specifies, that is the part I'm having trouble with. Also the other part that isn't working for me is the part where I show how the backup is doing and where it is going with the log files it generates.

One query here. When you say backup , what do you mean exactly ? Seeing your code, you are taking a backup of your home directory . But we need to know what back up you mean.

There also a question that, when user prefers to restore, you may have multiple old versions of backup to restore. I think you need to add the provision to choose which version user wants to restore unless you keep only one version of backup.

This is not the complete one.. modify according to your requirement

while true
do
    echo "Choose an option"
    echo "1. Backup"
    echo "2. Restore"
    echo -en "\nEnter your choice : "
    read choice
    
    case $choice in 
        
        1)     echo -n "Enter the backup filename: "
            read filename
            echo -n "Enter the Directory Name : "
            read dir_name
            mv $filename $dir_name
            echo "$filename moved successfully to $dir_name"

        2)    echo -n "Enter the filename to restore : "
            read filename
            echo -n "Enter the destination (directory name) : "
            read dir_name
            mv $filename $dir_name

        *)    echo "Choose the Correct choice (1/2)"
    esac
done

You can check

for some pointers on the menu part.

This Need a simple script has good pointers for backuping files. If you like it to be simple just use

For creating( -v for extra info you can skip it):

tar -czvf /home/$(whoami).$(date +%Y%m%d)-backup.tar.gz /home/$(whoami)

And for restore:

tar -xzvf /home/$(whoami).$(date +%Y%m%d)-backup.tar.gz

You can also ask the user for the date of the backup needing restore in the above format and use it in your script. It is always good idea to store backups at least on different disk, ideally on offsite storage.

p.s. for easier menu generation you can use pdmenu - pdmenu

Hey, thank you for all the help. I got the menu part working fine and making the choice and I could backup it was just asking the user where they wanted to backup that had me confused.

If I wanted to show the user the log files generated by the backup whilst it's being backed up how would I do that? And when I say backup, I just mean when a user trying to back up a file or folder.

Jess

With the above provided script(the one written by itkamaraj) you ask the user for backup destination directory and move the file there, this is not needed and probaly will confuse the user.

You can just easily ask them for destination(as you did) and create the archive there

Example:

while true
do
    echo "Choose an option"
    echo "1. Backup"
    echo "2. Restore"
    echo -en "\nEnter your choice : "
    read choice
    
    case $choice in 
        
        1)     echo -n "Enter the backup filename: "
            read filename
            echo -n "Enter the Directory Name : "
            read dir_name
            tar -czvf filename $dir_name/$filename.$(date +%Y%m%d)-backup.tar.gz
            echo "$filename moved successfully to $dir_name"

        2)  echo -n "Enter the destination (directory name) : "
        read dir_name
            echo -n "Backups in $dir_name:"
            ls -al $dir_name
            echo -n "Enter the filename to restore : "
            read filename
            tar -xzvf $filename $dir_name

        *)    echo "Choose the Correct choice (1/2)"
    esac
done

On the backup "log" the -v flag of the tar command shows every file that goes into the archive example:

[root@yorkfield Downloads]# ll
total 0
-rw-r--r--. 1 root root 0 Jun  5 00:50 1
-rw-r--r--. 1 root root 0 Jun  5 00:50 2
-rw-r--r--. 1 root root 0 Jun  5 00:50 3
-rw-r--r--. 1 root root 0 Jun  5 00:50 4
-rw-r--r--. 1 root root 0 Jun  5 00:50 6
[root@yorkfield Downloads]# 
[root@yorkfield Downloads]# tar -czvf test.tar.gz ./*
./1
./2
./3
./4
./6

Menu?? Menus are for the weak.
Are you required to use a menu?

You can better use the power of the command-line by exposing everything to command-line arguments and switches.

Others have made good points about file naming, so ignoring that part, here's my contest entry:

#!/bin/sh
function error_exit() { echo "ERROR: $*" 1>&2; exit 1; }
usage()
{
    cat <<EOF
Usage:
    ${0##*/} [switches] {b|r}

Arguments:
    b - Backup your home directory
    r - Restore your home directory
EOF
    exit 0
}
CMD= STORAGE=someplace
while test $# -gt 0; do
    case "$1" in
        b|B|backup)  CMD=cfj ;;
        r|R|restore) CMD=xfj ;;
        -h|-help|--help)  usage                   ;;
        -*)  error_exit "Unknown switch \"$1\""   ;;
        *)   error_exit "Unknown argument \"$1\"" ;;
    esac
    shift
done

test -z "$CMD"  &&  usage
cd
tar $CMD $STORAGE/home.tbz .