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 |
3e9236bd
|
Julien Enselme
|
if [ -z "${new_site}" ] ; then
|
77 |
15ed27d8
|
Julien Enselme
|
private_path=$(drush "@${dest_site_name}" vget --format=string file_private_path 2> /dev/null)
|
78 |
|
|
public_path=$(drush "@${dest_site_name}" vget --format=string file_public_path 2> /dev/null)
|
79 |
|
|
temp_path=$(drush "@${dest_site_name}" vget --format=string file_temporary_path 2> /dev/null)
|
80 |
c9d0efbc
|
Julien Enselme
|
fi
|
81 |
42f1f46f
|
Julien Enselme
|
|
82 |
71d02f8c
|
Julien Enselme
|
## Sync
|
83 |
8550e097
|
Julien Enselme
|
current_date=$(date "+%Y-%m-%d-%Hh%Mm%Ss")
|
84 |
15ed27d8
|
Julien Enselme
|
sql_file="${dir_tmp}/${current_date}.${source_site_name}.sql"
|
85 |
|
|
drush -y "@${source_site_name}" sql-dump --result-file="${sql_file}"
|
86 |
|
|
sed -i -e "s#https?://assos.centrale-marseille.fr/${source_site_name}#https://assos.centrale-marseille.fr/${dest_site_name}#g" "${sql_file}"
|
87 |
|
|
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}"
|
88 |
ab3add7e
|
Julien Enselme
|
drush -y "@${dest_site_name}" sql-drop
|
89 |
|
|
drush -y "@${dest_site_name}" sql-cli < "${sql_file}"
|
90 |
3e9236bd
|
Julien Enselme
|
rm "${sql_file}"
|
91 |
42f1f46f
|
Julien Enselme
|
|
92 |
71d02f8c
|
Julien Enselme
|
## Restore file system
|
93 |
3e9236bd
|
Julien Enselme
|
if [ -n "${private_path}" ] ; then
|
94 |
15ed27d8
|
Julien Enselme
|
drush -y "@${dest_site_name}" vset file_private_path "${private_path}"
|
95 |
71d02f8c
|
Julien Enselme
|
fi
|
96 |
3e9236bd
|
Julien Enselme
|
if [ -n "${public_path}" ] ; then
|
97 |
15ed27d8
|
Julien Enselme
|
drush -y "@${dest_site_name}" vset file_public_path "${public_path}"
|
98 |
71d02f8c
|
Julien Enselme
|
fi
|
99 |
3e9236bd
|
Julien Enselme
|
if [ -n "${temp_path}" ] ; then
|
100 |
15ed27d8
|
Julien Enselme
|
drush -y "@${dest_site_name}" vset file_temporary_path "${temp_path}"
|
101 |
71d02f8c
|
Julien Enselme
|
fi
|
102 |
|
|
|
103 |
15ed27d8
|
Julien Enselme
|
if "${prod}"; then
|
104 |
|
|
drush -y "@${dest_site_name}" vset maintenance_mode 0
|
105 |
c9d0efbc
|
Julien Enselme
|
else
|
106 |
15ed27d8
|
Julien Enselme
|
drush -y "@${dest_site_name}" vset maintenance_mode 1
|
107 |
c9d0efbc
|
Julien Enselme
|
fi |