Skip to content

Commit f1bdf32

Browse files
committedMar 8, 2017
Merge branch 'master' of github.com:gustavomrfz/automate_wordpress
2 parents c8f52de + 5a4c418 commit f1bdf32

File tree

10 files changed

+479
-16
lines changed

10 files changed

+479
-16
lines changed
 

‎.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/install_one_instance
2+

‎README.md

+123-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,123 @@
1-
# automate_wordpress
2-
Scripting for automatization of Wordpress
1+
# Atomate Wordpress
2+
3+
# Scripts for automating tasks related to Wordpress
4+
5+
These **Bash scripts** automate some usual tasks working with **Wordpress**, and **Nginx**/**MariaDB** backend. The scripts are usefull on a clean Debian Jessie installation and surely they don't work on Ubuntu so far. It's advise to **not** install before running install script by first time an Apache or other HTTP server, unless they are listening at any port except port 80, so they could may cause conflicts with Nginx.
6+
7+
## Install one local instance
8+
9+
Install a Wordpress instance on local.
10+
11+
### Usage
12+
13+
First change mod permissions of file to execute script, then:
14+
15+
`./install_wordpress.sh`
16+
17+
### Parameters
18+
19+
Some parameters are needed. They must be defined in `/conf/config` file.
20+
21+
###### Domain for Wordpress
22+
`domain=`
23+
24+
###### Title for Wordpress
25+
`title=`
26+
27+
###### Mysql root password
28+
`mysql_root_pass=`
29+
30+
###### Install path (without final slash)
31+
`wp_path=`
32+
33+
###### Charset that will use Wordpress
34+
`wp_charset=`
35+
36+
##### Wordpress locales
37+
`wp_locale=`
38+
39+
###### Wordpress user with administration rights
40+
`wp_admin_user=`
41+
42+
###### Password for Wordpress administrator
43+
`wp_admin_password=`
44+
45+
###### Wordpress administrator email
46+
`wp_admin_email=``
47+
48+
###### Host of Mariadb
49+
`wp_db_host=``
50+
51+
###### Database user for Wordpress
52+
`wp_db_user=`
53+
`
54+
###### Database user for Wordpress password
55+
`wp_db_password=`
56+
`
57+
###### Name of database for Wordpress
58+
`wp_db_name=`
59+
60+
###### User for wp cli. It's needed to execute wp cli.
61+
`wpcli_user=`
62+
63+
64+
## Uninstall one local instance
65+
66+
Uninstall local Wordpress installed with the **above** script. It's more than probable that script weren't work in other cases.
67+
68+
#### Usage
69+
70+
Simply:
71+
72+
`./uninstall_wordpress.sh yourdomain.org`
73+
74+
Only a FQDN domain name is needed as parameter. Script takes the rest of parameters, such the database name, from `wp-config.php` and Nginx's available sites.
75+
76+
## Backup Wordpress
77+
78+
Backup database, files and Nginx's site configuration via SSH to a remote folder. A pair of SSH keys is highly recommended. If you need some help to use them, this [tutorial](https://debian-administration.org/article/530/SSH_with_authentication_key_instead_of_password) is a good first step.
79+
80+
### Usage
81+
82+
`./backup_wordpress.sh`
83+
84+
### Parameters
85+
86+
Defined in `/params/backup.conf`
87+
88+
###### FQDN to backup
89+
`domain=`
90+
###### Remote user for SSH
91+
92+
`backup_user=`
93+
94+
###### Remote host (IP or domain name)
95+
`backup_host=`
96+
97+
###### Remote path where files will be stored
98+
`backup_remote_dir=`
99+
100+
101+
## Restore Wordpress
102+
103+
Restored a site backed with the previous script.
104+
105+
### Usage
106+
107+
`./restore_wordpress`
108+
109+
### Parameters
110+
111+
Defined in `params/restore.conf`
112+
113+
###### FQDN to backup
114+
`domain=`
115+
###### Remote user for SSH
116+
117+
`backup_user=`
118+
119+
###### Remote host (IP or domain name)
120+
`backup_host=`
121+
122+
###### Remote path where files will be stored
123+
`backup_remote_dir=`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#!/bin/bash
2+
3+
4+
# function: iferror
5+
# produces an exit code 1 with message
6+
function iferror {
7+
if [[ $? -eq 1 ]]; then
8+
echo $1; exit 1;
9+
fi
10+
}
11+
12+
# function: read_root_path
13+
# Read root path from Nginx server
14+
15+
function read_root_path {
16+
if [[ -e /etc/nginx/sites-available/$domain.conf ]];then
17+
wp_path=$(grep -E "root.*$domain" /etc/nginx/sites-available/$domain.conf \
18+
| awk -F' ' '{print substr($2, 1, length($2)-1)}');
19+
else
20+
iferror "Site is not available";
21+
fi
22+
}
23+
24+
# function: get_config_parameters
25+
# Read wp-config.php to get parameters
26+
27+
function get_config_parameters {
28+
29+
wp_cnf=$wp_path/wp-config.php;
30+
db_name=$(grep "DB_NAME" $wp_cnf | awk -F"'" '{print $4}');
31+
db_user=$(grep "DB_USER" $wp_cnf | awk -F"'" '{print $4}');
32+
db_password=$(grep "DB_PASSWORD" $wp_cnf | awk -F"'" '{print $4}');
33+
db_host=$(grep "DB_PASSWORD" $wp_cnf | awk -F"'" '{print $4}');
34+
if [[ $db_host == '' ]]; then
35+
db_host=localhost;
36+
fi
37+
}
38+
39+
# function: mysql_dump
40+
# Produces a backup of full database in a tar.gz file
41+
42+
function mysql_dump {
43+
ssh $backup_user@$backup_host "mkdir -p $backup_remote_dir/$domain"
44+
mysqldump --user $db_user --password=$db_password \
45+
$db_name | gzip -c | ssh $backup_user@$backup_host "cat > \
46+
$backup_remote_dir/$domain/$(date +%Y%m%d)$db_name.sql.gz" \
47+
|| iferror "Backup for database named $db_name has failed" \
48+
&& wp_simple_backup_files;
49+
}
50+
# function: wp_simple_backup_files
51+
# Produces a tar.gz to a remote host
52+
53+
function wp_simple_backup_files {
54+
wp_targz="/tmp/$(date +%Y%m%d)$domain.tar.gz";
55+
if [[ -d $wp_path ]]; then
56+
tar czfp $wp_targz -C $wp_path . \
57+
&& scp $wp_targz $backup_user@$backup_host:$backup_remote_dir/$domain/. \
58+
&& rm -f $targz_file \
59+
|| iferror "Backup file not sended to $backup_host"
60+
fi
61+
}
62+
# function: nginx_site_backup
63+
# Produces a file with Nginx configurarion of the given server
64+
65+
function nginx_site_backup {
66+
if [[ -e /etc/nginx/sites-enabled/$domain.conf ]]; then
67+
scp /etc/nginx/sites-enabled/$domain.conf \
68+
$backup_user@$backup_host:$backup_remote_dir/$domain/.
69+
fi
70+
}
71+
72+
function start {
73+
read_root_path \
74+
&& get_config_parameters \
75+
&& mysql_dump \
76+
&& nginx_site_backup
77+
}
78+
79+
if [[ -e ./params/backup.conf ]];then
80+
source ./params/backup.conf;
81+
else
82+
iferror "First you need configure parameters";
83+
fi
84+
85+
if [ "$(id -u)" != "0" ]; then
86+
echo "This script must be run as root" 1>&2
87+
exit 1
88+
fi
89+
start
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
domain=retore.org
2+
backup_user=gustavo
3+
backup_host=192.168.1.254
4+
backup_remote_dir=backup

‎install_one_instance/conf/install_wordpress.conf ‎install_one_local_instance/conf/config

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#wp_charset=
1111
# Wordpress locales
1212
#wp_locale=
13-
# Wordpress user with administrations rights
13+
# Wordpress user with administration rights
1414
#wp_admin_user=
1515
# Password for Wordpress administrator
1616
#wp_admin_password=

‎install_one_instance/install_wordpress.sh ‎install_one_local_instance/install_wordpress.sh

+15-11
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,9 @@
11
#!/bin/bash
22

3-
if [[ -e install_wordpress.conf ]];then
4-
source ./conf/install_wordpress.conf;
5-
else
6-
iferror "First you need configure parameters"
7-
fi
8-
9-
103
sudoers_root="root ALL=(ALL:ALL) ALL"
114
sudoers_wpcli="wpcli ALL=(www-data) NOPASSWD: /usr/local/bin/wp"
125

13-
useradd $wpcli;
6+
useradd $wpcli_user;
147

158
# function: start
169
# Install sudo if it is not and run script
@@ -47,7 +40,7 @@ set_php7_sources() {
4740

4841
function install_dependencies {
4942
apt-get -y install sudo mariadb-server mariadb-client \
50-
php7.0-mysql php7.0-fpm nginx nginx-extras php7.0 \
43+
php7.0-mysql php7.0-fpm nginx nginx-extras php7.0 imagemagick \
5144
&& install_WP_cli \
5245
&& if ! ( grep -qs $sudoers_root /etc/sudoers ); then
5346
echo $sudoers_root >> /etc/sudoers;
@@ -69,7 +62,7 @@ function create_database {
6962
# create user for wordpress and grant all privileges
7063

7164
function create_and_grant_user {
72-
mysql -uroot -p$mysql_root_pass -e " grant all privileges on $title.* to \
65+
mysql -uroot -p$mysql_root_pass -e " grant all privileges on $wp_db_name.* to \
7366
'$wp_db_user'@'localhost' identified by '$wp_db_password';"
7467
}
7568

@@ -116,12 +109,13 @@ function install_WP {
116109
|| iferror "Wordpress not configured" ;
117110

118111
sudo -u $wpcli_user -- wp core install \
119-
--url=$domain --title=$title --path=$wp_path --admin_user=$wp_admin_user \
112+
--url=$domain --title="$title" --path=$wp_path --admin_user=$wp_admin_user \
120113
--admin_password=$wp_admin_password --admin_email=$wp_admin_email \
121114
--path=$wp_path \
122115
|| iferror "Wordpress not installed";
123116

124117
chmod -R 775 $wp_path;
118+
chmod -R www-data: $wp_path;
125119
nginx_create_site
126120
fi
127121
}
@@ -158,4 +152,14 @@ function nginx_enable_site {
158152
systemctl reload nginx;
159153
}
160154

155+
if [ "$(id -u)" != "0" ]; then
156+
echo "This script must be run as root" 1>&2
157+
exit 1
158+
fi
159+
if [[ -e ./conf/config ]];then
160+
source ./conf/config;
161+
else
162+
iferror "First you need configure parameters"
163+
fi
164+
161165
start

‎install_one_instance/nginx/nginx.available ‎install_one_local_instance/nginx/nginx.available

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
server {
2-
listen 80 default_server;
3-
listen [::]:80 default_server ipv6only=on;
2+
listen 80;
43
# listen 443 ssl;
54
root ROOTPATH;
65
index index.php index.html index.htm;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
domain=
2+
backup_user=
3+
backup_host=
4+
backup_remote_dir=
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
#!/bin/bash
2+
3+
# function: iferror
4+
# produces an exit code 1 with message
5+
function iferror {
6+
if [[ $? -eq 1 ]]; then
7+
echo $1; exit 1;
8+
fi
9+
}
10+
11+
# function: mount_remote_backups
12+
# mount remote backup for a domain
13+
14+
function mount_remote_backup {
15+
mnt_dir=/tmp/$RANDOM;
16+
mkdir $mnt_dir
17+
scp -r $backup_user@$backup_host:$backup_remote_dir/$domain/* $mnt_dir
18+
sleep 1
19+
set_file_paths
20+
21+
}
22+
23+
# function: set_file_paths
24+
# set last Wordpress files packges
25+
26+
function set_file_paths {
27+
28+
wp_gz_file=$(find $mnt_dir -type f -name "*.tar.gz" | sort -rn | head -1);
29+
sql_gz_file=$(find $mnt_dir -type f -name "*.sql.gz" | sort -rn | head -1);
30+
nginx_file=$(find $mnt_dir -type f -name "*.conf");
31+
set_config_parameters
32+
}
33+
34+
# function: read_root_path
35+
# read root path from Nginx server configuration
36+
37+
function read_root_path {
38+
39+
domain_string_length=${#domain}
40+
name_offset=$(($domaing_string_length + 1 ))
41+
42+
if [[ -e $nginx_file ]];then
43+
wp_path=$(grep -E "root.*$domain" $nginx_file \
44+
| awk -v offset=$name_offset -F' ' '{print substr($2, 1, length($2) - offset)}')
45+
else
46+
iferror "Site root is not available";
47+
fi
48+
}
49+
50+
# function: restore_files
51+
# extract files to local root
52+
53+
function restore_files {
54+
read_root_path
55+
tar -zxf $wp_gz_file -C $wp_path/.
56+
echo "Wordpress files now are restored"
57+
58+
}
59+
60+
# function: set_config_parameters
61+
# get wp-config.php dabase parameters
62+
63+
function set_config_parameters {
64+
65+
restore_files && wp_cnf=$wp_path/wp-config.php
66+
67+
db_name=$(grep "DB_NAME" $wp_cnf | awk -F"'" '{print $4}');
68+
db_user=$(grep "DB_USER" $wp_cnf | awk -F"'" '{print $4}');
69+
db_password=$(grep "DB_PASSWORD" $wp_cnf | awk -F"'" '{print $4}');
70+
db_host=$(grep "DB_PASSWORD" $wp_cnf | awk -F"'" '{print $4}');
71+
72+
if [[ $db_host == '' ]]; then
73+
db_host=localhost;
74+
fi
75+
restore_database
76+
}
77+
78+
79+
80+
# function: restore_database
81+
# restore database and tables to Mariadb/mysql_root_pass
82+
83+
function restore_database {
84+
85+
zcat $sql_gz_file > /tmp/$domain.sql
86+
sql="/tmp/$domain.sql"
87+
mysql -u$db_user -p$db_password -e "create database if not exists $db_name;" \
88+
&& mysql -u$db_user -p$db_password $db_name < $sql \
89+
|| iferror "Datase not imported"
90+
echo "Database now is restored"
91+
}
92+
93+
94+
# function: nginx_disable_server
95+
# disable server to restore
96+
97+
function nginx_disable_server {
98+
if [[ -e /etc/nginx/sites-enables/$domain.conf ]]; then
99+
rm -f /etc/nginx/sites-enabled/$domain.conf;
100+
systemctl reload nginx;
101+
fi
102+
echo "Server $domain disabled"
103+
104+
}
105+
# function: nginx_enable_server
106+
# enable again with resotred server
107+
108+
function nginx_enable_server {
109+
#if [[ -e /etc/nginx/sites-available/$domain.conf ]]; then
110+
# mv -f /etc/nginx/sites-available ~/.;
111+
#fi
112+
cp -fp $mnt_dir/$domain.conf /etc/nginx/sites-available/$domain.conf \
113+
&& ln -fs /etc/nginx/sites-available/$domain.conf /etc/nginx/sites-enabled/$domain.conf
114+
systemctl reload nginx
115+
echo "Server $domain enabled"
116+
117+
}
118+
119+
120+
# function: start
121+
# start to run scrip
122+
123+
function start {
124+
nginx_disable_server \
125+
&& mount_remote_backup \
126+
&& nginx_enable_server
127+
sleep 4 && rm -fR $mnt_dir
128+
}
129+
130+
if [[ -e ./params/restore.conf ]];then
131+
source ./params/restore.conf;
132+
else
133+
iferror "First you need configure parameters";
134+
fi
135+
136+
if [ "$(id -u)" != "0" ]; then
137+
echo "This script must be run as root" 1>&2
138+
exit 1
139+
fi
140+
141+
start
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
#!/bin/bash
2+
3+
# function: iferror
4+
# produces an exit code 1 with message
5+
function iferror {
6+
if [[ $? -eq 1 ]]; then
7+
echo $1; exit 1;
8+
fi
9+
}
10+
11+
# function: read_root_path
12+
# Read root path from Nginx server
13+
14+
function read_root_path {
15+
if [[ -e /etc/nginx/sites-available/$domain.conf ]];then
16+
wp_path=$(grep -E "root.*$domain" /etc/nginx/sites-available/$domain.conf \
17+
| awk -F' ' '{print substr($2, 1, length($2)-1)}');
18+
else
19+
iferror "Site is not available";
20+
fi
21+
}
22+
23+
# function: get_config_parameters
24+
# Read wp-config.php to get parameters needed to uninstall
25+
26+
function get_config_parameters {
27+
wp_cnf=$wp_path/wp-config.php
28+
29+
db_name=$(grep "DB_NAME" $wp_cnf | awk -F"'" '{print $4}')
30+
db_user=$(grep "DB_USER" $wp_cnf | awk -F"'" '{print $4}')
31+
db_password=$(grep "DB_PASSWORD" $wp_cnf | awk -F"'" '{print $4}')
32+
db_host=$(grep "DB_PASSWORD" $wp_cnf | awk -F"'" '{print $4}')
33+
if [[ $db_host == '' ]]; then
34+
db_host=localhost
35+
fi
36+
}
37+
38+
# function: mysql_remove_database
39+
# Remove database from mysql
40+
41+
function mysql_remove_database {
42+
SQL="drop database if exists $db_name;";
43+
mysql -u$db_user -p$db_password -e "$SQL" || iferror "Database not removed";
44+
}
45+
46+
# function: remove_root_path
47+
# remove path of Wordpress installation
48+
49+
function remove_root_path {
50+
if [[ -d $wp_path ]]; then
51+
rm -rf $wp_path;
52+
else
53+
iferror "Root path does not exists"
54+
fi
55+
}
56+
57+
# function: nginx_disable_site
58+
# disable site from sites-enabled
59+
60+
function nginx_disable_site {
61+
if [[ -e /etc/nginx/sites-enabled/$domain.conf ]]; then
62+
rm -f /etc/nginx/sites-enabled/$domain.conf;
63+
else
64+
iferror "Site is not enabled"
65+
fi
66+
}
67+
68+
#function nginx_remove_site
69+
# remove server from sites-available
70+
71+
function nginx_remove_site {
72+
if [[ -e /etc/nginx/sites-available/$domain.conf ]]; then
73+
rm -f /etc/nginx/sites-available/$domain.conf;
74+
else
75+
iferror "Site is not available "
76+
fi
77+
}
78+
79+
function start {
80+
read_root_path \
81+
&& get_config_parameters \
82+
&& mysql_remove_database \
83+
&& remove_root_path \
84+
&& nginx_disable_site \
85+
&& nginx_remove_site
86+
}
87+
88+
if [[ $# -eq 0 ]]; then
89+
echo "You must enter a FQDN as parameter. \
90+
Example: ./uninstall_wordpress.sh evildomain.org"; exit 1;
91+
else
92+
domain=$1
93+
fi
94+
95+
if [ "$(id -u)" != "0" ]; then
96+
echo "This script must be run as root" 1>&2
97+
exit 1
98+
fi
99+
start

0 commit comments

Comments
 (0)
Please sign in to comment.