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.