#!/usr/bin/ruby -w ## clone_prod_to_test : copy files and database from prod site to test site, ## adjusting all configs and database settings so that it is all s ## self contained. Is is Very Important that nothing link back to or ## read from prod. Writing to prod files or db is also not permissible. ## The old test site is moved aside before the operation commences. ## ## Path used are for DreamHost which uses multiple sites per unix user, ## with docroot as the main domain folder. This script is setup for MySQL. ## We adjust configuration for webapps: file paths, domain names, dbname, ## and the data directory and it's path. ## We do not currently update any php overlay src to point to "test." ## ## Copyleft 2009 adric@adric.net CC-BY-SA, or Artistic licence ## a bunch of variables to change the targets @web_user_home = "/home/username" @prod_site_domain = "somedomain.com" @skin_site_domain = "some4otherdomain.com" @prod_site_path = @web_user_home + "/" + @prod_site_domain + "/" @prod_site_data_path= @web_user_home + "/" + @prod_site_domain + "_learn_moodledata/" @test_site_domain = "test.coderpreptest.com" @test_site_path = @web_user_home + "/" + @test_site_domain + "/" @test_site_data_path= @web_user_home + "/" + @test_site_domain + "_learn_moodledata/" @old = @web_user_home + "/" + "old/" + @test_site_domain @db_host = "localhost" @prod_db_name = "database" @db_user = "username" @db_ssssh = "tryharder" @test_db_name = "testing" @mysql_args = "-h #{@db_host} -u #{@db_user} --password=#{@db_ssssh} " @config_lines = [ {:app => "ZenCart", :module => "catalog", :directive => 'DB_DATABASE', :from => @prod_db_name, :to => @test_db_name, :file => "commerce/includes/configure.php" }, {:app => "ZenCart", :module => "catalog", :directive => 'HTTP_SERVER', :from => @prod_site_domain, :to => @test_site_domain, :file => "commerce/includes.php" }, {:app => "ZenCart", :module => "catalog", :directive => 'HTTPS_SERVER', :from => @prod_site_domain, :to => @test_site_domain, :file => "commerce/includes.php" }, {:app => "ZenCart", :module => "catalog", :directive => 'HTTP_SERVER', :from => @skin_site_domain, :to => @test_site_domain, :file => "commerce/includes.php" }, {:app => "ZenCart", :module => "catalog", :directive => 'HTTPS_SERVER', :from => @skin_site_domain, :to => @test_site_domain, :file => "commerce/includes.php" }, {:app => "ZenCart", :module => "catalog", :directive => 'DIR_FS_CATALOG', :from => @prod_site_path.chop, :to => @test_site_path.chop, :file => "commerce/includes/configure.php" }, {:app => "ZenCart", :module => "catalog", :directive => 'DIR_FS_CATALOG', :from => @prod_site_path, :to => @test_site_path, :file => "commerce/includes.php" }, {:app => "ZenCart", :module => "admin", :directive => 'DB_DATABASE', :from => @prod_db_name, :to => @test_db_name, :file => "commerce/admin/includes/configure.php" }, {:app => "ZenCart", :module => "admin", :directive => 'HTTP_CATALOG_SERVER', :from => @prod_site_domain, :to => @test_site_domain, :file => "commerce/admin/includes/configure.php" }, {:app => "ZenCart", :module => "admin", :directive => 'HTTPS_CATALOG_SERVER', :from => @prod_site_domain, :to => @test_site_domain, :file => "commerce/admin/includes/configure.php" }, {:app => "ZenCart", :module => "admin", :directive => 'HTTP_SERVER', :from => @prod_site_domain, :to => @test_site_domain, :file => "commerce/admin/includes/configure.php" }, {:app => "ZenCart", :module => "admin", :directive => 'HTTPS_SERVER', :from => @prod_site_domain, :to => @test_site_domain, :file => "commerce/admin/includes/configure.php" }, {:app => "ZenCart", :module => "admin", :directive => 'HTTP_CATALOG_SERVER', :from => @skin_site_domain, :to => @test_site_domain, :file => "commerce/admin/includes/configure.php" }, {:app => "ZenCart", :module => "admin", :directive => 'HTTPS_CATALOG_SERVER', :from => @skin_site_domain, :to => @test_site_domain, :file => "commerce/admin/includes/configure.php" }, {:app => "ZenCart", :module => "admin", :directive => 'HTTP_SERVER', :from => @skin_site_domain, :to => @test_site_domain, :file => "commerce/admin/includes/configure.php" }, {:app => "ZenCart", :module => "admin", :directive => 'HTTPS_SERVER', :from => @skin_site_domain, :to => @test_site_domain, :file => "commerce/admin/includes/configure.php" }, {:app => "ZenCart", :module => "admin", :directive => 'DIR_FS_CATALOG', :from => @prod_site_path, :to => @test_site_path, :file => "commerce/admin/includes/configure.php" }, {:app => "ZenCart", :module => "admin", :directive => 'DIR_FS_ADMIN', :from => @prod_site_path, :to => @test_site_path, :file => "commerce/admin/includes/configure.php" }, {:app => "Moodle", :module => "config", :directive => '$CFG->wwwroot', :from => @prod_site_domain, :to => @test_site_domain, :file => "learn/config.php" }, {:app => "Moodle", :module => "config", :directive => '$CFG->wwwroot', :from => @skin_site_domain, :to => @test_site_domain, :file => "learn/config.php" }, {:app => "Moodle", :module => "config", :directive => '$CFG->dirroot', :from => @prod_site_path, :to => @test_site_path, :file => "learn/config.php" }, {:app => "Moodle", :module => "config", :directive => '$CFG->dataroot', :from => @prod_site_domain, :to => @test_site_domain, :file => "learn/config.php" } ] ### Move the old test folders aside, dump the old test db, ### Make new folders for the test domain. def clear_the_way `rm -rf #{@old}/#{@test_site_domain}` `rm -rf #{@old}/#{@test_site_domain}_data` `mv #{@test_site_path} #{@old}` `mv #{@test_site__data_path} #{@old}` `mysqldump #{@mysql_args} --databases #{@test_db_name} > #{@old}/test.sql` `mkdir #{@test_site_path}` `mkdir #{@test_site_data_path}` end ### Copy in the htaccess file to protect test site from public access. def htaccess `cp ~/work/configs/test-htaccess #{@test_site_path}.htaccess` end ### Grab a fresh dump of the prod db to file, change the file definitions, ### and load it into the test db slot. def copy_db_over `mysqldump --databases #{@mysql_args} #{@prod_db_name} > #{@test_site_path}clone.sql` `sed -i -e 's/#{@prod_db_name}/#{@test_db_name}/' #{@test_site_path}clone.sql` `mysql #{@mysql_args} #{@test_db_name} < #{@test_site_path}clone.sql` end ### Rsync in a copy of the prod site files, preserving everything we can. ### Run prepared sed on file list to change configuration domain, ### file paths, database to test values. def sync_in_files( config_lines ) `rsync -a #{@prod_site_path} #{@test_site_path}` ## some sed on multiple locations in multiple files ... config_lines.each do |config| ##p "About to sed: " + config[:app] + config[:module] + config[:directive] `sed -i -e '/#{config[:directive]}/s,#{config[:from]},#{config[:to]},' #{@test_site_path}#{config[:file]}` end `rsync -a #{@prod_site_data_path} #{@test_site_data_path}` end clear_the_way htaccess copy_db_over sync_in_files @config_lines