Find the answer to your Linux question:
Results 1 to 3 of 3
Hello This may be a simple thing to do but I'm a little stuck ! I have the following directory structure www/ www/folder1 www/folder2 www/folder3 Each folder (1,2 and 3) ...
  1. #1
    Just Joined!
    Join Date
    May 2006
    Posts
    10

    Recursively Compress Directories for Backup

    Hello

    This may be a simple thing to do but I'm a little stuck !

    I have the following directory structure

    www/

    www/folder1
    www/folder2
    www/folder3

    Each folder (1,2 and 3) has many subfolders and file within.

    I want to be able run a shell script that goes through the www folder and makes compressed tar archives of each folder with that foldername ready for backup offsite

    So the result would be

    www/folder1.tar.gz
    www/folder2.tar.gz
    www/folder3.tar.gz

    The original folders would be left intact and untouched as they are used by others.

    I know how to do one folder at a time but not all the diretories from one script.

    Thanks in advance

  2. #2
    Linux Newbie
    Join Date
    Sep 2005
    Location
    CZ
    Posts
    164

    Talking

    Perhaps this could be of some use:

    Code:
    #! /bin/bash
    
    for D in www/*
    do
      if [ -d "$D" ]
      then
        echo "Directory $D"
        tar -cvjf "`dirname "$D"`/`basename "$D"`.tar.bz2" "$D"
      else
        echo "Not a directory $D"
      fi
    done
    For names beginning with "." you have to find an alternative though

  3. #3
    Just Joined!
    Join Date
    Jun 2005
    Posts
    2
    A nice script. Just replace the www/* with `find .` to see hidden files, directories, etc.

    Try this one also ~~
    Code:
    #!/bin/bash
    DIR=/var/www
    LS=`which ls`
    
    #cd $DIR
    
    echo $LS | bash |
    while read x;
    do 
    	if [ -d "$x" ]
    		then
    			echo "backing up $x..."
    			tar -cjf "$x".tar.gz "$x"
    	fi
    done
    This will work for folders with spaces in them too!

Posting Permissions

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