Showing results for 
Search instead for 
Did you mean: 

Backup script

Go to solution
Piotr Kirklewski
Super Advisor

Backup script

Hi there

Im logged as a root, the script is in my home location /home/myuser/myscript

I'm getting this output:

: command not found
: command not found
: command not found
: command not found
'sqlback: line 24: syntax error near unexpected token `do
'sqlback: line 24: ` sed -e "s/\/var\/lib\/mysql\///"`; do

when I'm trying to run this script on my linux machine:


data=`date +%Y-%m-%d`

kat=/home/peterkirklewski/msqlbackup #backup dir

if [ -e $kat ]; then
mkdir $kat


rm -rf /tmp/mysqlback

mkdir /tmp/mysqlback

for var in `find /var/lib/mysql/ -type d | \
sed -e "s/\/var\/lib\/mysql\///"`; do
echo $var
mysqldump -u root --password=$PASS --opt --all $var > $kat/$var.sql done

How to get rid of this problem ?

Jesus is the King
V. Nyga
Honored Contributor

Re: Backup script


I would write some 'echo' commands in your script (at the beginning, several in the middle), so you can see where the ': command not found' message appears.

Also echo your variables you have set.

*** Say 'Thanks' with Kudos ***
James R. Ferguson
Acclaimed Contributor

Re: Backup script


One problem is that you appear to have too many "/" and/or no ";" or newline before the 'done':

for var in `find /var/lib/mysql/ -type d | \
sed -e "s/\/var\/lib\/mysql\//"`; do
echo $var


John Kittel
Trusted Contributor

Re: Backup script

use the shell's trace option. man sh for explanation. For example insert the statement

set -x

as the first line in your script.
Bill Hassell
Honored Contributor

Re: Backup script

In your script:

> mkdir /tmp/mysqlback

There's no test to see if mkdir was successful. If it fails, your script goes on assuming that the directory does indeed exist. Check the return code:

mkdir /tmp/mysqlback
[ $ERR -ne 0 ] && echo "mkdir failed error $ERR" && exit 1

Now this won't fix your problem, but it is a minimum when you are writing production script. Always test that each task is successful (or assume that everything fails until proven successful)

> for var in `find /var/lib/mysql/ -type d | \
> sed -e "s/\/var\/lib\/mysql\///"`; do
> echo $var
> mysqldump -u root --password=$PASS --opt --all $var > $kat/$var.sql done

For reliability, it is important to test each step before moving on. It appears that you are using sed to strip off the leading /var/lib/mysql path and keep what is left. So if that directory has no directories in it, the command fails unexpectedly. The sed expression will return subdirectories too as in:

echo "/var/lib/mysql/a/b/c"| sed -e "s/\/var\/lib\/mysql\///"

This may also be a failure that must be tested.

I would rewrite this so you can see each step:

MYFIND=$(find /var/lib/mysql -type d)
[ "$VAR" = "" ] && echo "\nNo dirs found\n" && exit 2
for VAR in "$MYFIND"
DIR=$(echo $VAR | ed -e "s/\/var\/lib\/mysql\///")
[ "$DIR" = "" ] && echo "\nNull DIR value" && exit 3
echo $DIR
mysqldump -u root --password=$PASS --opt --all $DIR > $kat/$DIR.sql
[ $ERR -ne 0 ] && echo "\nmysqldump failed on DIR=$DIR\n" && exit 4

Now you can see each step when you run it with set -x at the top of the script.

Bill Hassell, sysadmin
Bill Hassell
Honored Contributor

Re: Backup script

Oops, copy+paste dropped "s" from sed:

DIR=$(echo $VAR | sed -e "s/\/var\/lib\/mysql\///")

Bill Hassell, sysadmin
Peter Nikitka
Honored Contributor

Re: Backup script


- There is no need to diddle with backslash-escapes in the sed, just don't use '/' as delimiter.
- If no variables are involved in search/replace - strings I recommend to use single quotes, if needed.
- To overcome the directory-hierarchie-problem, drop the '/' in your result string.
So try:
for var in $(find /var/lib/mysql/ -type d |
sed -e s,/var/lib/mysql/,, -e s,/,-,,)

mfG Peter

The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"