set up magento in multiple environments

I just started digging in the shop software magento for a project and set up the following development environment.

In this scenario development is done on two local machines while products and pages beeing already edited on the “Live”-Web-Server.
Git is used for version control and deployment. Also FTP SSH.

sync multiple Magento environments

Magento is quite nice when it comes to deployment, as most config stuff is stored in XML files.
Those files are kept locally and ignored from version control.

So this is what my .gitignore looks like:

#cache, sessions, logs
/var/cache/*
/var/session/*
/var/locks/*
/var/report/*
/var/log/*
#media folder - images etc...
/media/*

#local config files
/app/etc/local.xml
.htaccess

#database dump
/database.sql

So i got a copy of the repository in each environment with a local .htaccess and local.xml file.

Theres a few things stored in DB by magento, though.
The most obvious is the “web/secure/base_url” and “web/unsecure/base_url” which both reside in the “core_config_data” table.
Those have to be set to the name of the host you’re working on.

This can be done with a shell script like this:


#create a dupm of the live DB on the server
ssh user@server.com 'mysqldump -u[username] -p[password] > ~/db_dump.sql'

#copy DB dump to local machine
scp user@server.com:~/db_dump.sql ./database.sql

#import SQL
mysql -u[local_mysql_user] -p[local_mysql_pw] [local_db_name] < ./database.sql

#update config settings in the DB
mysql -u[local_mysql_user] -p[local_mysql_pw] [local_db_name]<UPDATE
core_config_data
SET value='http://local.server.com/'
WHERE value ='http://server.com';
EOFMYSQL

This way all CMS and products  can be edited on the server, while the local machines can be easily synched.
For me this works fine, as we are working only in the file system in the local environment.
In case you can’t use the online Server as “database master” this might be not that simple.

As we most likely don’t want the whole /media folder under version control we might want to have another shell script to copy the media files to the local machine (via SCP in this case):


#remove local media folder
rm -rf ./media/*
#fetch media files
scp -r ssh_user@server.com:~/magento_folder/media/* ./media

I guess this could be done more efficiently with maybe ant or something.
But for the moment this works fine for me.

So now i can update my local environment with one line on the console:


git pull; sh sync_db.sh; sh sync_media.sh

One Reply to “set up magento in multiple environments”

  1. This way you can stream your DB to your live server:

    mysqldump -udblocalluser --ignore-table=localdb.core_config_data localdb | ssh sshserver 'mysql50 -Ddbremote -udbremoteuser -pdbremotepass -S/var/lib/mysql50/mysql.sock' 
    

    If its the first sync you need to remove the ignore, but for later syncs and you didn’t changed any system configs you can add it so you don’t have to replace the domain names.

    Enjoy

Comments are closed.