-
Notifications
You must be signed in to change notification settings - Fork 1
/
resync-drupal-db-for-dev
executable file
·173 lines (153 loc) · 6.59 KB
/
resync-drupal-db-for-dev
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/bin/bash
#############################################################################
# Purpose:
# Refresh a development site from a staging/QA or production site, and then
# devify it (i.e. make it suitable for development).
#
# It performs the following tasks:
#
# 1. Saves a cache-cleared dump of the destination database (DB) as a backup.
# 2. Overwrites the destination DB with the source's.
# 3. Updates the DB schema.
# 4. Imports the latest configuration saved in code (if confirmed).
# 5. Applies pending entity schema updates.
# 6. Disables modules that shouldn't be enabled during development.
# 7. Enables modules that are helpful for development.
# 8. Clears all caches.
# 9. Overwrites the destination's files directory with the source's.
# 10. Produces a one-time login URL for direct access to the admin interface.
# 11. Runs cron.
#
# Developers should be doing all of this every time they refresh their DBs.
# Because it's tricky and time-consuming to do all of these manually, some of
# the steps are often missed. This leads to configuration mismanagement issues
# between development sandboxes and other environments. I'd recommend that this
# script, or another one like it, be run frequently on the authoritative
# development/integration site (Dev) and local development sites to prevent
# such mishaps.
#
# The script tries to be as versatile as possible, working in a variety of
# GNU/Linux environments. If it doesn't work for yours, please submit a merge
# request so that we can get support added.
#
# Usage:
# resync-drupal-db-for-dev <SOURCE_DRUSH_ALIAS> <DESTINATION_DRUSH_ALIAS> [<MODULES_DISABLE>]
#
# Parameters:
# <SOURCE_DRUSH_ALIAS>: The site from which to copy the database. Required.
# <DESTINATION_DRUSH_ALIAS>: The site to which the database should be copied. Required.
# <MODULES_DISABLE>: A list of space-separated modules to disable. Optional.
# Assumptions:
# * The command paths below are correct.
# * Drush is installed locally and remotely (if the source is remote).
# * If you'll want to import it, the latest authoritative configuration is in the configuration directory.
# * If you'll want to synchronize files, the source files directory must be defined in its Drush alias file.
# e.g. $aliases['prod'] = array(
# 'path-aliases' => array(
# '%files' => '/absolute/path/to/files',
# ),
# );
# * Views, Devel, Maillog / Mail Developer, Configuration Update Manager and Admin Toolbar are installed.
# * Sudo access is available to properly set files directory permissions.
# * The following settings should be in your settings.local.php:
# $config['system.logging']['error_level'] = 'verbose';
# $config['system.performance']['css']['preprocess'] = FALSE;
# $config['system.performance']['js']['preprocess'] = FALSE;
# $config['system.file']['path']['temporary'] = '/tmp';
# $config['maillog.settings']['send'] = FALSE;
#
# Enhancements:
# * If you'd like to see database synchronization progress, install the Pipe
# Viewer utility. See https://drupal.org/project/drush_sql_sync_pipe for
# details.
#
# Author: Colan Schwartz - http://colans.net/
# Licence: GPLv3
#############################################################################
# Set command paths.
DRUSH=/usr/local/bin/drush
GZIP=/bin/gzip
CUT=/usr/bin/cut
GREP=/bin/grep
ECHO=/bin/echo
DATE=/bin/date
SUDO=/usr/bin/sudo
CHOWN=/bin/chown
ID=/usr/bin/id
MKDIR=/bin/mkdir
# Make sure that the parameters are specified.
if [ -z "$2" ]; then
$ECHO "Usage: $0 <source-drush-alias> <dest-drush-alias> [<modules-to-disable>]"
exit 1
fi
# Stop executing the script if any command fails.
# See http://stackoverflow.com/a/4346420/442022 for details.
set -e
set -o pipefail
# Set the web user.
if $ID -u "php-fpm" >/dev/null 2>&1
then
# We've got a PHP-FPM user.
USER_WEB=php-fpm
elif $ID -u "nginx" >/dev/null 2>&1
then
# We've got Nginx, but no PHP-FPM user.
USER_WEB=nginx
elif $ID -u "apache" >/dev/null 2>&1
then
# We're using Apache on a non-Debian-based system.
USER_WEB=apache
else
# We're on Debian/Ubuntu or some descendant.
USER_WEB=www-data
fi
# Set variables with defaults and arguments.
SOURCE=$1
DESTINATION=$2
# Set some modules to disable.
MODULES_ACQUIA="acquia_spi acquia_agent"
MODULES_ADVAGG="advagg advagg_js_compress advagg_mod advagg_css_compress advagg_css_cdn advagg_js_cdn advagg_bundler"
MODULES_DISABLE="automated_cron backup_migrate performance securepages $MODULES_ACQUIA $MODULES_ADVAGG $3"
$ECHO "Start time: $($DATE +%T)"
$ECHO "Clear the existing DB's cache to save disk space..."
$DRUSH $DESTINATION cache-rebuild
$ECHO "Saving a backup of the existing destination database..."
$DRUSH $DESTINATION sql-dump --create-db | $GZIP > ~/$DESTINATION.dump.mysql.gz
$ECHO "Sync the source site's database to the destination..."
# Always download and use Drush SQL Sync Pipe instead of Drush's standard
# sql-sync command. It's more efficient, skips cache data by default, and
# actually works on Pantheon sites.
$DRUSH pm-download drush_sql_sync_pipe --destination=$HOME/.drush -y
$DRUSH cache-clear drush
$DRUSH sql-sync-pipe --progress --sanitize --dump $SOURCE $DESTINATION
$ECHO "Updating the database schema..."
$DRUSH $DESTINATION updatedb -y
$ECHO "Potentially importing the latest configuration saved in code..."
$DRUSH $DESTINATION config-import
$ECHO "Applying pending entity schema updates..."
$DRUSH $DESTINATION entity-updates
$ECHO "Disabling modules not meant for development..."
$DRUSH $DESTINATION pm-uninstall -y $MODULES_DISABLE
$ECHO "Enabling extra modules for development..."
$DRUSH $DESTINATION pm-enable -y devel update admin_toolbar views_ui dblog maillog config_update_ui
$ECHO "Clearing the destination site's cache..."
$DRUSH $DESTINATION cache-rebuild
$ECHO "Run any periodic tasks that should be run..."
$DRUSH $DESTINATION cron
read -p "Synchronize all of the destination's files from the source? Your probably don't need this. (y/N)" -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]
then
$ECHO "Updating the public files directory..."
FILES=$($DRUSH dd $DESTINATION:%files)
# Transfer files ownership to the current user for the rsync.
$SUDO $CHOWN -R $USER $FILES
# Perform the transfer of files excluding private and cache files.
$DRUSH -r $FILES rsync --exclude-paths=private:css:js:advagg_js:advagg_css:styles:ctools:xmlsitemap $SOURCE:%files $FILES
# And then set it back to the Web user.
$SUDO $CHOWN -R $USER_WEB $FILES
fi
$ECHO "Get a URL for automatically logging into the admin interface."
$DRUSH $DESTINATION uli
$ECHO "All done!"
$ECHO "End time: $($DATE +%T)"