forked from Purii/simple-webapp-backup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
restore.sh
126 lines (114 loc) · 3.09 KB
/
restore.sh
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
#! /bin/sh
# simple-webapp-backup - A useful shellscript to backup and restore every Webapp which is based on MySQL and Files
# Copyright (C) 2014 Patrick Puritscher <[email protected]>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
#Config
APPNAME="Simple App"
APPSOURCEDIR="/var/www/webapp";
APPDATABASE="webapp";
APPDATABASEUSER="root"
RESTOREDIR="/var/www/webapp_backup";
usage() {
echo "usage: sh reset.sh [[[--create] [--restore] [--version]] | [-h]]"
}
##Restore App
restore() {
echo "$APPNAME will be restored..."
#Reset DB
echo "Please log in to the database:"
mysql -u $APPDATABASEUSER -p $APPDATABASE < $RESTOREDIR/dump.sql
#Reset destination
rm -R $APPSOURCEDIR/
mkdir $APPSOURCEDIR/
cp -Rp $RESTOREDIR/source/. $APPSOURCEDIR/.
echo "$APPNAME has been restored!"
}
##Backup App
create() {
if [ "$backupdefault" = "1" ]; then
echo -n "Overwrite Default-Backup?(y/n) > "
read overwritedefault
if [ "$overwritedefault" != "y" ]; then
echo "Backup cancelled!"
exit
fi
fi
echo "Creating new Backup.."
#Dump DB
echo "Please log in to the database:"
mysqldump -u $APPDATABASEUSER -p --add-drop-database $APPDATABASE > $RESTOREDIR/dump.sql
#Copy Files
if [ -d $RESTOREDIR/source/ ]; then
rm -R $RESTOREDIR/source/
fi
mkdir $RESTOREDIR/source/
cp -Rp $APPSOURCEDIR/. $RESTOREDIR/source/.
echo "Backup is ready!"
}
##Check Backupversion
checkversion() {
#Version existing?
if [ ! -d $RESTOREDIR/backup_"$backupversion" ]; then
echo "There's no backup called $backupversion"
echo -n "Do you want to create a new one?(y/n) > "
read dobackupdir
if [ "$dobackupdir" = "y" ]; then
mkdir $RESTOREDIR/backup_"$backupversion"
else
exit
fi
fi
}
if [ "$1" = "" ]; then
usage
exit;
fi
while [ "$1" != "" ]; do
case $1 in
--create ) create=1
;;
--restore ) restore=1
;;
--version ) version=1
;;
-h | --help ) usage
exit
;;
esac
shift
done
if [ "$version" = "1" ]; then
echo "Please enter a backupversion:"
read backupversion
checkversion
RESTOREDIR=$RESTOREDIR/backup_"$backupversion"
backupdefault=0
else
if [ ! -d $RESTOREDIR/_backup_default ]; then
mkdir $RESTOREDIR/_backup_default
fi
RESTOREDIR=$RESTOREDIR/_backup_default
backupdefault=1
fi
if [ "$restore" = "1" ]; then
restore
exit
fi
if [ "$create" = "1" ]; then
create
exit
fi