forked from UCLALibrary/drupal_admin_scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrupal_get_prod_content
executable file
·117 lines (97 loc) · 4.71 KB
/
drupal_get_prod_content
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
#!/bin/bash
## Refresh the database and static files used by the local Drupal instance (test or stage) with a copy
## of the database and static files used by the production Drupal instances
##
## Order of operations:
## 1. Test ability to use drush to connect to the production Drupal instances
## 2. Copy the production static files to the directory used by local Drupal instance
## 3. Drop the local Drupal instance's database tables to ensure a clean environment to import into
## 4. Copy the production database tables to the database used by the local Drupal instance
## 5. Clear the local Drupal cache (in the database)
##
## NOTE: database and static file operations are performed once, because each
## set of test, stage or production static files and database is shared by each
## set of test, stage or production Webheads
##
# 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"
# Can this value be found somewhere, instead of being hard-coded?
# Webhead Static Files Dir - path is the same on all Webheads
STATIC_FILES="/var/www/vhosts/webhead/sites_default_files/"
# Get local (destination) drush alias
# All drush aliases can be found in /home/$USER/.drush/aliases.drushrc.php
DEST_ALIAS=`$DRUSH site-alias --local`
if [ $? -ne 0 ] ; then
echo -e "Unable to get destination alias for local Drupal instance - exiting \n"
exit 1
fi
# Ensure that (local) destination is RHEL test or stage
# Expected drush alias format is rhel-test[digits], rhel-stage[digits], rhel-prod[digits]
case "$DEST_ALIAS" in
rhel-test*|rhel-stage*)
# Database content will be copied from production to $DEST_ALIAS
SRC_ALIAS="rhel-prod1"
;;
*)
echo -e" Must run this script on RHEL test or stage - exiting \n"
exit 1
;;
esac
# STEP 1 --------------------------------------------------------------------------------------------------------------
echo -e "\nSTEP 1 \n"
# Get $SRC_ALIAS hostname, and test ability of drush command to access $SRC_ALIAS
echo -e "Testing ability to reach $SRC_ALIAS....\n"
SRC_URI=`$DRUSH site-alias --field-labels=0 --fields=uri --format=list @$SRC_ALIAS`
if [ $? -gt 0 ] ; then
echo -e "Unable to reference $SRC_ALIAS - exiting \n"
exit 1
fi
echo -e "Sucessfully connected to $SRC_ALIAS instance. \n"
# STEP 2 --------------------------------------------------------------------------------------------------------------
echo -e "\nSTEP 2 \n"
# Copy Static Files
# Only need to copy once, because the static files are shared by each set of Webheads.
# Rsync options being used:
# -r = recurse into directories
# -l = copy symlinks as symlinks
# -o = preserve owner (2014-10-06 druadmin doesn't own the files anymore)
# -g = preserve group (2014-10-06 druadmin doesn't own the files anymore)
# -t = preserve times (2014-10-06 druadmin doesn't own the files anymore)
# -H = preserve hard links
# -p = preserve permissions (2014-10-06 druadmin doesn't own the files anymore)
# -v = increase verbosity
# --delete = files deleted from source (production) will be deleted on destination ($DOMAIN)
# --safe-links = ignore symlinks that point outside the tree (but there aren't any)
for DIR in $STATIC_FILES; do
echo -e "Copying $DIR used by $SRC_ALIAS to the one used by $DEST_ALIAS ... \n"
sudo -u root /usr/bin/rsync -e 'ssh' -r -l -o -g -t -H -p -v \
--delete --safe-links \
--log-format="%o %b %f%L" \
$USER@$SRC_URI:$DIR $DIR
done
# STEP 3 --------------------------------------------------------------------------------------------------------------
echo -e "\nSTEP 3 \n"
# Drop all tables in the database used by $DEST_ALIAS - this is to ensure a clean local environment
echo -e "Dropping all tables used by $DEST_ALIAS. \n"
$DRUSH @$DEST_ALIAS sql-drop --yes
# STEP 4 --------------------------------------------------------------------------------------------------------------
echo -e "\nSTEP 4 \n"
# Copy the database used by $SRC_ALIAS to the one used by $DEST_ALIAS
echo -e "Copying the database used by $SRC_ALIAS to the one used by $DEST_ALIAS. \n"
$DRUSH sql-sync @$SRC_ALIAS @$DEST_ALIAS --no-cache --yes
# STEP 5 --------------------------------------------------------------------------------------------------------------
echo -e "\nSTEP 5 \n"
# Clear the $DEST_ALIAS Drupal Cache
echo -e "Clearing the Drupal cache for $DEST_ALIAS... \n"
$DRUSH @$DEST_ALIAS cache-clear all
# Drupal refresh is complete
echo -e "Drupal content copy from $SRC_ALIAS to $DEST_ALIAS is complete. \n"