Find the answer to your Linux question:
Results 1 to 6 of 6
Hey there, I'm attempting to create a shell script using Ubuntu that will take one optional argument (which in this case is a path to a directory to backup) and ...
  1. #1
    Just Joined!
    Join Date
    Jul 2009
    Posts
    3

    Backup script

    Hey there,

    I'm attempting to create a shell script using Ubuntu that will take one optional argument (which in this case is a path to a directory to backup) and will back up (by using the tar command) the chosen directory which is the /home. And i want, after tar'ing the home directory, for it to save the output in /var/backups with the file name as the date.

    I'm very new to this so I was just hoping I could get pointed in the right direction!

    Cheers.

  2. #2
    Linux Guru Lakshmipathi's Avatar
    Join Date
    Sep 2006
    Location
    3rd rock from sun - Often seen near moon
    Posts
    1,568

    Smile

    Hi Welcome !

    Since you want direction rather than code check out this link for bash shell programming (with list of examples) Advanced Bash-Scripting Guide

    Learning bash will be very useful - Hope this helps

    If you stuck some where with your program,post it here.
    - Lakshmipathi.G
    -------------------
    FOSS India Award winning ext3fs Undelete tool and tutorials www.giis.co.in
    First they criticize you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
    -------------------

  3. #3
    Linux Newbie egan's Avatar
    Join Date
    Feb 2009
    Location
    Mountain View, CA
    Posts
    132
    What exactly do you need direction for? I assume you know how to tar things and move them to other directories? Is it just the argument handling that is new for you?

  4. #4
    Just Joined!
    Join Date
    Jul 2009
    Posts
    3
    Yup the arguements are something im not familar with

  5. #5
    Linux Newbie egan's Avatar
    Join Date
    Feb 2009
    Location
    Mountain View, CA
    Posts
    132
    Quote Originally Posted by nasser00 View Post
    Yup the arguements are something im not familar with
    Well if you name your program 'backup', and you want to call it like: backup /home to backup the home directory, then you will need to use positional arguments. They are referenced as $X where X is a number.

    $0 is the name of the program, i.e. backup
    $1 for you will be the directory to backup...
    $2 would be extra...

    To count the number of arguments (not including $0), you use $#. So you would want to check to make sure that the number of arguments is correct.

    Code:
    if [ -z $1 ]
    then
             echo "Error: Too few arguments"
             echo "Usage -- $0 [dirtobak]"
    fi
    to check if $1 is empty

    Code:
    if [ $# -gt 2 ]
    then
              echo "Error: Too many arguments"
              echo "Usage -- $0 [dirtobak]"
    fi
    to check if there are extra arguments.

    Once you have determined that the arguments were syntactically correct, you have to determine if they are valid. Since you expect $1 to be a directory, you'll want to make sure it isn't anything else.

    Code:
    if [ ! -d $1 ]
    then
             echo "Error: Invalid directory $1"
             echo "Usage -- $0 [dirtobak]"
    fi
    to check if $1 isn't a directory.

    Hopefully this will give you some help...

  6. #6
    Just Joined!
    Join Date
    Jul 2009
    Posts
    3
    Cheers for your help everyone im sure im heading in the right direction now and stuff seems to be coming along!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...