Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- ###########
- #
- # An import file to bring the latest database backup to the local system for development
- #
- ###########
- BACKUP_DIRECTORY=/home/pbaproduction/db_backups
- REMOTE_USER=readonly
- REMOTE_PASS="zRsVJvG&wP8.XK"
- DOMAIN=performancebeef.com
- # Local Credentials for accessing database
- LOCAL_USER=nishatech
- LOCAL_PASS=nishatech
- LOCAL_DATABASE=feedlot
- # Clears out the local database so the newer version can be imported
- function clearDatabase() {
- mysql --user=${LOCAL_USER} --password=${LOCAL_PASS} 2>&1 << EOF
- DROP DATABASE ${LOCAL_DATABASE};
- CREATE DATABASE ${LOCAL_DATABASE};
- EOF
- }
- # Converts a passed-in timestamp to a more readable format
- function prettifyDate() {
- local months=( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec )
- local YEAR=${1:0:4}
- local MONTH=${months[${1:5:2}]}
- local DAY=${1:7:2}
- DAY=`echo $DAY | sed 's/^0*//'`
- local HOUR=${1:10:2}
- HOUR=`echo $HOUR | sed 's/^0*//'` # Remove leading zeroes
- local MIN=${1:12:2}
- STAMP="${MONTH} ${DAY}"
- local AMPM="AM"
- if [[ "$HOUR" -ge 12 ]]
- then
- AMPM="PM"
- fi
- if [[ "$DAY" == 1* && "$DAY" -ne 1 ]]
- then
- STAMP="${STAMP}th"
- elif [[ "$DAY" == *1 ]]
- then
- STAMP="${STAMP}st"
- elif [[ "$DAY" == *2 ]]
- then
- STAMP="${STAMP}nd"
- elif [[ "$DAY" == *3 || "$DAY" -eq 3 ]]
- then
- STAMP="${STAMP}rd"
- else
- STAMP="${STAMP}th"
- fi
- STAMP="${STAMP}, ${YEAR} at $(( (HOUR + 11) % 12 + 1 )):${MIN} ${AMPM}"
- }
- # Get the latest backup created
- printf "Finding latest backup created on the server...\n"
- BACKUP_FILE=$(ssh ${REMOTE_USER}@${DOMAIN} "ls -1tr ${BACKUP_DIRECTORY}" | tail -n1)
- printf "Done.\n\n"
- DOWNLOAD_FILE="${BACKUP_DIRECTORY}/${BACKUP_FILE}"
- printf "Downloading ${BACKUP_FILE}...\n";
- sftp ${REMOTE_USER}@${DOMAIN}:${DOWNLOAD_FILE} ${BACKUP_FILE}
- printf "Done.\n\n"
- prettifyDate ${BACKUP_FILE:0:14}
- printf "Backup file was made on ${STAMP}.\n\n"
- printf "Clearing current database..."
- clearDatabase
- printf " Done.\n\n"
- printf "Downloading to local database... ";
- mysql --user=${LOCAL_USER} --password=${LOCAL_PASS} ${LOCAL_DATABASE} < ${BACKUP_FILE} 2>&1 | grep -v "Warning: Using a password"
- printf "Done.\n\n"
- printf "Cleaning up local files... "
- /bin/rm ${BACKUP_FILE}
- printf "Done.\n"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement