1

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...

2
  • You either need to cd /home/phoenix/testarchive before the loop or use absolute path while referring to the source directory inside the loop. I think cding is better. Commented Jul 18, 2017 at 18:27
  • This also works too! While I tested using cd, I must have placed it in the wrong spot (probably before the clear command). Thanks!!! Commented Jul 19, 2017 at 13:53

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I did see the error, I just wasn't sure of how to go about getting to the $dir variable to reflect the relative path. The dirs_basedir variable addition you put added worked like a charm!!! Thanks @janos :)
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.