Project

General

Profile

Revision 15ed27d8

Added by Julien Enselme over 9 years ago

Use getopts to print help and to parse script arguments.

View differences:

bin/d7-sync-site.sh
1 1
#!/usr/bin/env bash
2 2

  
3
help=<<EOF
3
usage() {
4
    help=<<EOF
4 5
This script is intended to ease the synchronisation between any site hosted by assos.
5 6
Typically, this script is usefull when you have (or want to have) a test site based on
6 7
a already working site. It relies on bash, drush and drush aliases.
7 8

  
8 9
Before synching the site, the destination site's database is backuped. If the
9
destination site does not exist, it is created.
10
destination site does not exist, it is created. If prod is set, the site is not in
11
maintenance mode.
10 12

  
11
usage: d7-sync.sh SOURCE_SITENAME DEST_SITENAME [--prod]
13
usage: d7-sync.sh -s SOURCE_SITENAME -d DEST_SITENAME [-p]
12 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

  
13 46

  
14 47
. /home/assos/bin/scripts-config.sh
15 48
. /home/assos/bin/scripts-utils.sh
......
18 51

  
19 52

  
20 53
# Create site if necessary
21
if ! site_exists "$2" > /dev/null ; then
22
    echo "$2 does not exit. We will create it"
23
    d7-create-site.sh "$2" --no-init-database
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
24 57
    # if the site is new, there is no database
25 58
    new_site=1
26 59
fi
......
28 61

  
29 62
# Backup the database of SOURCE_SITE
30 63
current_date=$(date "+%Y-%m-%d-%Hh%Mm%Ss")
31
if [ "$1" = "default" ] ; then
32
    dir="$2"
64
if [ "${source_site_name}" = "default" ] ; then
65
    dir="${dest_site_name}"
33 66
else
34
    dir="assos.centrale-marseille.fr.$2"
67
    dir="assos.centrale-marseille.fr.${dest_site_name}"
35 68
fi
36 69
if [ -z "${new_site}" ] ; then
37
    drush -y "@$1" sql-dump --result-file="${d7_dir_individual_manual_backup}/${dir}/${current_date}.${dir}.sql" --gzip
70
    drush -y "@${source_site_name}" sql-dump --result-file="${d7_dir_individual_manual_backup}/${dir}/${current_date}.${dir}.sql" --gzip
38 71
fi
39 72

  
40 73

  
......
45 78
# Sync databases
46 79
## Save file system
47 80
if [ -z "${new_site}" ] ; then
48
    private_path=$(drush "@$2" vget --format=string file_private_path 2> /dev/null)
49
    public_path=$(drush "@$2" vget --format=string file_public_path 2> /dev/null)
50
    temp_path=$(drush "@$2" vget --format=string file_temporary_path 2> /dev/null)
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)
51 84
fi
52 85

  
53 86
## Sync
54 87
current_date=$(date "+%Y-%m-%d-%Hh%Mm%Ss")
55
sql_file="${dir_tmp}/${current_date}.$1.sql"
56
drush -y "@$1" sql-dump --result-file="${sql_file}"
57
sed -i -e "s#https?://assos.centrale-marseille.fr/$1#https://assos.centrale-marseille.fr/$2#g" "${sql_file}"
58
sed -i -e "s#/$1/sites/assos.centrale-marseille.fr.$1#/$2/sites/assos.centrale-marseille.fr.$2#g" "${sql_file}"
59
drush "@$2" sql-drop
60
drush "@$2" sql-cli < "${sql_file}"
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}"
61 94
rm "${sql_file}"
62 95

  
63 96
## Restore file system
64 97
if [ -n "${private_path}" ] ; then
65
    drush -y "@$2" vset file_private_path "${private_path}"
98
    drush -y "@${dest_site_name}" vset file_private_path "${private_path}"
66 99
fi
67 100
if [ -n "${public_path}" ] ; then
68
    drush -y "@$2" vset file_public_path "${public_path}"
101
    drush -y "@${dest_site_name}" vset file_public_path "${public_path}"
69 102
fi
70 103
if [ -n "${temp_path}" ] ; then
71
    drush -y "@$2" vset file_temporary_path "${temp_path}"
104
    drush -y "@${dest_site_name}" vset file_temporary_path "${temp_path}"
72 105
fi
73 106

  
74
if [ "$3" = "--prod" ] ; then
75
    drush -y "@$2" vset maintenance_mode 0
107
if "${prod}"; then
108
    drush -y "@${dest_site_name}" vset maintenance_mode 0
76 109
else
77
    drush -y "@$2" vset maintenance_mode 1
110
    drush -y "@${dest_site_name}" vset maintenance_mode 1
78 111
fi

Also available in: Unified diff