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...
cd /home/phoenix/testarchivebefore the loop or use absolute path while referring to the source directory inside the loop. I thinkcding is better.cd, I must have placed it in the wrong spot (probably before theclearcommand). Thanks!!!