Skip to content

Commit e0c21c8

Browse files
1.0.2: Added ability to backup and restore web config and ssl certs
1 parent 6a214a4 commit e0c21c8

8 files changed

+59
-28
lines changed

.gitignore

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
.idea
33
.vagrant
44
/www
5-
/mysql_backups
6-
/mysql_data
5+
/backup
6+
/mysql
77
/config.yml

files/00-aliases.sh

+21-4
Original file line numberDiff line numberDiff line change
@@ -76,24 +76,41 @@ function backupMysql {
7676
;;
7777
*)
7878
echo "Dumping database: $db"
79-
mysqldump -uroot --databases $db | gzip > /srv/mysql_backups/$db.sql.gz
79+
mysqldump -uroot --databases $db | gzip > /srv/backup/mysql/$db.sql.gz
8080
;;
8181
esac
8282
done
83-
pt-show-grants -uroot -proot | gzip > /srv/mysql_backups/mysql_users.sql.gz
83+
pt-show-grants -uroot -proot | gzip > /srv/backup/mysql/mysql_users.sql.gz
8484
export MYSQL_PWD=''
8585
}
8686

8787
function restoreMysql {
8888
export MYSQL_PWD='root'
89-
databases=`ls -1 backupdbs.*.sql`
89+
databases=`ls -1 /srv/backup/mysql/*.sql.gz`
9090
for db in $databases; do
9191
echo "Importing $db ..."
92-
zcat $db | mysql -u $USER
92+
zcat $db | mysql -u root
9393
done
9494
export MYSQL_PWD=''
9595
}
9696

