Project

General

Profile

Paste
Download (4.18 KB) Statistics
| Branch: | Revision:

root / bin / d7-sync-site.sh @ master

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-site.sh -s SOURCE_SITENAME -d DEST_SITENAME [-p]
13
example: d7-sync-site.sh -s default -d clubdrupal [-p]"
14
    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

    
46
. /home/assos/bin/scripts-config.sh
47
. /home/assos/bin/scripts-utils.sh
48

    
49

    
50
# Create site if necessary
51
if ! site_exists "${dest_site_name}" > /dev/null ; then
52
    echo "${dest_site_name} does not exit. We will create it"
53
    d7-create-site.sh -s "${dest_site_name}" -m "will@be.erased" -p "willbeerased" -d
54
    # if the site is new, there is no database
55
    new_site=1
56
fi
57

    
58

    
59
# Backup the database of SOURCE_SITE
60
current_date=$(date "+%Y-%m-%d-%Hh%Mm%Ss")
61
if [ "${source_site_name}" = "default" ] ; then
62
    dir="${dest_site_name}"
63
else
64
    dir="assos.centrale-marseille.fr.${dest_site_name}"
65
fi
66
if [ -z "${new_site}" ] ; then
67
    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
fi
69

    
70

    
71
# Sync files
72
drush -y rsync --delete --exclude="*.php" "@${source_site_name}:%site" "@${dest_site_name}:%site"
73

    
74

    
75
# Sync databases
76
## Save file system
77
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

    
81
## Sync
82
current_date=$(date "+%Y-%m-%d-%Hh%Mm%Ss")
83
sql_file="${dir_tmp}/${current_date}.${source_site_name}.sql"
84
drush -y "@${source_site_name}" sql-dump --result-file="${sql_file}" --structure-tables-key=common-d7
85
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
drush -y "@${dest_site_name}" sql-drop
88
drush -y "@${dest_site_name}" sql-cli < "${sql_file}"
89
rm "${sql_file}"
90

    
91
## Restore file system
92
### 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
if [ -n "${private_path}" ] ; then
107
    private_path="${private_path/${source_path_name}/${dest_path_name}}"
108
    drush -y "@${dest_site_name}" vset file_private_path "${private_path}"
109
fi
110
if [ -n "${public_path}" ] ; then
111
    public_path="${public_path/${source_path_name}/${dest_path_name}}"
112
    drush -y "@${dest_site_name}" vset file_public_path "${public_path}"
113
fi
114
if [ -n "${temp_path}" ] ; then
115
    temp_path="${temp_path/${source_path_name}/${dest_path_name}}"
116
    drush -y "@${dest_site_name}" vset file_temporary_path "${temp_path}"
117
fi
118

    
119
if "${prod}"; then
120
    drush -y "@${dest_site_name}" vset maintenance_mode 0
121
else
122
    drush -y "@${dest_site_name}" vset maintenance_mode 1
123
fi