Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have as script that dumps a mysql database, and compresses the file. What I want to do is have another (edit) file which can change the username, password and database name. Then somehow connecting that file to this script.

In another file I want something that says for example.

username =  "username"
password = "password"
database = c2duo_db

Then in a script name mysqlbackup.sh some import the username password and database from the file.

#!/bin/sh

mysqldump -u"username" -p"password" --opt c2duo_db > /home/sh/c2duo_db-`date +%d%m%Y`.sql

cd /home/sh
tar -zcvf c2duo_db-`date +%d%m%Y`.tar.gz  *.sql
share|improve this question

2 Answers 2

up vote 5 down vote accepted

You can do this easily using the source command to import the contents of another file into the current shell environment (in your case your script) and run it there.

In db.conf:

username="username"
password="password"
database="database"

In your script:

#!/bin/sh

source /path/to/db.conf
cd /home/sh
dumpfile_name=$database-$(date +%d%m%Y).sql

mysqldump -u"$username" -p"$password" --opt "$database" > $dumpfile_name
gzip $dumpfile_name

Notes:

  • You should never put a space after the = assignment operator when assigning shell variables!
  • I did a couple things to cleanup your code. I didn't add the new dump to a single tar file like you were doing. I did show how you can compress an individual file. If you wanted to tar it so they were all in one archive you can do that too, but I find having individual compressed dump files quite useful.
  • I moved the cd operation to before the mysqldump command so that you wouldn't have to specify the path for the output file since that is the current directory. Just saves duplicated code.

Edit: Even with that step done, it seems like this is a half-baked solution to me. You might be interested in how you can pass values using arguments. For example you could take the hard coded path to the config file out of the script above and replace it with this:

#!/bin/sh

source "$1"
cd /home/sh
[...] # rest of script as above

You could then execute it like ./mysqlbackup.sh /path/to/db.conf. You could even take this a step farther and just write it all in one script and provide all three values as arguments:

#!/bin/sh

username="$1"
password="$2"
database="$3"
cd /home/sh
[...] # rest of script as above

...and call it like ./mysqlbackup.sh username password database.

share|improve this answer
    
This is exactly what I am looking for. Thanks –  Shehzad009 Jul 25 '11 at 10:42

You could change the first file to:

s/username/joe/
s/password/secret/
s/database/c2duo_db/g

store it as db.sed, and call it with

sed -f db.sed mysqlbackup.sh > joeSqlbackup.sh

For different users/passwords and databases you would generate different sed-scripts.

share|improve this answer

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.