I am writing a simple shell script to automate a simple backup of a website and it's database. This is the working code:
#!/bin/bash
# creates a backup of the mysql & webdata for a specific website.
TODAY=`date +%A`
# these variables cannot contain any spaces and must be modified based on:
# the name of the /automation/folder/ of the application being backed up (application)
# on what server (localhost)
# where the backed up data should be replicated (remotehost)
# the location of the www data for the specific application (webdata)
# /var/www/somefolder
# the username, password and database required to establish the mysql connection (sqluser, sqlpass, sqldb)
# SQLPASS must contain the -p prefix
# example: -pS0m3PassW0rd
APPLICATION=someapp
LOCALHOST=thisserver
REMOTEHOST=otherserver
WEBDATA=location
SQLUSER=usernam
SQLPASS=-ppassword
SQLDB=dbname
# these variables are specific to the application being backed up.
ARCHIVE=/automation/$APPLICATION/$LOCALHOST
ERRORLOG=/automation/$APPLICATION/errorlog.txt
SQLFILE=/automation/$APPLICATION/$APPLICATION.sql
WEBFILE=/automation/$APPLICATION/$APPLICATION.web.tar
SQLGZ=/automation/$APPLICATION/$APPLICATION.sql.gz
WEBGZ=/automation/$APPLICATION/$APPLICATION.web.tar.gz
SQLA=/automation/$APPLICATION/$LOCALHOST/$APPLICATION.sql.gz
WEBA=/automation/$APPLICATION/$LOCALHOST/$APPLICATION.web.tar.gz
rm $SQLFILE
rm $WEBFILE
rm $SQLGZ
rm $WEBGZ
echo "Backup started: " `date` >> $ERRORLOG
mysqldump -u $SQLUSER $SQLPASS $SQLDB > $SQLFILE
tar -cf $WEBFILE $WEBDATA
if [ -s $SQLFILE ];
then
if [ -s $WEBFILE ];
then
gzip $SQLFILE
gzip $WEBFILE
else
echo $SQLFILE was created, $WEBFILE was not, script terminating. >> $ERRORLOG
exit
fi
else
echo $SQLFILE was not created, script terminating. >> $ERRORLOG
exit
fi
if [ -s $SQLGZ ];
then
if [ -s $WEBGZ ];
then
rm $ARCHIVE/*.$TODAY
mv $SQLGZ $SQLA.$TODAY
mv $WEBGZ $WEBA.$TODAY
else
echo $SQLGZ was created, $WEBGZ was not, script terminating. >> $ERRORLOG
exit
fi
else
echo $SQLGZ was not created, script terminating. >> $ERRORLOG
exit
fi
scp $SQLA.$TODAY username@$REMOTEHOST.domain.com:/automation/$APPLICATION/$LOCALHOST
scp $WEBA.$TODAY edcns5al@$REMOTEHOST.domain.com:/automation/$APPLICATION/$LOCALHOST
chown -R root:automation /automation/
chmod -R 770 /automation/
echo Backup completed: `date` >> $ERRORLOG
What I want to do is be able to change the dynamic variables based on an answer file or parameter using something like this from the command line:
backup.sh filename.ans
The filename.ans would contain the variables that I want to set that way I can specify what backup I want to execute without having to maintain multiple copies of the script, each containing their own changes to the variables.
I've tried to use the following code to read the file, but all I can do is cat the file.
for fname in "$@"; do
bash "$fname"
# these variables are specific to the application being backed up.
ARCHIVE=/automation/$APPLICATION/$LOCALHOST
ERRORLOG=/automation/$APPLICATION/errorlog.txt
SQLFILE=/automation/$APPLICATION/$APPLICATION.sql
WEBFILE=/automation/$APPLICATION/$APPLICATION.web.tar
SQLGZ=/automation/$APPLICATION/$APPLICATION.sql.gz
WEBGZ=/automation/$APPLICATION/$APPLICATION.web.tar.gz
SQLA=/automation/$APPLICATION/$LOCALHOST/$APPLICATION.sql.gz
WEBA=/automation/$APPLICATION/$LOCALHOST/$APPLICATION.web.tar.gz
echo $SQLA
done
exit
But the echo returns /automation///.sql.gz
What is the easiest way to load the variables from another file?
This is probably very basic, but unfortunately I don't know the term to search on if I wanted to do what I am trying, so I am coming up empty.