I’m looking to create a deploy script that I can run from a terminal and it automatically deploys my site from a repository. The steps I’ve identified are:
- Connect to remote server via SSH
- Fetch latest version of site from remote repository
- Run any SQL patches
- Clean up and exit
I’ve placed the SSH connection and git pull
commands in my shell file, but what I’m stuck with is MySQL with it being an (interactive?) shell itself. So in my file I have:
#!/bin/bash
# connect to remote server via SSH
ssh $SSH_USER@$SSH_HOST
# update code via Git
git pull origin $GIT_BRANCH
# connect to the database
mysql --user $MYSQL_USER --password=$MYSQL_PASSWORD --database=$MYSQL_DBNAME
# run any database patches
# disconnect from the database
# TODO
exit 0
As you can see, I’m connecting to the database, but not sure how to then execute any MySQL statements.
At the moment, I have a directory containing SQL patches in numerical order. So 1.sql, 2.sql, and so on. Then in my database, I have a table that simply records the last patch to be run. So I’d need to do a SELECT
statement, read the last patch to be ran, and then run any neccesary patches.
- How do I issue the
SELECT
statement to themysql
prompt in my shell script? - Then what would be the normal flow? Close the connection and re-open it, passing a patch file as the input? Or to run all required patches in one connection?
- I assume I’ll be checking the last patch file, and doing a
do
loop for any patches in between?
Help here would be greatly appreciated.