1

How would I go about compressing a list of directories into separate archives? I have read solutions that just archive all the directories within a given dir, but there are some I want to skip. Perhaps an easier way would be to define the directories I DO NOT want to archive.

This is for backup purposes so if incremental archiving can be easily implemented that would be a huge plus. Unfortunately rsync is not an option for me.

Alternatively a script such as AutoMySQLBackup but for directories would be a great pay dirt for me.

bskool
  • 151
  • 1
  • 1
  • 8
  • You've tagged tar, does its `--exclude` flag not accomplish what you want? – Joe Sep 01 '15 at 08:06
  • 1
    @Joe, if you look at the answer I posted below, --exclude would not work for this. Though using it the way I assume you meant, it would not create separate files for each directory. But one archive with the defined directories excluded. Unfortunately it does not accomplish what i want. – bskool Sep 01 '15 at 09:41

1 Answers1

1

I came up with this. It looks inside a defined directory, archives all the directories within to their own separate files and gives them a name based off the source directory name & date of script execution.

#!/bin/bash

#START
TIME=`date +%Y-%m-%d_%Hh%Mm`     # Append date and time to backup file
SRCDIR=/srv                      # Location of directories to backup
DESDIR=/srv/backup               # Destination of backup files
EXCLUDE=exclude.txt  # File which defines what to exclude from archiving

for dir in $SRCDIR/*/
do
  base=$(basename "$dir")
  tar -cpzf $DESDIR/$base-$TIME.tar.gz $dir
done
#END

The problem with this? Unless the destination directory is outside the source directory, it will keep archiving the backup folder where the previous archive/s was/were already created; as the $EXCLUDE variable is not being used.

bskool
  • 151
  • 1
  • 1
  • 8
  • The question was eventually resolved here: http://unix.stackexchange.com/questions/227868/how-to-exclude-directories-from-tar-in-this-script – Gerald Schneider Sep 07 '15 at 05:46