Bash script to automate database synchronization with Acquia Dev Cloud database and localhost

I started to use Acquia Dev Cloud to host a Drupal project this week and now i'm needing to review my custom bash scripts to automate some development tasks.
First of them that i'm creating is for a fundamental taks: synchronize remote database with my local database.
Above is the bash script that i created to get the last database backup file created in the Acquia environment. This script will need a improvement in the coming days, because i think that it'll break as i start to use the stage and prod environments, as long as i'm using only the dev right now.
Enjoy.

#!/bin/bash

DRUSH=`which drush`

USER=YOUR_ACQUIA_USER

SERVER=YOUR_ACQUIA_SERVER

BACKUP_PATH=/mnt/files/YOURSITE/backups

FILE_TO_USE=`ssh $USER@$SERVER "ls -lrt $BACKUP_PATH | tail -1 | cut -d' ' -f 8"`

FILE_TO_USE_GUNZIPED=${FILE_TO_USE%.*}

TARGET_PATH=YOUR_LOCAL_DRUPAL_FOLDER

echo "1 - Downloading file $BACKUP_PATH/$FILE_TO_USE from $USER@$SERVER"

scp $USER@$SERVER:$BACKUP_PATH/$FILE_TO_USE $TARGET_PATH

echo "2 - Download completed"

cd $TARGET_PATH

echo "3 - Decompress backup file"

gunzip $FILE_TO_USE

chmod 666 $FILE_TO_USE_GUNZIPED

echo "4 - Starting to clean up target database and import the backup"

for I in `$DRUSH sqlq "SHOW tables"`; do $DRUSH sqlq "DROP TABLE IF EXISTS $I;" && echo "Table $I deleted";done;

echo "5 - Database tables deleted"

echo "6 - Starting to import the backup data"

$DRUSH sqlc < $FILE_TO_USE_GUNZIPED

echo "7 - Backup completed"

cd -

Tags: 

Categoria: