Can I create a configuration file for a shell script?

Solution 1:

Create a file (e.g. settings) with your settings as variables:

username='foo'
pass='bar'
website='baz'
database='yak'

Then source the file from your script and you will be able to access the variables:

. settings
echo $username     # <- this will print "foo"

Concerning your second "question", if you have multiple hosts you can just have multiple config files and source the one passed as an argument to your script. For example, create a config file called site1.

Call your script with ./backup.sh site1

. $1               # <- this will load the file "site1"
echo $username     # <- this will print "foo"

With your backup script, this would mean:

#!/bin/bash
. $1
ssh [email protected] "tar cjvf webfilesbackup-date-`date +%Y%m%d`.tar.bz2 public_html/"
ssh [email protected] "mysqldump -u $user -p$pass $database > databasebackup-`date +%Y%m%d`-db.sql"
# ... and so on