I am trying to make a create a backup bash script from multiple source dir to a different destination dir.
So I used find to search for source dir folder name, save it into variable and use it for tar filenames.
I also pointed tar source dir to $file instead but when I execute the script, it returns an error (Tar: Cannot stat: No such file or directory)
for file in `find $src_dir -type d -maxdepth 1 -mindepth 1 -name $skip_containers -prune -o -printf '%f\n' | grep -v "\.$"`; do
echo "Compressing $file..."
tar zcf "$backup_dir/$file.tar.gz" -T $file
done
Variable used
src_dir=pointing to the origin of my backup dataskip_containers=some directories I am skippingbackup_dir=pointing to a external backup drive (same filesystem as my source)file=for tar filename and also source dir loop
File structure
/path/to/data/
├── dir1
├── dir2
├── dir3
├── dir4
Desirable result is to create tar for each dir and backup into external dir
You need to prevent word splitting:
See: https://www.gnu.org/software/bash/manual/html_node/Word-Splitting.html