Reputation: 1
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 data
skip_containers
=some directories I am skipping
backup_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
Upvotes: 0
Views: 602
Reputation: 4900
suggesting one line:
tar -zcf "$backup_dir/$file.$(date '+%Y-%m-%d').tar.gz" $(find $src_dir -type d -maxdepth 1 -mindepth 1 -name $skip_containers -prune -o -printf '%f\n' | grep -v "\.$")
Also suggest to investigate rsync
command.
Upvotes: 0
Reputation: 185560
You need to prevent word splitting:
find ...... -print0 |
xargs -0 tar zcf "$backup_dir/$file.tar.gz"
See: https://www.gnu.org/software/bash/manual/html_node/Word-Splitting.html
Upvotes: 0