Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
for fl in $BUFILES; do
echo -n "compressing previous $fl using gzip... "
if [ -f $fl ]; then
gzip -f -S .gz $fl >/dev/null 2>>/mnt/hdb/bu/bulogerr.txt
if [ "$?" -eq 0 ]; then
rm $fl >/dev/null 2>>/mnt/hdb/bu/bulogerr.txt
echo "sucess"
else
echo "failed: could not gzip $fl"
fi
else
echo "failed: $fl could not be found"
fi
done
for fl in $FILES; do
set -- $fl
echo -n "backup $1 with tar... "
tar cvf $2 $1 >/dev/null 2>>/mnt/hdb/bu/bulogerr.txt
if [ "$?" -eq 0 ]; then
echo "sucess"
else
echo "failed: see bulogerr.txt for details"
fi
done
echo "backup done"
exit 0
the first part where I compress the .tar files works out just fine but the second part where I create the .tar files doesn't work at all.
I get the following output:
start full backup
backup /etc with tar... failed: see bulogerr.txt for details
backup /mnt/hdb/bu/etc_all.tar with tar... failed: see bulogerr.txt for details
backup /var with tar... failed: see bulogerr.txt for details
backup /mnt/hdb/bu/var_all.tar with tar... failed: see bulogerr.txt for details
backup /home with tar... failed: see bulogerr.txt for details
backup /mnt/hdb/bu/home_all.tar with tar... failed: see bulogerr.txt for details
backup /root with tar... failed: see bulogerr.txt for details
backup /mnt/hdb/bu/root_all.tar with tar... failed: see bulogerr.txt for details
backup /usr/local/apache2 with tar... failed: see bulogerr.txt for details
backup /mnt/hdb/bu/apache2_all.tar with tar... failed: see bulogerr.txt for details
backup /usr/local/mysql/var with tar... failed: see bulogerr.txt for details
backup /mnt/hdb/bu/mysqldb_all.tar with tar... failed: see bulogerr.txt for details
backup done
=> Instead of executing the "tar cvf $2 $1 >/dev/null 2>>/mnt/hdb/bu/bulogerr.txt" command with both the variables, it looks like he tries it with each one at a time:
I want: tar cvf /mnt/hdb/bu/etc_all.tar /etc >/dev/null 2>>/mnt/hdb/bu/bulogerr.txt
but all I seem to get is: tar cvf /etc >/dev/null 2>>/mnt/hdb/bu/bulogerr.txt or: tar cvf /mnt/hdb/bu/etc_all.tar >/dev/null 2>>/mnt/hdb/bu/bulogerr.txt
I was looking for 2dim arrays but they look quite impossible in bash...
Thanks if someone can help me out or point me in the right direction here.
You could simplify the problem by creating a map for the 6,
* etc -> /etc
* var -> /var
...
* mysql -> /usr/local/mysql/var
You could just set "etc=/etc" and so on, and get the directory using
Code:
eval dir=$`echo $fl`
Or do the same using symlinks, or maybe bash has some associative arrays.
And then you could have a 1d loop over "etc var home root apache2 mysqldb".
But you asked for a bash solution with a 2D array. You can separate the two values that belong together with a comma, and change the following part of your original script:
Code:
FILES="/etc,/mnt/hdb/bu/etc_all.tar
/var,/mnt/hdb/bu/var_all.tar
/home,/mnt/hdb/bu/home_all.tar
/root,/mnt/hdb/bu/root_all.tar
/usr/local/apache2,/mnt/hdb/bu/apache2_all.tar
/usr/local/mysql/var,/mnt/hdb/bu/mysqldb_all.tar"
for fl in $FILES; do
set -- `echo $fl | tr , \ `
echo -n "backup $1 with tar... "
The "tr" command translates the comma, which isn't special to the shell, back to space, which is. Instead of comma you could use any other character that doesn't occur in FILES.
Ok, so I understand that tr can split a variable using a delimiter you can choose freely.
But I still don't understand why simply set -- $fl didn't work.
I tested it on the shell:
var="abc def"
set -- $var
echo $1 #output: abc
echo $2 #output: def
The command tr "translates", hence the name. E.g., 'tr abc xyz' will replace every occurrence of a with x, of b with y, and of c with z. See 'man tr' for more. So tr doesn't officially "split" a variable -- it's bash's choice to take whitespace as separator (but not, e.g., comma, slash, letters).
The original loop didn't do what you expected because the list $FILES had 12 words, each of which was assigned to fl, one at a time. Both space and newline are whitespace, and equally separate words. By using commas instead of spaces, I changed that to 6 words.
That said, my preferred solution would still avoid 2D arrays:
Code:
etc=/etc
var=/var
home=/home
root=/root
apache2=/usr/local/apache2
mysqldb=/usr/local/mysql/var
for f in etc var home root apache2 mysqldb; do
eval dir=$`echo $f`
echo -n "backup $dir with tar... "
tar cvf /mnt/hdb/bu/${f}_all.tar $dir >/dev/null 2>>/mnt/hdb/bu/bulogerr.txt
if [ "$?" -eq 0 ]; then
echo "sucess"
else
echo "failed: see bulogerr.txt for details"
fi
done
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.