Reputation: 13
I'm having a bit of an issue with my backup script. It uses a for
loop to archive anything within a given directory using tar
. It lists the directories perfectly and creates separate archives for each directory how I'd like them to be, but the variable isn't listing the path to the directory to backup. Can anyone give me an idea of how to make sure the variable is populated with the correct information?
Here's the script:
#!/bin/bash
set -xv
dirs=$(ls /home/phoenix/testarchive)
dest="/home/phoenix/backup/"
archive=".tar.bz2"
clear
echo "Archiving data..."
for dirs in $dirs
do
echo "Archiving $dirs..."
tar cjf "${dest}${dirs}_$(date +%F)${archive}" $dirs
echo "Archive complete!!"
done
echo "All archives created!!"
echo "Test created archive to ensure validity."
ls -lh $dest
And here is my error output:
echo "Archiving data..."
+ echo 'Archiving data...'
Archiving data...
for dirs in $dirs
do
echo "Archiving $dirs..."
tar cjf "${dest}${dirs}_$(date +%F)${archive}" $dirs
echo "Archive complete!!"
done
+ for dirs in $dirs
+ echo 'Archiving folder1...'
Archiving folder1...
++ date +%F
+ tar cjf /home/phoenix/backup/folder1_2017-07-18.tar.bz2 folder1
tar: folder1: Cannot stat: No such file or directory
tar: Exiting with failure status due to previous errors
+ echo 'Archive complete!!'
Archive complete!!
+ for dirs in $dirs
+ echo 'Archiving folder2...'
Archiving folder2...
++ date +%F
+ tar cjf /home/phoenix/backup/folder2_2017-07-18.tar.bz2 folder2
tar: folder2: Cannot stat: No such file or directory
tar: Exiting with failure status due to previous errors
+ echo 'Archive complete!!'
Archive complete!!
echo "All archives created!!"
+ echo 'All archives created!!'
All archives created!!
echo "Test created archive to ensure validity."
+ echo 'Test created archive to ensure validity.'
Test created archive to ensure validity.
ls -lh $dest
+ ls -lh /home/phoenix/backup/
total 8.0K
-rw-r--r-- 1 phoenix phoenix 46 Jul 18 13:48 folder1_2017-07-18.tar.bz2
-rw-r--r-- 1 phoenix phoenix 46 Jul 18 13:48 folder2_2017-07-18.tar.bz2
The scripts, like i mentioned, is naming exactly how I want, but the actual data archive isn't getting created. I'm sure ls
is the issue but I don't know of any other way to get the desired result...
Upvotes: 1
Views: 47
Reputation: 4455
The short answer is that tar is using a relative path. I draw that conclusion from this error message:
tar: folder1: Cannot stat: No such file or directory
Anyway, I suggest you cd to the parent directory. You can probably test my theory just by cd'ing prior to running your script.
cd /home/phoenix/testarchive
You'll have to be careful with the location of the backup files, because they will be put in the current directory, too, unless you put a path in the tar statement.
Upvotes: 0
Reputation: 124646
It's always good to read error messages carefully and understand them:
+ tar cjf /home/phoenix/backup/folder1_2017-07-18.tar.bz2 folder1 tar: folder1: Cannot stat: No such file or directory tar: Exiting with failure status due to previous errors
This means that folder1
could not be found.
Why?
Where is folder1
?
It's in the /home/phoenix/testarchive
directory.
When you execute the script,
is the current working directory in /home/phoenix/testarchive
?
Probably not.
I suggest to write the script like this:
#!/bin/bash
set -xv
dirs_basedir=/home/phoenix/testarchive
dest="/home/phoenix/backup"
archive=".tar.bz2"
clear
echo "Archiving data..."
for path in "$dirs_basedir"/*
do
basedir=${path%/*}
dir=${path##*/}
echo "Archiving $dir in $basedir..."
tar cjf "${dest}/${dirs}_$(date +%F)${archive}" -C "$basedir" "$dir"
echo "Archive complete!!"
done
echo "All archives created!!"
echo "Test created archive to ensure validity."
ls -lh $dest
Although this may look slightly more complicated than your original, it's safer.
Upvotes: 1