Projet

Général

Profil

Paste
Télécharger (4,12 ko) Statistiques
| Branche: | Révision:

root / bin / d7-sync-site.sh @ 2a0f9a31

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 --structure-tables-key=common-d7
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
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

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

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

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