Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 15
I'm just getting into bash scripting, so go easy on me . Nevermind the Dutch, it's only comments, so irrelevant to the script itself. Here's my backup script: Code: #!/bin/sh ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Linux Engineer
    Join Date
    Apr 2005
    Location
    Belgium
    Posts
    1,429

    Simple backup script with some minor hiccups


    I'm just getting into bash scripting, so go easy on me . Nevermind the Dutch, it's only comments, so irrelevant to the script itself. Here's my backup script:
    Code:
    #!/bin/sh
    # Scriptje dat de belangrijkste configuratiebestanden van Linux backupt.
    
    # root-privileges zijn nodig om dit script zijn werk te laten doen!
    
    SRC_DIR1=/etc
    SRC_DIR2=/etc/rc.d
    SRC_DIR3=/boot
    BACKUP_DIR=/home/stijn/backups/Zenwalk
    ROOT_UID=0 # Only users with $UID 0 have root privileges
    E_NOTROOT=67 # Non-root exit error
    E_XCD=66 # Can't change directory?
    
    if [ "$UID" -ne "$ROOT_UID" ]
    then
      echo "This script must be run as root!"
      exit $E_NOTROOT
    fi
    
    cd $BACKUP_DIR || {
      echo "Cannot change to necessary directory." >&2
      exit $E_XCD;
    }
    
    if [ "ls -l" != "totaal 0" ]
    then 
      rm -r *; echo "Clearing backup destination directory..."
    else
      echo "Backup destination directory is clean."
    fi
    
    echo "Creating backup structure and copying source files..."
    
    cd $SRC_DIR1 || {
      echo "Cannot change to necessary directory." >&2
      exit $E_XCD;
    }
    
    mkdir $BACKUP_DIR/etc
    
    cp -pR group fstab netpkg.conf cups sudoers lilo.conf sane.d/plustek.conf profile.d/lang.csh profile.d/lang.sh samba/smb.conf $BACKUP_DIR/etc
    
    cd $SRC_DIR2 || {
      echo "Cannot change to necessary directory." >&2
      exit $E_XCD;
    }
    
    mkdir $BACKUP_DIR/etc/rc.d
    
    cp -pR rc.netfilter rc.M rc.6 rc.modules $BACKUP_DIR/etc/rc.d/
    
    cd $SRC_DIR3 || {
      echo "Cannot change to necessary directory." >&2
      exit $E_XCD;
    }
    
    mkdir $BACKUP_DIR/boot
    
    cp -pR System.map-2.6.12.2-K7-splash vmlinuz-2.6.12.2-K7-splash map initrd.gz initrd $BACKUP_DIR/boot
    
    cd $BACKUP_DIR || {
      echo "Cannot change to necessary directory." >&2
      exit $E_XCD;
    }
    echo "Creating backup archive. The archive will be named Zenwalkbackup$(date +%d.%m.%Y-%H.%M).tar.bz2 and can be restored from /home/stijn/backups/"
    
    tar cjvf Zenwalkbackup$(date +%d.%m.%Y-%H.%M).tar.bz2 *
    
    echo "Moving created backup archive to default directory, and removing the backup's source files..."
    
    mv Zenwalkbackup$(date +%d.%m.%Y-%H.%M).tar.bz2 ..
    
    rm -r *
    
    echo "Backups made successfully. Bailing out."
    
    exit
    This part is one of the first to create problems. As you can notice I clean up the backup directory before and after the backup is made. When I start with a clean dir, this part should make sure I don't get an error message "Unable to remove '*': No such file or directory", and yet, I get it every single time... (There's no need to check for hidden dirs or files, they won't be in there.)
    Code:
    if [ "ls -l" != "totaal 0" ]
    then 
      rm -r *; echo "Clearing backup destination directory..."
    else
      echo "Backup destination directory is clean."
    fi
    On to the next part. When the scripts starts tar to compress the files, tar lets all the files scroll on the screen. I haven't found a 'silent' switch in 'man tar'. Is there any way I can hide this output? I don't need it really.

    Thanks in advance .
    ** Registered Linux User # 393717 and proud of it ** Check out www.zenwalk.org
    ** Zenwalk 2.8 - Xfce 4.4 beta 2- 2.6.17.6 kernel = Slack on steroids! **

  2. #2
    Linux Newbie
    Join Date
    Jul 2005
    Location
    ~/home
    Posts
    105
    you can send all output to /dev/null:
    Code:
    tar zcvf blabla > /dev/null 2>&1
    since your comments are in dutch, you probably will understand this too
    http://wiki.nedlinux.nl/index.php?pa...sh+-+Redirects

    (sorry for you other guys..)
    Help me getting a Opera licence
    Beginning with debian? -> read THIS!

  3. #3
    Linux Engineer
    Join Date
    Apr 2005
    Location
    Belgium
    Posts
    1,429
    Thanks man . Do you have any clue on the first problem I encounter?

    Edit: ok, I see, the first issue can also be solved with redirection . Thanks a lot!
    ** Registered Linux User # 393717 and proud of it ** Check out www.zenwalk.org
    ** Zenwalk 2.8 - Xfce 4.4 beta 2- 2.6.17.6 kernel = Slack on steroids! **

  4. $spacer_open
    $spacer_close
  5. #4
    Linux Newbie
    Join Date
    Jul 2005
    Location
    ~/home
    Posts
    105
    this should do it
    Code:
    if [ "ls -l" != "totaal 0" ]
    then
      rm -rf *
      echo "Clearing backup destination directory..."
    else
      echo "Backup destination directory is clean."
    fi
    i can't explain why the f (force) option to rm -r surpresses the output?

    Code:
    camulos:/home/alain# sh backup
    Clearing backup destination directory...
    Creating backup structure and copying source files...
    Creating backup archive. The archive will be named Zenwalkbackup09.10.2005-20.32.tar.bz2 and can be restored from /home/stijn/backups/
    Moving created backup archive to default directory, and removing the backup's source files...
    Backups made successfully. Bailing out.
    camulos:/home/alain#
    Help me getting a Opera licence
    Beginning with debian? -> read THIS!

  6. #5
    Linux Engineer
    Join Date
    Apr 2005
    Location
    Belgium
    Posts
    1,429
    Yeah, quite bizarre... But the redirection tip you gave me before is kind of a workaround, so...

    Thanks again, now I can see what's best .
    ** Registered Linux User # 393717 and proud of it ** Check out www.zenwalk.org
    ** Zenwalk 2.8 - Xfce 4.4 beta 2- 2.6.17.6 kernel = Slack on steroids! **

  7. #6
    Banned CodeRoot's Avatar
    Join Date
    Sep 2005
    Posts
    567

    Re: Simple backup script with some minor hiccups

    Quote Originally Posted by borromini
    ... this part should make sure I don't get an error message "Unable to remove '*': No such file or directory", and yet, I get it every single time...
    Code:
    if [ "ls -l" != "totaal 0" ]
    then 
      rm -r *; echo "Clearing backup destination directory..."
    else
      echo "Backup destination directory is clean."
    fi
    'totaal' => 'total' ----- unless 'totaal' == (Dutch) 'total' :P ----- I wouldn't know...

    Essentially, [ "ls -l" != "totaal 0" ] will always be true, even if the directory is empty.

  8. #7
    Linux Engineer
    Join Date
    Apr 2005
    Location
    Belgium
    Posts
    1,429
    Yep, 'totaal' means 'total' in Dutch .

    If my statement will always be true, why is that, and is there another way to check if the directory is empty? If yes, how do I do that?
    ** Registered Linux User # 393717 and proud of it ** Check out www.zenwalk.org
    ** Zenwalk 2.8 - Xfce 4.4 beta 2- 2.6.17.6 kernel = Slack on steroids! **

  9. #8
    Banned CodeRoot's Avatar
    Join Date
    Sep 2005
    Posts
    567
    I believe:

    Code:
    if [ "`ls -l`" != "total 0" ]
    then
      rm -r *; echo "Clearing backup destination directory..."
    else
      echo "Backup destination directory is clean."
    fi
    is what you were aiming for...

  10. #9
    Linux Engineer
    Join Date
    Apr 2005
    Location
    Belgium
    Posts
    1,429
    But the output of the shell will be "totaal 0" (it has been localized). So then my original code would be right I presume?
    ** Registered Linux User # 393717 and proud of it ** Check out www.zenwalk.org
    ** Zenwalk 2.8 - Xfce 4.4 beta 2- 2.6.17.6 kernel = Slack on steroids! **

  11. #10
    Banned CodeRoot's Avatar
    Join Date
    Sep 2005
    Posts
    567
    :o
    If I understand you correctly, you are saying that:

    1. 'totaal' really is the correct Dutch spelling.
    2. 'ls -l' executed on an empty directory will return "totaal 0" (because of the system language setting).

    In that case:

    1. I have learned something new and interesting today...

    2. I'm sorry, I was just trying to be amusing (and I guess I was, because the joke is on me, right? ) -- uh, let me change that to:

    Code:
    if [ "`ls -l`" != "totaal 0" ]
    then
      rm -r *; echo "Clearing backup destination directory..."
    else
      echo "Backup destination directory is clean."
    fi
    What is different is the set of ' ` ' characters surrounding the 'ls' command - used as an "execute command" directive.

Page 1 of 2 1 2 LastLast

Posting Permissions

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