forked from outroll/vesta
-
Notifications
You must be signed in to change notification settings - Fork 46
/
v-migrate-site-to-https
190 lines (155 loc) · 6.88 KB
/
v-migrate-site-to-https
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#!/bin/bash
# info: migrate site to https
# options: DOMAIN [--DATABASE_NAME=...] [--DATABASE_USERNAME=...] [--DATABASE_PASSWORD=...]
#
# Tool that will replace http to https URLs in database, install SSL and switch to force-https proxy template
# Automatic detection of CMS, automaticaly read DB user, DB name and DB pass.
#----------------------------------------------------------#
# Variable&Function #
#----------------------------------------------------------#
whoami=$(whoami)
if [ "$whoami" != "root" ]; then
echo "You must be root to execute this script"
exit 1
fi
# Argument definition
domain=$1
user=$(/usr/local/vesta/bin/v-search-domain-owner $domain)
if [ -z "$user" ]; then
echo "domain $domain doesn't exist"
exit 1
fi
# Importing system environment
source /etc/profile
# Includes
source /usr/local/vesta/func/main.sh
source /usr/local/vesta/func/db.sh
#----------------------------------------------------------#
# Verifications #
#----------------------------------------------------------#
check_args '1' "$#" 'DOMAIN [--DATABASE_NAME=...] [--DATABASE_USERNAME=...] [--DATABASE_PASSWORD=...]'
is_format_valid 'domain' 'user'
is_object_valid 'user' 'USER' "$user"
is_object_unsuspended 'user' 'USER' "$user"
FROM_DATABASE_NAME=''
FROM_DATABASE_USERNAME=''
FROM_DATABASE_PASSWORD=''
FROM_DOMAIN=$domain
FROM_USER=$user
r=$(/usr/local/vesta/bin/v-get-database-credentials-of-domain $FROM_DOMAIN)
# echo $r
eval $r
source /usr/local/vesta/func/handle_parameters.sh
FROM_CONFIG_FILE=$CONFIG_FILE
FROM_CONFIG_FILE_FULL_PATH=$CONFIG_FILE_FULL_PATH
if [ ! -f "$FROM_CONFIG_FILE_FULL_PATH" ]; then
echo "Error: CONFIG_FILE_FULL_PATH $FROM_CONFIG_FILE_FULL_PATH does not exists"
exit 2
fi
IT_IS_WP=0
if [ "$CMS_TYPE" = "wordpress" ]; then
IT_IS_WP=1
fi
if [ -z "$FROM_DATABASE_NAME" ]; then
FROM_DATABASE_NAME=$DATABASE_NAME
fi
if [ -z "$FROM_DATABASE_USERNAME" ]; then
FROM_DATABASE_USERNAME=$DATABASE_USERNAME
fi
if [ -z "$FROM_DATABASE_PASSWORD" ]; then
FROM_DATABASE_PASSWORD=$DATABASE_PASSWORD
fi
if [ -z "$FROM_DATABASE_NAME" ]; then
echo "Error: DATABASE_NAME is empty"
exit 3
fi
if [ -z "$FROM_DATABASE_USERNAME" ]; then
echo "Error: DATABASE_USERNAME is empty"
exit 4
fi
if [ -z "$FROM_DATABASE_PASSWORD" ]; then
echo "Error: DATABASE_PASSWORD is empty"
exit 5
fi
DB_EXISTS=$(check_if_database_exists "$user" "$FROM_DATABASE_NAME")
if [ "$DB_EXISTS" = "no" ]; then
echo "Error: database $FROM_DATABASE_NAME does not exists"
exit 6
fi
phpver=$(/usr/local/vesta/bin/v-get-php-version-of-domain "$FROM_DOMAIN")
# ----------- CHECK -------------
FROM_REPLACE1="http://$FROM_DOMAIN"
TO_REPLACE1="https://$FROM_DOMAIN"
FROM_REPLACE2="http://www.$FROM_DOMAIN"
TO_REPLACE2="https://www.$FROM_DOMAIN"
if [ $IT_IS_WP -eq 0 ]; then
if [ ! -f "/root/Search-Replace-DB/srdb.cli.php" ]; then
if [ ! -f "/usr/bin/git" ]; then
apt-get update > /dev/null 2>&1
apt-get -y install git > /dev/null 2>&1
fi
cd /root
git clone https://github.com/interconnectit/Search-Replace-DB.git
fi
else
if [ ! -f "/usr/local/bin/wp" ]; then
echo "=== Downloading latest wp-cli"
wget -nv https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar -O /usr/local/bin/wp
chmod +x /usr/local/bin/wp
fi
fi
# ----------- PRINT -------------
echo "==============================================================================="
echo "FROM_REPLACE1 = $FROM_REPLACE1"
echo "TO_REPLACE1 = $TO_REPLACE1"
echo "FROM_REPLACE2 = $FROM_REPLACE2"
echo "TO_REPLACE2 = $TO_REPLACE2"
echo "----"
echo "DOMAIN = $FROM_DOMAIN"
echo "USER = $FROM_USER"
echo "SITE_FOLDER = $SITE_FOLDER"
echo "IT_IS_WP = $IT_IS_WP"
echo "CONFIG_FILE_FULL_PATH = $FROM_CONFIG_FILE_FULL_PATH"
echo "DATABASE_NAME = $FROM_DATABASE_NAME"
echo "DATABASE_USERNAME = $FROM_DATABASE_USERNAME"
echo "DATABASE_PASSWORD = $FROM_DATABASE_PASSWORD"
echo "==============================================================================="
read -p "=== Press Enter to continue ==="
#----------------------------------------------------------#
# Action #
#----------------------------------------------------------#
if [ ! -f "/home/$user/conf/web/ssl.$domain.ca" ]; then
/usr/local/vesta/bin/v-add-letsencrypt-domain "$user" "$domain" "www.$domain" "yes"
fi
if [ -f "/home/$user/conf/web/ssl.$domain.ca" ]; then
if [ -f "/usr/local/vesta/data/templates/web/nginx/force-https.stpl" ]; then
/usr/local/vesta/bin/v-change-web-domain-proxy-tpl "$user" "$domain" "force-https" "jpeg,jpg,png,gif,bmp,ico,svg,tif,tiff,css,js,ttf,otf,webp,txt,csv,rtf,doc,docx,xls,xlsx,ppt,pptx,odf,odp,ods,odt,pdf,psd,ai,eot,eps,ps,zip,tar,tgz,gz,rar,bz2,7z,aac,m4a,mp3,mp4,ogg,wav,wma,3gp,avi,flv,m4v,mkv,mov,mpeg,mpg,wmv,exe,iso,dmg,swf,woff,woff2" "yes"
fi
else
echo "Error: Can not generate SSL."
exit 9
fi
echo "=== Replacing $FROM_REPLACE1 to $TO_REPLACE1 in folder $TO_FOLDER"
grep -rl "$FROM_DOMAIN" $SITE_FOLDER | xargs sed -i "s#$FROM_REPLACE1#$TO_REPLACE1#g"
echo "=== Replacing $FROM_REPLACE2 to $TO_REPLACE2 in folder $TO_FOLDER"
grep -rl "$FROM_DOMAIN" $SITE_FOLDER | xargs sed -i "s#$FROM_REPLACE2#$TO_REPLACE2#g"
if [ $IT_IS_WP -eq 0 ]; then
echo "=== Replacing $FROM_REPLACE1 to $TO_REPLACE1 in database $FROM_DATABASE_NAME"
php /root/Search-Replace-DB/srdb.cli.php -h localhost -n "$FROM_DATABASE_NAME" -u "$FROM_DATABASE_USERNAME" -p "$FROM_DATABASE_PASSWORD" -s "$FROM_REPLACE1" -r "$TO_REPLACE1"
echo "=== Replacing $FROM_REPLACE2 to $TO_REPLACE2 in database $FROM_DATABASE_NAME"
php /root/Search-Replace-DB/srdb.cli.php -h localhost -n "$FROM_DATABASE_NAME" -u "$FROM_DATABASE_USERNAME" -p "$FROM_DATABASE_PASSWORD" -s "$FROM_REPLACE2" -r "$TO_REPLACE2"
else
cd $SITE_FOLDER
echo "=== Replacing $FROM_REPLACE1 to $TO_REPLACE1 in database $FROM_DATABASE_NAME"
sudo -H -u$FROM_USER /usr/bin/php$phpver /usr/local/bin/wp search-replace "$FROM_REPLACE1" "$TO_REPLACE1" --precise --all-tables --skip-columns=guid --skip-plugins=$(sudo -H -u$FROM_USER /usr/bin/php$phpver /usr/local/bin/wp plugin list --field=name | tr '\n' ',') --skip-themes;
echo "=== Replacing $FROM_REPLACE2 to $TO_REPLACE2 in database $FROM_DATABASE_NAME"
sudo -H -u$FROM_USER /usr/bin/php$phpver /usr/local/bin/wp search-replace "$FROM_REPLACE2" "$TO_REPLACE2" --precise --all-tables --skip-columns=guid --skip-plugins=$(sudo -H -u$FROM_USER /usr/bin/php$phpver /usr/local/bin/wp plugin list --field=name | tr '\n' ',') --skip-themes;
fi
echo "===== DONE ===="
echo "You can visit $TO_REPLACE1"
#----------------------------------------------------------#
# Vesta #
#----------------------------------------------------------#
# Logging
log_event "$OK" "$ARGUMENTS"
exit