I’ve been having a hell of time trying to find decent server space. I was using space on a friend’s machine, but I realised I needed to have a little more control. Moving to a reseller hosting account with iWeb was the next logical step.
For the past few months I’ve been slowly moving all my sites over the new host. I hate migrating servers! Hate it, hate it, hate it! Today, however, I had enough and decided to investigate various ways of automating (to some degree) this process.
There are heaps of really large scale solutions out there but none to suit my needs. I knew what I needed to do, and I knew everything I needed to do could be achieved from the command line.
Enter the bash script.
- Get a mysqldump of my database
mysqldump --user user_name --password=password database_name > output.sql
- Zip/archive my entire web root directory
tar jcvf web_root.tar.bz *
- Copy the archive over to the new server
scp username@server:~/public_html
- Unzip it into the web root of the new server
tar jxvf web_root.tar.bz
- Import the mysqldump into the new databse
mysql -u -ppassword database < output.sql>
It gets fun when we start running commands on the remote server through SSH…
Confused? Have a look at this bit of code:
ssh username@server "cd /var/www; tar jxvf web_root.tar.bz; mysql -u -ppassword database < output.sql"
It opens a secure shell connection to the remote server and runs some code. Love it.
I’ve compiled all those commands above into one handy shell script. Have a look at the source code below, or click the download link at the bottom of this post.
#!/bin/bash
#host_migrate.sh
# (C) 2004 - darren [at] dontcom [dot] com
# Use this script to migrate from one linux environment to another.
# I used this script when I swapped hosts.
# it does a mysql dump; tars the entire web root up; scp's it to
# the new server; unpacks the files;dumps the .sql file into new database
# dont forget to chmod +rx on this file :-)
# SET VARIABLES HERE
# New server settings
NEW_SERVER='' #new server name ( eg IP|domain name )
NEW_SERVER_DIR='' #web path on new server ( eg /var/www/ )
NEW_SERVER_USER='' #new server username
NEW_SERVER_PASSWORD='' #new server password
NEW_SERVER_DB='' #new server db name
NEW_SERVER_DBA='' #new server db username
NEW_SERVER_DBPASSWORD='' #new server db password
# Old/Current server settings
MYSQL_USER='' #mysql username
MYSQL_PASSWORD='' #mysql password
MYSQL_DBASE='' #mysql database name
OUTPUT_SQL='' #mysql output file
WEB_ROOT_ARCHIVE='' #archive name of all files
# the actual code. - no need to modify this...
mysqldump --user $MYSQL_USER --password=$MYSQL_PASSWORD $MYSQL_DBASE > $OUTPUT_SQL
echo "dumped $MYSQL_DBASE database to $OUTPUT_SQL"
tar jcvf $WEB_ROOT_ARCHIVE *
echo "tared all files in folder to $WEB_ROOT_ARCHIVE"
scp $WEB_ROOT_ARCHIVE $NEW_SERVER_USER@$NEW_SERVER:$NEW_SERVER_DIR
echo "scped $WEB_ROOT_ARCHIVE to $NEW_SERVER_USER"
Download the script here.