Created
March 15, 2012 07:10
-
-
Save leek/2042674 to your computer and use it in GitHub Desktop.
Rackspace Cloud Sites Migration Utility Script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# @author: Chris Jones <leeked@gmail.com> | |
# @version: 1.1.0 | |
# | |
# Rackspace Cloud Sites only allows running scripts via their web-based "cron" tool. | |
# FTP this script into your Web directory and add it to the scheduled tasks section. | |
# Set the "Command Language" to: perl | |
# Set the "Command to Run" to: migrate.sh refreshdb | |
# | |
CWDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" | |
SOURCE_HOST="255.255.255.255" | |
SOURCE_PORT="2222" | |
SOURCE_USER="root" | |
SOURCE_PATH="/var/www/vhosts/example.com/htdocs" | |
DESTINATION_PATH="$CWDIR/web/content" | |
if [ ! -f .ssh/id_rsa.pub ] | |
then | |
echo "No public-key found, creating one..." | |
cd .ssh | |
ssh-keygen -t rsa -f id_rsa | |
ssh-keyscan -t rsa -p $SOURCE_PORT $SOURCE_HOST >> known_hosts | |
echo "Public-key created, add to ~/.ssh/authorized_keys on $SOURCE_HOST" | |
cat id_rsa.pub | |
fi | |
DESTINATION_DB_ORIG[0]="exampledb_1" | |
DESTINATION_DB_HOST[0]="mysql50-##.wc1" | |
DESTINATION_DB_USER[0]="######_user1" | |
DESTINATION_DB_PASS[0]="abc123" | |
DESTINATION_DB_NAME[0]="######_exampledb_1" | |
DESTINATION_DB_ORIG[1]="exampledb_2" | |
DESTINATION_DB_HOST[1]="mysql50-##.wc1" | |
DESTINATION_DB_USER[1]="######_user2" | |
DESTINATION_DB_PASS[1]="abc123" | |
DESTINATION_DB_NAME[1]="######_exampledb_2" | |
# | |
# Add as many as you need | |
# | |
DESTINATION_DB_ORIG[10]="exampledb_11" | |
DESTINATION_DB_HOST[10]="mysql50-##.wc1" | |
DESTINATION_DB_USER[10]="######_user11" | |
DESTINATION_DB_PASS[10]="abc123" | |
DESTINATION_DB_NAME[10]="######_exampledb_11" | |
for ((i = 0; i < ${#DESTINATION_DB_ORIG[@]}; i++)) | |
do | |
if [ -n "$1" -a "$1" == "refreshdb" ] | |
then | |
echo "Getting a new database dump for: ${DESTINATION_DB_ORIG[$i]}" | |
ssh -p $SOURCE_PORT $SOURCE_USER@$SOURCE_HOST "cd $SOURCE_PATH && \ | |
mkdir -p sql && touch sql/.htaccess && \ | |
echo 'Order deny,allow' > sql/.htaccess && \ | |
echo 'Deny from all' >> sql/.htaccess && \ | |
mysqldump --no-create-db ${DESTINATION_DB_ORIG[$i]} > sql/dump-${DESTINATION_DB_ORIG[$i]}.sql" | |
fi | |
done | |
echo "Running rsync..." | |
rsync -auz -e "ssh -p $SOURCE_PORT" --include=".htaccess" $SOURCE_USER@$SOURCE_HOST:$SOURCE_PATH $DESTINATION_PATH | |
for ((i = 0; i < ${#DESTINATION_DB_ORIG[@]}; i++)) | |
do | |
if [ -f "$DESTINATION_PATH/sql/dump-${DESTINATION_DB_ORIG[$i]}.sql" ] | |
then | |
echo "Loading database dump for: ${DESTINATION_DB_ORIG[$i]}" | |
mysql -h ${DESTINATION_DB_HOST[$i]} -u ${DESTINATION_DB_USER[$i]} --password=${DESTINATION_DB_PASS[$i]} ${DESTINATION_DB_NAME[$i]} < $DESTINATION_PATH/sql/dump-${DESTINATION_DB_ORIG[$i]}.sql | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment