| 1 | = How do I copy my Drupal dev site to my Drupal live site? = |
| 2 | |
| 3 | Many of us maintain two different Drupal web sites, one is the live site (in the faq I will refer to that site as www.example.org) and one is a development site (dev.example.org). The development site is used to test out new code or a new design before it is ready to go live. |
| 4 | |
| 5 | At a certain point, when your dev site is ready to go live, you need to copy your dev site to your live. Once you are done, your live site will be identical to your dev site. And, you can now continuing developing your dev site for the next iteration of your site. |
| 6 | |
| 7 | Below are directions for creating a script that will make a backup copy of your live site and copy the dev site to the live site. These direcitons assume a familariaty with [wiki:secure_shell ssh]. |
| 8 | |
| 9 | By creating a script, you can easily build a process that can be repeated in the future. |
| 10 | |
| 11 | You can create the script on your local computer and [wiki:sftp sftp] the script to your server, or if you are comfortable with command line editors like nano or vim, you can create directly from a [wiki:secure_shell ssh] session. |
| 12 | |
| 13 | In this example, let's call the script: sync-dev-to-live. It should be placed in the home directory of the user that controls the live site. |
| 14 | |
| 15 | It should have the following contents: |
| 16 | |
| 17 | {{{ |
| 18 | #!/bin/bash |
| 19 | |
| 20 | ### Change the values below |
| 21 | path_to_live_site="/home/members/MEMBERSHORTNAME/sites/LIVEDOMAIN/web" |
| 22 | path_to_dev_site="/home/members/MEMBERSHORTNAME/sites/DEVDOMAIN/web" |
| 23 | live_site_db_name= |
| 24 | dev_site_db_user= |
| 25 | dev_site_db_pass= |
| 26 | dev_site_db_name= |
| 27 | |
| 28 | ### Optionally change the value below - by default the script will create |
| 29 | ### a directory in the live-user's home directory called sync-staging which |
| 30 | ### will hold temporary copies of the various sites |
| 31 | path_to_sync_staging="~/sync-staging" |
| 32 | |
| 33 | ### Optionally change the path below if your Drupal settings.php file is in a |
| 34 | ### different location |
| 35 | settings_file="sites/default/settings.php" |
| 36 | |
| 37 | ### leave these values commented out UNLESS your dev site is on a different |
| 38 | ### server then your live site |
| 39 | # dev_ssh_login="user@host.mayfirst.org:" |
| 40 | |
| 41 | ### No need to change any other values |
| 42 | |
| 43 | if [ ! -f ~/.my.cnf ]; then |
| 44 | echo "I didn't find a .my.cnf file - which means I won't be able to automatically login to your database. Please create a .my.cnf file with your database details or request help from support.mayfirst.org" |
| 45 | exit |
| 46 | fi |
| 47 | |
| 48 | # remove trailing slash from web variables |
| 49 | path_to_live_site=$(echo $path_to_live_site|sed "s/\\/$//") |
| 50 | path_to_dev_site=$(echo $path_to_dev_site|sed "s/\\/$//") |
| 51 | |
| 52 | live_backup="$path_to_sync_staging/live-backup" |
| 53 | dev_staging="$path_to_sync_staging/dev-staging" |
| 54 | |
| 55 | if [ ! -d "$path_to_sync_staging" ]; then |
| 56 | echo "Creating $path_to_sync_staging" |
| 57 | mkdir "$path_to_sync_staging" |
| 58 | fi |
| 59 | |
| 60 | if [ ! -d "$live-backup" ]; then |
| 61 | echo "Creating $live-backup" |
| 62 | mkdir "$live-backup" |
| 63 | fi |
| 64 | |
| 65 | if [ ! -d "$dev_staging" ]; then |
| 66 | echo "Creating $dev_staging" |
| 67 | mkdir "$dev_staging" |
| 68 | fi |
| 69 | |
| 70 | function interpret_reply() { |
| 71 | if [ "$REPLY" = "y" ] || [ "$REPLY" == "Y" ] || [ -z "$REPLY" ]; then |
| 72 | echo "Executing...." |
| 73 | REPLY=y |
| 74 | else |
| 75 | echo "Not executing" |
| 76 | REPLY=n |
| 77 | fi |
| 78 | } |
| 79 | |
| 80 | read -p "Back up live site files? [Yn] " |
| 81 | interpret_reply |
| 82 | if [ "$REPLY" = "y" ]; then |
| 83 | rsync -av "$path_to_live_site" "$live_backup/" |
| 84 | fi |
| 85 | |
| 86 | read -p "Back up live site database? [Yn] " |
| 87 | interpret_reply |
| 88 | if [ "$REPLY" = "y" ]; then |
| 89 | mysqldump "$live_site_name" > "$live_backup/${live_site_name}.sql" |
| 90 | fi |
| 91 | |
| 92 | read -p "Synchronizing files from dev site to staging area? [Yn] " |
| 93 | interpret_reply |
| 94 | if [ "$REPLY" = "y" ]; then |
| 95 | rsync -av --exclude '/*settings.php' "${path_to_dev_site}" "${path_to_sync_staging}/" |
| 96 | fi |
| 97 | |
| 98 | read -p "Dump dev database to staging area? [Yn] " |
| 99 | interpret_reply |
| 100 | if [ "$REPLY" = "y" ]; then |
| 101 | mysqldump -u "$dev_db_user" -p${dev_db_pass} "$dev_db_name" > "${path_to_sync_staging/${dev_db_name}.sql" |
| 102 | fi |
| 103 | |
| 104 | read -p "Copy live site settings.php to dev staging area? [Yn] " |
| 105 | interpret_reply |
| 106 | if [ "$REPLY" = "y" ]; then |
| 107 | cp "${path_to_live_site}/$settings_file" "${path_to_sync_staging/web/$settings_file" |
| 108 | fi |
| 109 | |
| 110 | echo "Your live site has been backed up and the dev site is staged to be moved" |
| 111 | echo "Please carefully examine $path_to_sync_staging to make sure everything is in place" |
| 112 | read -p "Press any key to make the dev site live or hit ctl-cancel to abort" |
| 113 | |
| 114 | read -p "Delete live web site files? [Yn] " |
| 115 | interpret_reply |
| 116 | if [ "$REPLY" = "y" ]; then |
| 117 | rm -rf $path_to_live_site/* |
| 118 | rm $path_to_live_site/.htaccess |
| 119 | fi |
| 120 | |
| 121 | read -p "Copy in dev files? [Yn] " |
| 122 | interpret_reply |
| 123 | if [ "$REPLY" = "y" ]; then |
| 124 | rsync -av "${path_to_sync_staging}/web/" "$path_to_live_site/" |
| 125 | fi |
| 126 | |
| 127 | read -p "Import database? [Yn] " |
| 128 | interpret_reply |
| 129 | if [ "$REPLY" = "y" ]; then |
| 130 | mysql < "$path_to_sync_staging/${dev_db_name}.sql" |
| 131 | fi |
| 132 | |
| 133 | echo "All done." |
| 134 | }}} |