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