1 |
71d02f8c
|
Julien Enselme
|
#!/usr/bin/env bash
|
2 |
|
|
|
3 |
15ed27d8
|
Julien Enselme
|
usage() {
|
4 |
d3e12e0b
|
Julien Enselme
|
help="This script is intended to ease the synchronisation between any site hosted by assos.
|
5 |
71d02f8c
|
Julien Enselme
|
Typically, this script is usefull when you have (or want to have) a test site based on
|
6 |
|
|
a already working site. It relies on bash, drush and drush aliases.
|
7 |
|
|
|
8 |
f4bff92c
|
Julien Enselme
|
Before synching the site, the destination site's database is backuped. If the
|
9 |
15ed27d8
|
Julien Enselme
|
destination site does not exist, it is created. If prod is set, the site is not in
|
10 |
|
|
maintenance mode.
|
11 |
71d02f8c
|
Julien Enselme
|
|
12 |
284ceaad
|
Florent Torregrosa
|
usage: d7-sync-site.sh -s SOURCE_SITENAME -d DEST_SITENAME [-p]
|
13 |
|
|
example: d7-sync-site.sh -s default -d clubdrupal [-p]"
|
14 |
15ed27d8
|
Julien Enselme
|
echo -e "${help}"
|
15 |
|
|
}
|
16 |
|
|
|
17 |
|
|
source_site_name=''
|
18 |
|
|
dest_site_name=''
|
19 |
|
|
prod=false
|
20 |
|
|
while getopts "hs:d:p" opt; do
|
21 |
|
|
case "${opt}" in
|
22 |
|
|
s)
|
23 |
|
|
source_site_name="${OPTARG}";;
|
24 |
|
|
d)
|
25 |
|
|
dest_site_name="${OPTARG}";;
|
26 |
|
|
p)
|
27 |
|
|
prod=true;;
|
28 |
|
|
h)
|
29 |
|
|
usage; exit 0;;
|
30 |
|
|
:)
|
31 |
|
|
echo "Option -$OPTARG requires an argument." >&2
|
32 |
|
|
usage >&2; exit 1;;
|
33 |
|
|
\?)
|
34 |
|
|
usage >&2; exit 1;;
|
35 |
|
|
esac
|
36 |
|
|
done
|
37 |
|
|
shift $((OPTIND-1))
|
38 |
|
|
|
39 |
|
|
if [ -z "${source_site_name}" ] || [ -z "${dest_site_name}" ]; then
|
40 |
|
|
echo "At least a required parameter is missing." >&2
|
41 |
|
|
usage >&2
|
42 |
|
|
exit 1
|
43 |
|
|
fi
|
44 |
|
|
|
45 |
71d02f8c
|
Julien Enselme
|
|
46 |
06e9d071
|
Assos Assos
|
. /home/assos/bin/scripts-config.sh
|
47 |
|
|
. /home/assos/bin/scripts-utils.sh
|
48 |
71d02f8c
|
Julien Enselme
|
|
49 |
42f1f46f
|
Julien Enselme
|
|
50 |
71d02f8c
|
Julien Enselme
|
# Create site if necessary
|
51 |
15ed27d8
|
Julien Enselme
|
if ! site_exists "${dest_site_name}" > /dev/null ; then
|
52 |
|
|
echo "${dest_site_name} does not exit. We will create it"
|
53 |
b02bac9b
|
Julien Enselme
|
d7-create-site.sh -s "${dest_site_name}" -m "will@be.erased" -p "willbeerased" -d
|
54 |
c9d0efbc
|
Julien Enselme
|
# if the site is new, there is no database
|
55 |
|
|
new_site=1
|
56 |
71d02f8c
|
Julien Enselme
|
fi
|
57 |
|
|
|
58 |
42f1f46f
|
Julien Enselme
|
|
59 |
71d02f8c
|
Julien Enselme
|
# Backup the database of SOURCE_SITE
|
60 |
8550e097
|
Julien Enselme
|
current_date=$(date "+%Y-%m-%d-%Hh%Mm%Ss")
|
61 |
15ed27d8
|
Julien Enselme
|
if [ "${source_site_name}" = "default" ] ; then
|
62 |
|
|
dir="${dest_site_name}"
|
63 |
71d02f8c
|
Julien Enselme
|
else
|
64 |
15ed27d8
|
Julien Enselme
|
dir="assos.centrale-marseille.fr.${dest_site_name}"
|
65 |
c9d0efbc
|
Julien Enselme
|
fi
|
66 |
3e9236bd
|
Julien Enselme
|
if [ -z "${new_site}" ] ; then
|
67 |
2a0f9a31
|
Florent Torregrosa
|
drush -y "@${source_site_name}" sql-dump --result-file="${d7_dir_individual_manual_backup}/${dir}/${current_date}.${dir}.sql" --gzip --structure-tables-key=common-d7
|
68 |
71d02f8c
|
Julien Enselme
|
fi
|
69 |
|
|
|
70 |
42f1f46f
|
Julien Enselme
|
|
71 |
71d02f8c
|
Julien Enselme
|
# Sync files
|
72 |
b39d5907
|
Julien Enselme
|
drush -y rsync --delete --exclude="*.php" "@${source_site_name}:%site" "@${dest_site_name}:%site"
|
73 |
71d02f8c
|
Julien Enselme
|
|
74 |
42f1f46f
|
Julien Enselme
|
|
75 |
71d02f8c
|
Julien Enselme
|
# Sync databases
|
76 |
|
|
## Save file system
|
77 |
c541cb82
|
Julien Enselme
|
private_path=$(drush "@${source_site_name}" vget --format=string file_private_path 2> /dev/null)
|
78 |
|
|
public_path=$(drush "@${source_site_name}" vget --format=string file_public_path 2> /dev/null)
|
79 |
|
|
temp_path=$(drush "@${source_site_name}" vget --format=string file_temporary_path 2> /dev/null)
|
80 |
42f1f46f
|
Julien Enselme
|
|
81 |
71d02f8c
|
Julien Enselme
|
## Sync
|
82 |
8550e097
|
Julien Enselme
|
current_date=$(date "+%Y-%m-%d-%Hh%Mm%Ss")
|
83 |
15ed27d8
|
Julien Enselme
|
sql_file="${dir_tmp}/${current_date}.${source_site_name}.sql"
|
84 |
2a0f9a31
|
Florent Torregrosa
|
drush -y "@${source_site_name}" sql-dump --result-file="${sql_file}" --structure-tables-key=common-d7
|
85 |
15ed27d8
|
Julien Enselme
|
sed -i -e "s#https?://assos.centrale-marseille.fr/${source_site_name}#https://assos.centrale-marseille.fr/${dest_site_name}#g" "${sql_file}"
|
86 |
|
|
sed -i -e "s#/${source_site_name}/sites/assos.centrale-marseille.fr.${source_site_name}#/${dest_site_name}/sites/assos.centrale-marseille.fr.${dest_site_name}#g" "${sql_file}"
|
87 |
ab3add7e
|
Julien Enselme
|
drush -y "@${dest_site_name}" sql-drop
|
88 |
|
|
drush -y "@${dest_site_name}" sql-cli < "${sql_file}"
|
89 |
3e9236bd
|
Julien Enselme
|
rm "${sql_file}"
|
90 |
42f1f46f
|
Julien Enselme
|
|
91 |
71d02f8c
|
Julien Enselme
|
## Restore file system
|
92 |
c541cb82
|
Julien Enselme
|
### Fix values for default sites/default/files must become sites/assos.centrale-marseille.fr.SITE/files
|
93 |
|
|
### For the others sites/assos.centrale-marseille.fr.SITE/files must become assos.centrale-marseille.fr.OTHER_SITE/files
|
94 |
|
|
if [ "${source_site_name}" = "default" ]; then
|
95 |
|
|
dest_path_name="assos.centrale-marseille.fr.${dest_site_name}"
|
96 |
|
|
else
|
97 |
|
|
dest_path_name="${dest_site_name}"
|
98 |
|
|
fi
|
99 |
|
|
|
100 |
|
|
if [ "${dest_site_name}" = "default" ]; then
|
101 |
|
|
souce_path_name="assos.centrale-marseille.fr.${source_site_name}"
|
102 |
|
|
else
|
103 |
|
|
source_path_name="${source_site_name}"
|
104 |
|
|
fi
|
105 |
|
|
|
106 |
3e9236bd
|
Julien Enselme
|
if [ -n "${private_path}" ] ; then
|
107 |
c541cb82
|
Julien Enselme
|
private_path="${private_path/${source_path_name}/${dest_path_name}}"
|
108 |
15ed27d8
|
Julien Enselme
|
drush -y "@${dest_site_name}" vset file_private_path "${private_path}"
|
109 |
71d02f8c
|
Julien Enselme
|
fi
|
110 |
3e9236bd
|
Julien Enselme
|
if [ -n "${public_path}" ] ; then
|
111 |
c541cb82
|
Julien Enselme
|
public_path="${public_path/${source_path_name}/${dest_path_name}}"
|
112 |
15ed27d8
|
Julien Enselme
|
drush -y "@${dest_site_name}" vset file_public_path "${public_path}"
|
113 |
71d02f8c
|
Julien Enselme
|
fi
|
114 |
3e9236bd
|
Julien Enselme
|
if [ -n "${temp_path}" ] ; then
|
115 |
c541cb82
|
Julien Enselme
|
temp_path="${temp_path/${source_path_name}/${dest_path_name}}"
|
116 |
15ed27d8
|
Julien Enselme
|
drush -y "@${dest_site_name}" vset file_temporary_path "${temp_path}"
|
117 |
71d02f8c
|
Julien Enselme
|
fi
|
118 |
|
|
|
119 |
15ed27d8
|
Julien Enselme
|
if "${prod}"; then
|
120 |
|
|
drush -y "@${dest_site_name}" vset maintenance_mode 0
|
121 |
c9d0efbc
|
Julien Enselme
|
else
|
122 |
15ed27d8
|
Julien Enselme
|
drush -y "@${dest_site_name}" vset maintenance_mode 1
|
123 |
c9d0efbc
|
Julien Enselme
|
fi |