97+
function backupWebconfig {
98+
if [ -f /srv/backup/webconfig/config.tar ]; then
99+
rm -f /srv/backup/webconfig/config.tar
100+
fi
101+
tar -cf /srv/backup/webconfig/config.tar /etc/apache2/sites-available/200-* /etc/apache2/sites-enabled/200-* /etc/apache2/ssl
102+
}
103+
104+
function restoreWebconfig {
105+
if [ -f /srv/backup/webconfig/config.tar ]; then
106+
cd /
107+
sudo tar -xf /srv/backup/webconfig/config.tar
108+
cd -
109+
else
110+
echo "Error - no /srv/backup/webconfig/config.tar file is present. You need to execute backupWebconfig first."
111+
fi
112+
}
113+
97114
function phpRestart() {
98115
source /vagrant/php_versions.sh
99116
for i in "${php_versions[@]}"; do

files/vhost.sh

+8-8
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ function add_vhost {
175175
#chown USER:USER $DocumentRoot #POSSIBLE IMPLEMENTATION, new flag -u ?
176176
fi
177177

178-
create_vhost > /etc/apache2/sites-available/${ServerName}.conf
178+
create_vhost > /etc/apache2/sites-available/200-${ServerName}.conf
179179

180180
# Make directory to place SSL Certificate if it doesn't exists
181181
if [[ ! -d $KeyPath ]]; then
@@ -198,10 +198,10 @@ function add_vhost {
198198
fi
199199
fi
200200

201-
create_ssl_vhost >> /etc/apache2/sites-available/${ServerName}.conf
201+
create_ssl_vhost >> /etc/apache2/sites-available/200-${ServerName}.conf
202202

203203
# Enable Site
204-
cd /etc/apache2/sites-available/ && a2ensite ${ServerName}.conf
204+
cd /etc/apache2/sites-available/ && a2ensite 200-${ServerName}.conf
205205

206206
# Add entry to hosts
207207
if ! grep -q "127.0.0.1 $ServerName" /etc/hosts ; then
@@ -211,14 +211,14 @@ function add_vhost {
211211
}
212212

213213
function remove_vhost {
214-
if [ ! -f "/etc/apache2/sites-available/$ServerName.conf" ]; then
214+
if [ ! -f "/etc/apache2/sites-available/200-$ServerName.conf" ]; then
215215
echo "vHost $ServerName not found. Aborting"
216216
show_usage
217217
fi
218-
if [ -f "/etc/apache2/sites-enabled/$ServerName.conf" ]; then
219-
cd /etc/apache2/sites-available/ && a2dissite ${ServerName}.conf
218+
if [ -f "/etc/apache2/sites-enabled/200-$ServerName.conf" ]; then
219+
cd /etc/apache2/sites-available/ && a2dissite 200-${ServerName}.conf
220220
fi
221-
rm /etc/apache2/sites-available/${ServerName}.conf
221+
rm /etc/apache2/sites-available/200-${ServerName}.conf
222222
service apache2 reload
223223
}
224224

@@ -315,7 +315,7 @@ elif [ "$Task" = "add" ] ; then
315315
elif [ "$CertPath" == "" ] && [ "$KeyPath" != "" ]; then
316316
echo 'When supplying KeyPath, CertPath must also be supplied!! Aborting'
317317
show_usage
318-
elif [ -f "/etc/apache2/sites-available/$ServerName.conf" ]; then
318+
elif [ -f "/etc/apache2/sites-available/200-$ServerName.conf" ]; then
319319
if confirm "vHost $ServerName already exists. Remove and Recreate? [y/N]" ; then
320320
remove_vhost
321321
else

files/welcome.txt

+12-4
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,12 @@ The following functions and aliases are available:
77
\e[1mGENERAL:\e[0;32m
88
mem - Shows percentage of memory you have remaining in this VM environment
99
vhelp - Displays this help message
10-
www - Changes to the /srv/www directory
1110

1211

1312
\e[1mMYSQL:\e[0;32m
14-
backupMysql - Makes a dump of all your mysql databases and users into /srv/mysql_backups.
13+
backupMysql - Makes a dump of all your mysql databases and users into /srv/backup/mysql
1514
This is shared on your host machine so will survive a \e[33mvagrant destroy\e[0;32m
16-
restoreMysql - Restores all your mysql databases and users from /srv/mysql_backups.
15+
restoreMysql - Restores all your mysql databases and users from /srv/backup/mysql.
1716
This is shared on your host machine so will survive a \e[33mvagrant destroy\e[0;32m
1817

1918
\e[1mMAGENTO:\e[0;32m
@@ -33,4 +32,13 @@ The following functions and aliases are available:
3332
xdebug 0|1 - Enables / Disables xdebug for all installed PHP versions (switching off saves time and memory)
3433

3534
The following PHP versions are available with this release and may be invoked in shell via their aliases:
36-
###php_versions###\e[0m
35+
###php_versions###\e[0;32m
36+
37+
\e[1mWEB:\e[0;32m
38+
backupWebConfig - Makes a dump of all your VHost configuration settings and SSL certificates into /srv/backup
39+
This is shared on your host machine so will survive a \e[33mvagrant destroy\e[0;32m
40+
restoreWebConfig - Restores all of your VHost configuration settings and SSL certificates from /srv/backup
41+
This is shared on your host machine so will survive a \e[33mvagrant destroy\e[0;32m
42+
www - Changes to the /srv/www directory
43+
44+
\e[0m

scripts/000-setup_environment.sh

+4
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ while IFS='' read -r line || [[ -n "$line" ]]; do
3636
fi
3737
done </vagrant/files/hosts.txt
3838

39+
# Create backup folders for mysql and web config
40+
mkdir -f /srv/backup/mysql
41+
mkdir -f /srv/backup/webconfig
42+
3943
# Copy bash aliases and welcome message for all users
4044
cp /vagrant/files/00-aliases.sh /etc/profile.d/
4145
cp /vagrant/files/99-welcome.sh /etc/profile.d/

scripts/100-setup_apache.sh

+4-2
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@ a2enmod rewrite actions ssl headers
1313
a2enmod proxy_fcgi
1414
a2enmod proxy_http
1515

16-
#Change Listen Port
16+
# Change Listen Port
1717
sed -i.bak 's/Listen 80$/Listen 8090/' /etc/apache2/ports.conf
1818

19-
#Change user and groups to vagrant
19+
# Change user and groups to vagrant
2020
sed -i.bak 's/www-data$/vagrant/' /etc/apache2/envvars
2121

22+
rm /etc/apache2/sites-available/default-ssl.conf
23+
2224
# Setup VHOST Script
2325
yes | cp -rf /vagrant/files/vhost.sh /usr/local/bin/vhost
2426
chmod +x /usr/local/bin/vhost

scripts/400-setup_mysql.sh

+5-5
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ if [ ! -f /etc/init.d/mysql* ]; then
2222

2323
sed -i "s/bind-address.*/bind-address = 0.0.0.0/" /etc/mysql/my.cnf
2424
sed -i "s/max_allowed_packet.*/max_allowed_packet = 64M/" /etc/mysql/my.cnf
25-
sed -i "s/datadir.*/datadir = \/srv\/mysql_data/" /etc/mysql/my.cnf
26-
if [ ! -d /srv/mysql_data/mysql ]; then
27-
echo "Moving mysql databases from /var/lib/mysql/ to /srv/mysql_data ..."
28-
mv /var/lib/mysql/* /srv/mysql_data
25+
sed -i "s/datadir.*/datadir = \/srv\/mysql/data/" /etc/mysql/my.cnf
26+
if [ ! -d /srv/mysql/data/mysql ]; then
27+
echo "Moving mysql databases from /var/lib/mysql/ to /srv/mysql/data ..."
28+
mv /var/lib/mysql/* /srv/mysql/data
2929
else
30-
echo "Not moving mysql databases from /var/lib/mysql/ to /srv/mysql_data since data is already present there"
30+
echo "Not moving mysql databases from /var/lib/mysql/ to /srv/mysql/data since data is already present there"
3131
fi
3232
service mysql start
3333
export MYSQL_PWD='root'

scripts/600-setup_mail.sh

+3-3
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ EOL
4141
sudo service mailhog start
4242
fi
4343

44-
if [ ! -f /etc/apache2/sites-available/mailhog.demacmedia.com.conf ]; then
44+
if [ ! -f /etc/apache2/sites-available/100-mailhog.demacmedia.com.conf ]; then
4545
# Add Vhost
46-
sudo tee /etc/apache2/sites-available/mailhog.demacmedia.com.conf <<EOL
46+
sudo tee /etc/apache2/sites-available/100-mailhog.demacmedia.com.conf <<EOL
4747
<VirtualHost *:8090>
4848
ProxyPreserveHost On
4949
ProxyRequests Off
@@ -54,7 +54,7 @@ if [ ! -f /etc/apache2/sites-available/mailhog.demacmedia.com.conf ]; then
5454
EOL
5555

5656
# Enable Vhost
57-
sudo a2ensite mailhog.demacmedia.com
57+
sudo a2ensite 100-mailhog.demacmedia.com
5858

5959
# Reload apache
6060
sudo service apache2 reload

0 commit comments

Comments
 (0)