1
|
#!/usr/bin/env bash
|
2
|
|
3
|
usage() {
|
4
|
help="This script is intended to ease the synchronisation between any site hosted by assos.
|
5
|
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
|
Before synching the site, the destination site's database is backuped. If the
|
9
|
destination site does not exist, it is created. If prod is set, the site is not in
|
10
|
maintenance mode.
|
11
|
|
12
|
usage: d7-sync.sh -s SOURCE_SITENAME -d DEST_SITENAME [-p]"
|
13
|
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
|
|
45
|
. /home/assos/bin/scripts-config.sh
|
46
|
. /home/assos/bin/scripts-utils.sh
|
47
|
|
48
|
|
49
|
# Create site if necessary
|
50
|
if ! site_exists "${dest_site_name}" > /dev/null ; then
|
51
|
echo "${dest_site_name} does not exit. We will create it"
|
52
|
d7-create-site.sh -s "${dest_site_name}" -m "will@be.erased" -p "willbeerased" -d
|
53
|
# if the site is new, there is no database
|
54
|
new_site=1
|
55
|
fi
|
56
|
|
57
|
|
58
|
# Backup the database of SOURCE_SITE
|
59
|
current_date=$(date "+%Y-%m-%d-%Hh%Mm%Ss")
|
60
|
if [ "${source_site_name}" = "default" ] ; then
|
61
|
dir="${dest_site_name}"
|
62
|
else
|
63
|
dir="assos.centrale-marseille.fr.${dest_site_name}"
|
64
|
fi
|
65
|
if [ -z "${new_site}" ] ; then
|
66
|
drush -y "@${source_site_name}" sql-dump --result-file="${d7_dir_individual_manual_backup}/${dir}/${current_date}.${dir}.sql" --gzip
|
67
|
fi
|
68
|
|
69
|
|
70
|
# Sync files
|
71
|
drush -y rsync --delete --exclude="*.php" "@${source_site_name}:%site" "@${dest_site_name}:%site"
|
72
|
|
73
|
|
74
|
# Sync databases
|
75
|
## Save file system
|
76
|
if [ -z "${new_site}" ] ; then
|
77
|
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
|
fi
|
81
|
|
82
|
## Sync
|
83
|
current_date=$(date "+%Y-%m-%d-%Hh%Mm%Ss")
|
84
|
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
|
drush -y "@${dest_site_name}" sql-drop
|
89
|
drush -y "@${dest_site_name}" sql-cli < "${sql_file}"
|
90
|
rm "${sql_file}"
|
91
|
|
92
|
## Restore file system
|
93
|
if [ -n "${private_path}" ] ; then
|
94
|
drush -y "@${dest_site_name}" vset file_private_path "${private_path}"
|
95
|
fi
|
96
|
if [ -n "${public_path}" ] ; then
|
97
|
drush -y "@${dest_site_name}" vset file_public_path "${public_path}"
|
98
|
fi
|
99
|
if [ -n "${temp_path}" ] ; then
|
100
|
drush -y "@${dest_site_name}" vset file_temporary_path "${temp_path}"
|
101
|
fi
|
102
|
|
103
|
if "${prod}"; then
|
104
|
drush -y "@${dest_site_name}" vset maintenance_mode 0
|
105
|
else
|
106
|
drush -y "@${dest_site_name}" vset maintenance_mode 1
|
107
|
fi
|