faq/domain/make-dev-site-live: sync-dev-to-live

File sync-dev-to-live, 4.2 KB (added by Daniel Kahn Gillmor, 6 years ago)
Line 
1#!/bin/bash -e
2
3### Change the values below
4path_to_live_site="/home/members/fflic/sites/fflic.org/web"
5path_to_dev_site="/home/members/fflic/sites/dev.fflic.org/web"
6
7## optionally file in the following database info if you want to
8## copy the dev database over the live database
9live_site_db_name=
10dev_site_db_user=
11dev_site_db_pass=
12dev_site_db_name=
13
14### Optionally change the value below - by default the script will create
15### a directory in the live-user's home directory called sync-staging which
16### will hold temporary copies of the various sites
17path_to_sync_staging=~/sync-staging
18
19### Optionally change the path below if you have a read protected settings
20### file on your dev site. If you dev site does not have a read protected
21### settings file, then comment out this line
22#settings_file="sites/default/settings.php"
23
24### No need to change any other values
25
26if [ ! -f ~/.my.cnf ]; then
27        echo "I didn't find a .my.cnf file - which means "
28        echo "I won't be able to automatically login to your "
29        echo "database. Please create a .my.cnf file with your "
30        echo "database details or request help from support.mayfirst.org"
31        exit
32fi
33
34if [ ! -d "$path_to_live_site" ]; then
35        echo "Path to live site ($path_to_live_site) doesn't exist"
36        exit
37fi
38
39if [ ! -d "$path_to_dev_site" ]; then
40        echo "Path to dev site ($path_to_dev_site) doesn't exist"
41        exit
42fi
43
44# remove trailing slash from web variables
45path_to_live_site=$(echo $path_to_live_site|sed "s/\\/$//")
46path_to_dev_site=$(echo $path_to_dev_site|sed "s/\\/$//")
47
48live_backup="$path_to_sync_staging/live-backup"
49dev_staging="$path_to_sync_staging/dev-staging"
50
51if [ ! -d "$path_to_sync_staging" ]; then
52        echo "Creating $path_to_sync_staging"
53        mkdir "$path_to_sync_staging"
54fi
55
56if [ ! -d "$live_backup" ]; then
57        echo "Creating $live_backup"
58        mkdir "$live_backup"
59fi
60
61if [ ! -d "$dev_staging" ]; then
62        echo "Creating $dev_staging"
63        mkdir "$dev_staging"
64fi
65
66function interpret_reply() {
67        if [ "$REPLY" = "y" ] || [ "$REPLY" == "Y" ] || [ -z "$REPLY" ]; then
68                echo "Executing...."
69                REPLY=y
70        else
71                echo "Not executing"
72                REPLY=n
73        fi
74}
75
76echo
77echo "Path to dev site: $path_to_dev_site"
78echo "Path to live site: $path_to_live_site"
79echo "Live backup: $live_backup"
80echo "Dev staging: $dev_staging"
81
82echo
83
84read -p "Back up live site files? [Yn] "
85interpret_reply
86if [ "$REPLY" = "y" ]; then
87        rsync -av "$path_to_live_site" "$live_backup/"
88fi
89
90if [ -n "$live_site_db_name" ]; then
91        read -p "Back up live site database? [Yn] "
92        interpret_reply
93        if [ "$REPLY" = "y" ]; then
94                mysqldump "$live_site_db_name" > "$live_backup/${live_site_db_name}.sql"
95        fi
96fi
97
98echo
99read -p "Synchronizing files from dev site to staging area? [Yn] "
100interpret_reply
101if [ "$REPLY" = "y" ]; then
102        exclude=
103        if [ -n "$settings_file" ]; then
104                exclude="--exclude '*{$settings_file}'"
105        fi
106        rsync -av $exclude "${path_to_dev_site}" "${dev_staging}/"
107fi
108
109if [ -n "$dev_site_db_user" ]; then
110        echo
111        read -p "Dump dev database to staging area? [Yn] "
112        interpret_reply
113        if [ "$REPLY" = "y" ]; then
114                mysqldump -u "$dev_site_db_user" -p${dev_site_db_pass} "$dev_site_db_name" > "${dev_staging}/${dev_site_db_name}.sql"
115        fi
116fi
117
118if [ -n "$settings_file" ]; then
119        echo
120        read -p "Copy live site settings.php to dev staging area? [Yn] "
121        interpret_reply
122        if [ "$REPLY" = "y" ]; then
123                cp "${path_to_live_site}/$settings_file" "${dev_staging}/web/$settings_file"
124        fi
125fi
126
127echo
128echo 
129echo "Your live site has been backed up and the dev site is staged to be moved"
130echo "Please carefully examine:"
131echo  $path_to_sync_staging 
132echo "to make sure everything is in place"
133echo
134read -p "Press any key to make the dev site live or hit ctl-cancel to abort"
135
136echo
137read -p "Delete live web site files? [Yn] "
138interpret_reply
139if [ "$REPLY" = "y" ]; then
140        rm -rf $path_to_live_site/*     
141        rm -f $path_to_live_site/.htaccess
142fi
143
144echo
145read -p "Copy in dev  files? [Yn] "
146interpret_reply
147if [ "$REPLY" = "y" ]; then
148        rsync -av "${dev_staging}/web/" "$path_to_live_site/"
149fi
150
151if [ -n "$dev_site_db_name" ]; then
152        echo
153        read -p "Import database? [Yn] "
154        interpret_reply
155        if [ "$REPLY" = "y" ]; then
156                # drop all tables in the existing database
157                mysqldump  --add-drop-table --no-data "$live_site_db_name" | grep ^DROP | mysql
158                mysql < "$dev_staging/${dev_db_name}.sql"
159        fi
160fi
161
162echo "All done."
163