Howdy,
It sounds like you're mostly there. You need to stage this right and it'll be a breeze. You've got a lot of the logic thought out so let's get started.
First Let's make a scripts folder to run this all from do that with this command:
cd <enter> (this will get you back to your home folder)
mkdir scripts
in this folder I would put a sample copy of your htaccess-open and htaccess-closed. You don't need to name them .htaccess... so you won't loose them.
Now for the script. I picked bash since a lot of what you have is in bash ready.
Code:
#!/bin/bash
#BackupStore.sh
#purpose to close the store and backup the database
echo "Closing up shop"
cat ~/scripts/htaccess-closed > ~/public_html/.htaccess
echo "Sleeping 10 seconds to let connections die"
sleep 10s
mysqldump --opt (DB_NAME) -u (DB_USERNAME) -p(DB_PASSWORD) > /path/to/dbbackup-$(date +%m%d%Y).sql
sleep 3s
cat ~/scripts/htaccess-open > ~/public_html/.htaccess
Run it in cron as sh scriptname.sh or chmod +x it and ./path/to/scriptname.sh to run it.