0

Basically this is the question im trying to answer

2) Write a shell script (fridaybackup.sh) which will, if run on Friday, backup all files in user2 home directory. The script will create a backup (tar) file named user2backup.tar and zip file named user2backup.zip

This is what I have so far

    #!/bin/sh
    echo
    echo "Do you want to create a full backup (Y=Yes, N=No) : \c"
    read INPUT

    day=$(date +%A)
    time=$(date +%m-%d-%y)
    filename=user2backup${time}.tar.gz
    srcdir='/export/home/student/user2'
    desdir='/export/home/student/backupfolder'

   case $INPUT in
   N) echo "Bye." ;;
   Y)
   if [ "$day" = "Monday" ]
   then 
   tar -cpzf $desdir/$filename $srcdir

    else
    echo "A full backup is done on Fridays only!!!"

     fi ;;
    *) echo "Error" ;;
  esac

But I get this result

    student@solaris:~/user2$ sh fridaybackup.sh

    Do you want to create a full backup (Y=Yes, N=No) : Y
    tar: /export/home/student/backupfolder/user2backup10-02-17.tar.gz: No 
     such file or directory
    student@solaris:~/user2$ gedit fridaybackup.sh

Any help would be great and thanks in advance

1
  • 1
    If your variables are set the result of the execution of a given UNIX/Linux command then it should be within $() and so for example day=$(date +%A) Oct 2, 2017 at 11:05

3 Answers 3

1

there seems to be a copy/paste problem,

variable must be set using

day=$(date +%A)

I believe you use backtick, that dissapear under formating, as "Monday" error shows.

there is a syntax error near if

error Monday: not found

prove that setting was correct, as in line 14 bash encounter :

( Monday = friday )

this forked a sub shell, running Monday as program name and = Friday as argument. This is not what you intend.

In shell test are done using

if [ "$day" = "Friday" ]

or

if test "$day" = "Friday"

thus correct shell look like :

#!/bin/sh
 echo
 echo "Do you want to create a full backup (Y=Yes, N=No) : \c"
 read INPUT

 day=$(date +%A)
 time=$(date +%m-%d-%y)
 filename=user2backup$time.tar.gz
 srcdir='/export/home/student/user2'
 desdir='/export/home/student/backupfolder'

 case $INPUT in
 N) echo "Bye." ;;
 Y)
 if [ "$day" = "Friday" ]
 then 
 tar -cpzf $desdir/$filename $srcdir

 else
 echo "A full backup is done on Fridays only!!!"

 fi ;;
 *) echo "Error" ;;
 esac
0

Try using read -p "Do you want to create a full backup (Y=Yes, N=No) : " INPUT instead of your:

echo "Do you want to create a full backup (Y=Yes, N=No) : \c"
read INPUT

EDIT: Also as @Raman in comment explained, you made mistake declaring your variables.

This would be the best and most safe way to declare your filename variable:

filename="user2backup${time}.tar.gz"
0

The description does not require asking the user. It could do it automatically:

 #!/bin/sh
 if [ $(date +%A) = Friday ]; then
    TARFILE=/export/home/student/backupfolder/user2backup$(date +%m-%d-%y).tar
    tar -cpf $TARFILE /export/home/student/user2
    zip $TARFILE.zip $TARFILE
 fi

You must log in to answer this question.

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