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