0

I've got a sh script to backup a database-server.

#!/bin/bash
mysqldump -u <username> -p<password> --all-databases --single-transaction --opt > /home/backup/h_157_2-1.sql
rsync -zrp --partial /home/backup/h_157_2-1.sql root@<server-ip>:/home/backup/H_157_2/

When I execute those two command on their own in the command line, they work as expected and I get a .sql file with content. But when I execute the script the file only contains this:

Usage: mysqldump [OPTIONS] database [tables]
OR     mysqldump [OPTIONS] --databases [OPTIONS] DB1 [DB2 DB3...]
OR     mysqldump [OPTIONS] --all-databases [OPTIONS]
For more options, use mysqldump --help

I already tried to change up the order of the options or leave out the --opt but the result was still the same. So what could cause the command to not work in the script?

DocRattie
  • 1,392
  • 2
  • 13
  • 27

1 Answers1

0

I couldn't find a way to resolve the initial problem. Even with the help from Elzo Valugi in chat the result was still the same: Excecuted on command line the mysqldump worked finde. Excecuted in the script I got the same msg all the time. (see initial question).

To resolve it I built a workaround to dump every database on it's own.

#!/bin/sh
for dir in /var/lib/mysql/*/; 
do 
  dir=${dir%*/}
  mysqldump -u <user> -p<password> ${dir##*/} --single-transaction --opt > /home/backup/h_157_2_${dir##*/}.sql
  rsync -zrp --partial /home/backup/h_157_2_${dir##*/}.sql root@<server-ip>:/home/backup/H_157_2/
done;

With this I loop through all the directorys in the mysql storage directory, cut them down to the dir-name and use that for the mysqldump. This puts every database in its own file. I think this could be resolved to combine them all to one, but for my needs I'm fine with different files.

DocRattie
  • 1,392
  • 2
  • 13
  • 27