This repository was archived by the owner on Sep 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathclone_site.sh
More file actions
executable file
·146 lines (131 loc) · 4 KB
/
clone_site.sh
File metadata and controls
executable file
·146 lines (131 loc) · 4 KB
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
#!/usr/bin/env bash
set -e
# The directory this script is in.
REAL_PATH=`readlink -f "${BASH_SOURCE[0]}"`
SCRIPT_DIR=`dirname "$REAL_PATH"`
usage() {
cat $SCRIPT_DIR/README.md |
# Remove ticks and stars.
sed -e "s/[\`|\*]//g"
}
# Parse options.
WEBROOT=$WORKSPACE
DRUSH="drush"
WEBGROUP=
VERBOSE=
GHPRID=
EXTRA_SETTINGS=
HARDLINKS=
DEBUG=
CLONE="table"
while getopts “hH:i:l:d:g:e:cvx” OPTION; do
case $OPTION in
h)
usage
exit
;;
H)
if [[ ! -d $OPTARG ]]; then
echo "The $OPTARG directory does not exist. Please choose a directory that contains an identical copy of the files directory."
exit 1
fi
HARDLINKS="--link-dest=\"$OPTARG\""
;;
i)
GHPRID=$OPTARG
;;
l)
WEBROOT=$OPTARG
;;
d)
DRUSH=$OPTARG
;;
e)
EXTRA_SETTINGS="--extra-settings=\"$OPTARG\""
;;
c)
CLONE="database"
;;
v)
VERBOSE="--verbose"
;;
x)
set -x
DEBUG="--debug"
;;
g)
WEBGROUP=$OPTARG
;;
?)
usage
exit
;;
esac
done
# Remove the switches we parsed above from the arguments.
shift `expr $OPTIND - 1`
# Now, parse arguments.
SOURCE=$1
URL=${2:-http://default}
# If we're missing some of these variables, show the usage and throw an error.
if [[ -z $WEBROOT ]]; then
echo "You must specify a webroot."
exit 1
fi
if [[ -z $SOURCE ]]; then
echo "You must specify a source alias."
exit 1
fi
if [[ -z $GHPRID ]]; then
echo "You must specify a github pull request id."
exit 1
fi
# Put drush in verbose mode, if requested, and include our script dir so we have
# access to our custom drush commands.
DRUSH="$DRUSH $VERBOSE $DEBUG --include=$SCRIPT_DIR"
# The docroot of the new Drupal directory.
DOCROOT=$WEBROOT/$GHPRID
# The base prefix to use for the database tables.
PREFIX="pr_"
# The unique prefix to use for just this pull request.
DB_PREFIX="${PREFIX}${GHPRID}_"
DB_SUFFIX="_${GHPRID}"
# The drush options for the Drupal destination site. Eventually, we could open
# this up to allow users to specify a drush site alias, but for now, we'll just
# manually specify the root and uri options.
DESTINATION="--root=$DOCROOT --uri=$URL"
# Check to make sure drush is working properly, and can access the source site.
$DRUSH $SOURCE status --quiet
if [ "$CLONE" == "database" ]; then
# Copy the existing settings.php to the new site, but add a database name suffix.
$DRUSH $DESTINATION --yes $EXTRA_SETTINGS --database clone-settings-php $SOURCE $DB_SUFFIX
# Copy the database to the new location
$DRUSH $DESTINATION sql-sync --create-db --yes $SOURCE @self
else
# Copy the existing settings.php to the new site, but add a database prefix.
$DRUSH $DESTINATION --yes $EXTRA_SETTINGS clone-settings-php $SOURCE $DB_PREFIX
# Drop all database tables with this prefix first, in case the environment is
# being rebuilt, and new tables were created in the environment.
$DRUSH $SOURCE --yes drop-prefixed-tables $DB_PREFIX
# Copy all the database tables, using the new prefix.
$DRUSH $SOURCE --yes clone-db-prefix $DB_PREFIX $PREFIX
fi
# If we have the registry-rebuild command available, let's go ahead and use it
# first. If modules or classes have changed names or directories, then the
# following drush commands will fail.
if [[ -n `eval $DRUSH $DESTINATION help --pipe | grep registry-rebuild` ]]; then
eval $DRUSH $DESTINATION registry-rebuild
fi
# Now, rsync the files over. If we have a webgroup, set the sticky bit on the
# directory before rsyncing. We then rsync with --no-p, --no-o, and
# --omit-dir-times, to avoid errors. There are dynamically created directories
# we can exclude as well, such as css, js, styles, etc.
if [[ $WEBGROUP ]]; then
DESTINATION_FILES=`$DRUSH $DESTINATION dd files`
mkdir -p -m 2775 "$DESTINATION_FILES"
chgrp $WEBGROUP $DESTINATION_FILES
fi
$DRUSH $DESTINATION -y rsync $HARDLINKS \
$SOURCE:%files @self:%files \
--omit-dir-times --no-p --no-o \
--exclude-paths="css:js:styles:imagecache:ctools:tmp"