Click to See Complete Forum and Search --> : Passwords in Bash Script


TheSpeedoBeast
06-13-2005, 10:42 AM
I am in the process of writing my first program ever! (A simple bash script to back up important files to my windows computer from my linux server).

This is my problem: one of the backups I am doing is dumping my mysql tables to my windows partition (via samba). That all works fine and dandy, but when I do that, it prompts me for my mysql table passwords for each table. Is there any way to automatically enter the password in the script, so that I do not have to type my mysql table passwords in four times? Any help here is greatly appreciated!


#!/bin/bash
mount //UPSTAIRS/SharedDocs
cd /var
tar -czf www.tar www/
mv www.tar /mnt/upstairs
cd /home/shook
tar -czf Maildir.tar Maildir/
mv Maildir.tar /mnt/upstairs
mysqldump --opt -p forum > /mnt/upstairs/forum.dump
mysqldump --opt -p wordpress > /mnt/upstairs/wordpress.dump
mysqldump --opt -p gregg > /mnt/upstairs/gregg.dump
mysqldump --opt -p photo > /mnt/upstairs/photo.dump
umount //UPSTAIRS/SharedDocs

TheSpeedoBeast
06-13-2005, 10:49 AM
I just realized that you can just do --password=*password* at the mysqladmin prompt, but I am still curious. If there isn't a command-line modifier that lets you enter the password in the line available, how would you go about setting up the bash script to automatically "type" in a password, etc.?

ph34r
06-13-2005, 10:52 AM
mysql -h hostname -u username -p password

Leave -p there and take out the password and it will prompt you for it.

TheSpeedoBeast
06-13-2005, 11:10 AM
Is there anyway to enter a password without using command line modifiers (like using echo or something related?) such as... if 'apt-get upgrade' prompts you to type 'y' in a script to accept the upgrade, could you add something to the script to make it type 'y' and hit enter for you? Dont know if I am making sense or not here...

Hayl
06-13-2005, 11:22 AM
afaik, there is an apt command line switch that means "yes to all" that you can do. check the man page or --help

TheSpeedoBeast
06-13-2005, 11:29 AM
I know there is an accept all switch for apt, but I was just putting out a hypothetical situation for a hypothetical script.

Maybe all "good" programs have modifiers that will make it so that you never need an external way to input data into prompts in a script. I dunno, I will keep looking into this. It's really hard to word this stuff :)

AdamZ
06-13-2005, 04:37 PM
I've seen this done with a scripting language called (IIRC) "expect". From what I understood of it, you can tell it to expect a certain string as a prompt, and then have it "type" in a certain string in return (can be a variable too, so you can prompt for the password just once instead of hardcoding it into your script).

bwkaz
06-13-2005, 06:40 PM
Don't bother with expect, just use mysqldump in a different mode. ;)

mysqldump -p --databases forum wordpress gregg photo >all-dbs.dump

Or alternately:

mysqldump -p --all-databases >all-dbs.dump

This second version will probably also include the client_test_db and other similar databases; you may not want them.

But this will only prompt you for your password once (because you only invoke mysqldump once).

TheSpeedoBeast
06-14-2005, 12:55 AM
If I use your method then, bkwaz, how do I go about uploading the dumped file back onto the mysql server in the event of a crash?

bwkaz
06-14-2005, 06:52 PM
How would you restore the databases if you used your method (assuming you could get the passwords working)? Do the same thing. ;)

This will restore all 4 databases in one step, rather than requiring 4 steps. It'll probably take just as long, but it'll be less typing. :)