Find the answer to your Linux question:
Results 1 to 3 of 3
Hello all, I have created a simple Bash script that backs up one of my directories Called Code. The problem that i am having with this script is that it ...
  1. #1
    Just Joined!
    Join Date
    May 2007
    Posts
    1

    Need help with a bash script that does a backup

    Hello all,

    I have created a simple Bash script that backs up one of my directories Called Code.

    The problem that i am having with this script is that it creates the backup directory and informs me that it is created. But after that it produces an error message see below (Script and error message).

    The error message states that the files were backed up but the Tar command stated other wise.

    Could you please take a look at the code and error message and tell me what I am missing.

    ################### Code ##################

    #!/bin/bash
    ## This script uses the tar, mkdir and the cp commands
    ## to create an archive of files and place the files
    ## in an archive directory

    # Scan System for an archive directory
    # if it does not exist, then create the directory

    # Bash variable
    if [ -e $HOME/backups ]
    then
    echo "the Backup Directory exists."
    else
    echo "A Backup directory does not exist, one will be created."
    mkdir $HOME/backups
    echo "The backup directory has been created."
    fi

    # define variables and the location of the files to be archived
    FILES=$HOME/Code

    # Name of the compressed files
    ARcHIVENAME=Code.tgz

    #location of the archive directory
    BACKUPDIR=/home/jamtech/backups

    # Create compressed archive and copy the compressed
    # files to the Archive directory
    tar czf $ARCHIVENAME $FILES
    cp -ap $ARCHIVENAME $BACKUPDIR

    if [ -e $BACKUPDIR/$ARCHIVENAME ]
    then
    echo "The files were backedup!"
    else
    echo " the Files were not backup. DEBUG Backup.sh "
    fi
    ################# End Code #########################

    ***********Error Message

    jamtech@pandorra:~/Code$ bash backup.sh
    the Backup Directory exists.
    tar: Cowardly refusing to create an empty archive
    Try `tar --help' for more information.
    cp: missing destination file
    Try `cp --help' for more information.
    The files were backedup!
    jamtech@pandorra:~/Code$
    **********Error Message

  2. #2
    Linux Newbie jpalfree's Avatar
    Join Date
    Jul 2005
    Location
    Montreal, CA
    Posts
    198
    I think it's in the line after where it says: # Name of the compressed files

    Since you have a lowercase "c" in your declaration: ARcHIVENAME=Code.tgz

    am I right?
    Avatar from xkcd.com, a hilarious computer related webcomic.

  3. #3
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
    Posts
    1,117
    Hi.

    If your eyes are not as sharp as those of jpalfree, you could use features in the shell to help avoid these kinds of problems. One is the "nounset" option. Here's an example:
    Code:
    #!/bin/sh
    
    # @(#) s2       Demonstrate option for catching errors in unset variables.
    
    echo " sh version: $BASH_VERSION"
    
    ARcHIVENAME=Code.tgz
    echo " Working on $ARCHIVENAME"
    
    set -o nounset
    
    echo " Working on $ARCHIVENAME"
    
    exit 0
    Running this in verbose mode shows how it would behave:
    Code:
    % sh -v s2
    #!/bin/sh
    
    # @(#) s2       Demonstrate option for catching errors in unset variables.
    
    echo " sh version: $BASH_VERSION"
     sh version: 2.05b.0(1)-release
    
    ARcHIVENAME=Code.tgz
    echo " Working on $ARCHIVENAME"
     Working on
    
    set -o nounset
    
    echo " Working on $ARCHIVENAME"
    s2: line 12: ARCHIVENAME: unbound variable
    This is useful in the initial development of a script, and a good thing to leave in for important scripts like backups ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

Posting Permissions

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