Bash Script: one tar gz archive from each subfolder.

my treasure, missing link between dogs and unicorns, i'll never get used to such a beauty. My actual best friend. Her nose stinks of a mixture of out-of-date parmigiano and various other cheeses.
my Filippa

Hi all, this is a backup tool to be used in for automated or manual backup. What this small script does is creating one tar.gz archive for each subdirectory found starting from a given directory.

It takes just two arguments: where to start from, where to save the new created archives.

The goal is “KEEP IT SIMPLE”, so you can easily adapt this t your systems. You can for instance, hardcode the two args so you can use it for a specific duty (web folders backup /var/www or whatever).

Here you are:

#!/bin/bash
cd $1
for dir in */
do
base=$(basename "$dir")
tar -czf "$2/${base}.tar.gz" "$dir"
echo "compressing $2/${base}.tar.gz $dir";
done

Example. let’s say that you have a webserver whit an usual directory structure for clients, for example:
/var/www/client1
/var/www/client2
/var/www/clientx
and so.
If you wanted a separated tar gz archive for each client subfolder you should use this script this way:
./multitar /var/www /backups

 

Have a nice weekend

Giuseppe

(Visited 1,292 times, 1 visits today)