UNIX Shell Script Backup Directory on Specific day

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite












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










share|improve this question



















  • 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)
    – Raman Sailopal
    Oct 2 '17 at 11:05














up vote
0
down vote

favorite












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










share|improve this question



















  • 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)
    – Raman Sailopal
    Oct 2 '17 at 11:05












up vote
0
down vote

favorite









up vote
0
down vote

favorite











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










share|improve this question















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







shell scripting tar zip






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Oct 2 '17 at 12:52

























asked Oct 2 '17 at 10:58









Taylor Cairns

32




32







  • 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)
    – Raman Sailopal
    Oct 2 '17 at 11:05












  • 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)
    – Raman Sailopal
    Oct 2 '17 at 11:05







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)
– Raman Sailopal
Oct 2 '17 at 11:05




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)
– Raman Sailopal
Oct 2 '17 at 11:05










3 Answers
3






active

oldest

votes

















up vote
1
down vote



accepted










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





share|improve this answer





























    up vote
    0
    down vote













    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"





    share|improve this answer





























      up vote
      0
      down vote













      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





      share|improve this answer




















        Your Answer







        StackExchange.ready(function()
        var channelOptions =
        tags: "".split(" "),
        id: "106"
        ;
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function()
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled)
        StackExchange.using("snippets", function()
        createEditor();
        );

        else
        createEditor();

        );

        function createEditor()
        StackExchange.prepareEditor(
        heartbeatType: 'answer',
        convertImagesToLinks: false,
        noModals: false,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: null,
        bindNavPrevention: true,
        postfix: "",
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        );



        );













         

        draft saved


        draft discarded


















        StackExchange.ready(
        function ()
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f395622%2funix-shell-script-backup-directory-on-specific-day%23new-answer', 'question_page');

        );

        Post as a guest






























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        1
        down vote



        accepted










        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





        share|improve this answer


























          up vote
          1
          down vote



          accepted










          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





          share|improve this answer
























            up vote
            1
            down vote



            accepted







            up vote
            1
            down vote



            accepted






            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





            share|improve this answer














            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






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Oct 2 '17 at 11:20

























            answered Oct 2 '17 at 11:13









            Archemar

            19.1k93366




            19.1k93366






















                up vote
                0
                down vote













                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"





                share|improve this answer


























                  up vote
                  0
                  down vote













                  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"





                  share|improve this answer
























                    up vote
                    0
                    down vote










                    up vote
                    0
                    down vote









                    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"





                    share|improve this answer














                    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"






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Oct 2 '17 at 11:13

























                    answered Oct 2 '17 at 11:08









                    Godvil

                    337




                    337




















                        up vote
                        0
                        down vote













                        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





                        share|improve this answer
























                          up vote
                          0
                          down vote













                          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





                          share|improve this answer






















                            up vote
                            0
                            down vote










                            up vote
                            0
                            down vote









                            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





                            share|improve this answer












                            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






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Oct 2 '17 at 13:02









                            hschou

                            1,66349




                            1,66349



























                                 

                                draft saved


                                draft discarded















































                                 


                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function ()
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f395622%2funix-shell-script-backup-directory-on-specific-day%23new-answer', 'question_page');

                                );

                                Post as a guest













































































                                Popular posts from this blog

                                How to check contact read email or not when send email to Individual?

                                Bahrain

                                Postfix configuration issue with fips on centos 7; mailgun relay