Grouping array elements - possible?

I have a script which takes backup of some configuration files on my server. It does that by using an array which contains the complete path to the files to backup.

It copys the files to a pre defined dir. Each "program" has it's own folder, ex. apache.conf is being copied to /predefined dir/apache/. The script uses the filename without it's extension to create the folder (apache.conf becomes apache - php.ini becomes php etc)

However, some filenames don't allow this method to work as they're called "log.conf" or things like that.

The goal is to be able to add files to the array in the script, without having to add alot of exceptions for files without a usable name.

My question is - are there any way to add a "group" to each filepath? like apache.conf is in "apache" group, php.ini and php_browscap.ini is in "php" group etc.? Or are there a better way to do it?

This is my code. Please bear with me if something looks wierd, I'm YABN (Yet Another Bashscript Newbie) :smiley:

files=(
'/php.ini'
'/php_browscap.ini'
'/mnt/HDA_ROOT/.config/apache/apache.conf'
)

dest='/share/MD0_DATA/Backup/servers/digisnas/'
fcount=${#files[@]}
ip=$(ip addr show eth0 | grep inet | awk '{print $2;}')
datetime=$(date +"%d-%m-%Y %T")
date=$(date +"%d-%m-%Y")
host=$(hostname)

normal="\033[m"
green="\033[32m"
boldGreen="\033[1;32m"

initmsg="Initiating"
backupmsg="Backing"
dots="..."

echo -e "\n$boldGreen$initmsg backup of $fcount files...$normal\n"
for i in "${files[@]}"
do
nline=${i##*/}
aparm=${nline%.*}

if [ -d "$dest$aparm" ]; then
        if [ ! -d "$dest$aparm/$date" ]; then
                mkdir -m 775 "$dest$aparm/$date"
        fi
elif [ ! -d "$dest$aparm" ]; then
        mkdir -m 775 "$dest$aparm"
        mkdir -m 775 "$dest$aparm/$date"
fi
echo -n "$backupmsg up $i$dots"
cp -p $i $dest$aparm/$date
echo -e "Backup date: $datetime
Backup source: $i
Backup destination: $dest
Backup made by: $host $ip" > $dest$aparm/$date/$aparm$date.log
echo -e " [$green OK$normal ]"
done

bump /tooshort

Bumping up posts or double posting is not permitted in these forums.

Please read the rules, which you agreed to when you registered, if you have not already done so.

You may receive an infraction for this. If so, don't worry, just try to follow the rules more carefully. The infraction will expire in the near future

Thank You.

The UNIX and Linux Forums.

Renaming schemes to prevent files from clashing can get pretty complicated. Some ones I actually see used are things like _cfg0000_log.conf, etc.

You could keep the absolute path, changing / into # or somesuch, so they all fit in one array.

But all of this seems pretty complicated for no benefit... I would just create a tarball. It can hold all the original file names, paths, permissions, owners, and timestamps without fuss.

tar -cpf "${dest}/${date}-backup.tar" "${files[@]}"

Yeah, but I have another script which I use for restoring the files again. If I use tarball, that complicates the behavior of the restore script. Also, I'm also backing up whole folders in some cases (new addition), but in some cases I wouldn't be restoring the whole folder, but only a few files from it.

I would think tar simplifies all of these... You can specify whole folders or individual files both on creation and extraction. Maybe doesn't fit into your original script, via replacing the vast majority of it!

I don't mind rewriting it if it solves the problem.

You mentioned tar could hold the original path. Could you please elaborate? How would that work?

Let me illustrate:

$ # Create a tar file with an absolute path to a folder,
$ # and an absolute path to a file
$ tar -cpf example.tar /etc/openvpn /etc/passwd

$ # list everything in the tar
$ tar -tf example.tar
etc/openvpn/
etc/openvpn/.keep
etc/openvpn/openvpn.conf
etc/openvpn/.keep_net-misc_openvpn-0
etc/openvpn/openvpn-status.log
etc/openvpn/ipp.txt
etc/openvpn/ccd/
etc/openvpn/ccd/tech02
etc/openvpn/ccd/nathan
etc/openvpn/ccd/cherylynn
etc/passwd

$

...so it remembers not just the file name and contents, but the path it came from, and the ownership of each folder along the way. Two different files named the same thing will not conflict as long as you use absolute paths.

Note that my version of tar, GNU tar, removes the first / from absolute paths, but not all of them do!

tar even lets you extract individual files via tar -xpf /path/to/file.tar -C / path/and/filename1 just/a/path ...

The -p tells it to be more complete about remembering and restoring file permission and ownership attributes. That's not too relevant when storing or extracting source code, but for backups is useful.

The -C tells it to change directory into / before extracting, probably a good idea if it contains absolute path names.