Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I am running a mysqldump via a bash script and have encountered a problem with a password that contains special characters.

mysqldump -hlocalhost -uUSERNAME -pPA$$W0RD DATABASE | 
                                gzip > /home/USERNAME/backups-mysql/BACKUP.gz

How do I escape the password?

share|improve this question

2 Answers 2

up vote 5 down vote accepted

Even better, don't put the username or password on the command line where it can be seen by anyone who can do ps -ef.

Create a control file named something like /etc/mysqldump.cnf:

[client]
user=root
password=YOUR_MYSQL_ROOT_PASSWORD

...where client is literal (not the name of your server) and YOUR_MYSQL_ROOT_PASSWORD is... well... your mysql root password.

Then invoke like this:

mysqldump --defaults-file=/etc/mysqldump.cnf DATABASE |                                 gzip > /home/USERNAME/backups-mysql/BACKUP.gz

Personally my invocation is more like this:

#!/bin/bash
NOW=`perl -e 'print time;'`
cd /opt/backup/mysql
mkdir $NOW
for i in `echo "show databases" | mysql -u root --password="MySqlRootPassword" | grep -v Database`; do
        mysqldump --defaults-file=/etc/mysqldump.cnf --databases $i --opt > $NOW/$i.dump
one
tar cfpz $NOW.tgz $NOW
du -sh $NOW $NOW/* > $NOW.report
rm -rf $NOW

I've never figured out how to use a similar --defaults-file parameter for mysql, however since this command runs fairly quickly the risk of exposure is much lower... although really that should be fixed.

share|improve this answer
    
brilliant answer, thank you –  psynnott Mar 27 '10 at 17:21
    
For something similar to using --defualts-file for mysql, see serverfault.com/a/313965/53106 –  Series8217 Dec 28 '11 at 21:41
mysqldump -hlocalhost -uUSERNAME -p'PA$$W0RD' DATABASE | 
                                gzip > /home/USERNAME/backups-mysql/BACKUP.gz

It works?

share|improve this answer
    
Yes, this worked. –  psynnott Mar 25 '10 at 11:44

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.