forked from UCLALibrary/drupal_admin_scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrupal_copy_code
executable file
·136 lines (111 loc) · 4.97 KB
/
drupal_copy_code
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
#!/bin/bash
## Copy Drupal code from local Webhead to its fellow Webheads
##
## Order of operations:
## 1. Test ability to use drush to connect to each group of test, stage or production Drupal instances
## 2. Put the local (source) Drupal instance into ready-only maintenance mode
## 3. Rsync Drupal code files from the local Webhead to its fellow Webheads
## 4 Apply any db changes, clear the Drupal cache, and turn maintenance mode off
##
# Must be this user to run this script
# Assume that this user's ~/.ssh/known_hosts on the remote machine already has
# an entry for the local machine
USER=druadmin
if [ "`/usr/bin/id -urn`" != "$USER" ] ; then
echo "You must be the $USER user to execute this script - exiting."
exit 1
fi
# Avoid being in the Drupal root tree to avoid built-in aliases (e.g. @self)
cd $HOME
DRUSH="/usr/local/bin/drush"
# STEP 1 ---------------------------------------------------------------------------------------------------------------------------
echo -e "\nSTEP 1"
# Get local drush alias
# All drush aliases can be found in /home/$USER/.drush/aliases.drushrc.php
SRC_ALIAS=`$DRUSH site-alias --local`
if [ $? -ne 0 ] ; then
echo -e "\nUnable to get source alias for local Drupal instance - exiting."
exit 1
fi
# Expected drush alias format is rhel-test[digits], rhel-stage[digits], rhel-prod[digits]
BASE_ALIAS=`echo $SRC_ALIAS | sed 's/[0-9]*$//'`
echo -e "\nCollecting variables from all $BASE_ALIAS Drupal instances."
# Get drush aliases for fellow sites
DEST_ALIASES=`$DRUSH site-alias | grep -v $SRC_ALIAS | grep $BASE_ALIAS`
if [ -z "$DEST_ALIASES" ] ; then
echo -e "\nUnable to get destination aliases - exiting."
exit 1
fi
# Get URIS for local and fellow sites
SRC_URI="`$DRUSH site-alias --field-labels=0 --fields=uri --format=list @$SRC_ALIAS`"
if [ -z "$SRC_URI" ] ; then
echo -e "\nNo source webheads found - exiting."
exit 1
fi
DEST_URIS=""
for ALIAS in $DEST_ALIASES ; do
DEST_URIS="$DEST_URIS `$DRUSH site-alias --field-labels=0 --fields=uri --format=list @$ALIAS`"
done
if [ -z "$DEST_URIS" ] ; then
echo -e "\nNo destination webheads found - exiting."
exit 1
fi
# Determine Drupal Code directory: it is the same on source and all destinations
# Ask source for its Drupal root directory
SRC_ROOT_DIR=`$DRUSH site-alias --field-labels=0 --fields=root --format=list @$SRC_ALIAS 2>/dev/null`
if [ $? -ne 0 ] ; then
echo -e "\nUnable to determine $SRC_ALIAS root directory - exiting."
exit 1
fi
# Drupal code directory is one level above
DRUPAL_CODE_DIR=`dirname $SRC_ROOT_DIR 2>/dev/null`
if [ $? -ne 0 ] ; then
echo -e "\nUnable to determine $SRC_ALIAS code directory - exiting."
exit 1
fi
echo -e "\nSucessfully connected to $SRC_ALIAS and $DEST_ALIASES Drupal instances."
# STEP 2 ---------------------------------------------------------------------------------------------------------------------------
echo -e "\n\nSTEP 2"
# Put local Drupal instance into maintenance (read-only) mode
echo -e "\nPutting the $SRC_ALIAS Drupal instance into maintenance mode."
$DRUSH @$SRC_ALIAS vset maintenance_mode 1
# STEP 3 ---------------------------------------------------------------------------------------------------------------------------
echo -e "\n\nSTEP 3"
# Copy the Drupal code files from here to $DEST_URIS
# Rsync options being used:
# -c = decision to skip is based on checksum, not on (default) modification time & size
# -r = recurse into directories
# -l = copy symlinks as symlinks
# -g = preserve group ($USER owns the files)
# -o = preserve owner ($USER owns the files)
# -p = preserve permissions ($USER owns the files)
# -t = preserve times ($USER owns the files)
# -H = preserve hard links
# -v = increase verbosity
# --delete = delete extraneous files from dest dirs - for example if need to remove obsolete code/modules
# --safe-links = ignore symlinks that point outside the tree - don't have examples of any symlinks
# --temp-dir = create temporary files in directory DIR
# Appending a slash to the source directory name causes rsync to copy the contents
# Otherwise, rsync would copy the directory itself
for DEST_URI in $DEST_URIS; do
echo -e "\nCopying Drupal code files from here to $DEST_URI."
rsync -e 'ssh' -c -r -l -g -o -p -t -H -v \
--delete --safe-links --temp-dir=/tmp \
$DRUPAL_CODE_DIR/ $DEST_URI:$DRUPAL_CODE_DIR
done
# STEP 4 ---------------------------------------------------------------------------------------------------------------------------
echo -e "\n\nSTEP 4"
# Apply any database changes.
echo -e "\nApplying any database changes."
$DRUSH @$SRC_ALIAS -y updatedb
# Clear the cache
echo -e "\nClearing the Drupal cache."
$DRUSH @$SRC_ALIAS -y cache-clear all
# Turn maintenance mode off
echo -e "\nTurning maintenance mode off."
$DRUSH @$SRC_ALIAS vset maintenance_mode 0
# Clear the cache again to be sure maintenance mode is not preserved
echo -e "\nClearing the Drupal cache."
$DRUSH @$SRC_ALIAS -y cache-clear all
# All done
echo -e "\nCopying of code from here to$DEST_URIS is complete."