-
Notifications
You must be signed in to change notification settings - Fork 1
/
wpBackup.sh
executable file
·158 lines (132 loc) · 4.9 KB
/
wpBackup.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
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
#!/bin/sh
#backup and restore wordpress data
# Check for root privileges
if ! [ $(id -u) = 0 ]; then
echo "This script must be run with root privileges"
exit 1
fi
# Initialize defaults
cron=""
POOL_PATH=""
APPS_PATH=""
CONFIG_PATH=""
JAIL_NAME=""
WP_SOURCE=""
WP_DESTINATION=""
BACKUP_PATH=""
BACKUP_NAME=""
DATABASE_NAME=""
DB_BACKUP_NAME=""
DB_PASSWORD=""
SCRIPT=$(readlink -f "$0")
SCRIPTPATH=$(dirname "$SCRIPT")
. $SCRIPTPATH/WordpressBackup-config
CONFIGS_PATH=$SCRIPTPATH/configs
# Check for WordpressBackup-config and set configuration
if ! [ -e $SCRIPTPATH/WordpressBackup-config ]; then
echo "$SCRIPTPATH/WordpressBackup-config must exist."
exit 1
fi
# Check that necessary variables were set by WordpressBackup-config
if [ -z $POOL_PATH ]; then
echo 'Configuration error: POOL_PATH must be set'
exit 1
fi
if [ -z $APPS_PATH ]; then
echo 'Configuration error: APPS_PATH must be set'
exit 1
fi
if [ -z $CONFIG_PATH ]; then
echo 'Configuration error: CONFIG_PATH must be set'
exit 1
fi
if [ -z $JAIL_NAME ]; then
echo 'Configuration error: JAIL_NAME must be set'
exit 1
fi
if [ -z $WP_SOURCE ]; then
echo 'Configuration error: WP_SOURCE must be set'
exit 1
fi
if [ -z $WP_DESTINATION ]; then
echo 'Configuration error: WP_DESTINATION must be set'
exit 1
fi
if [ -z $BACKUP_PATH ]; then
echo 'Configuration error: BACKUP_PATH must be set'
exit 1
fi
if [ -z $BACKUP_NAME ]; then
echo 'Configuration error: BACKUP_NAME must be set'
exit 1
fi
if [ -z $DB_PASSWORD ]; then
echo 'Configuration error: DB_PASSWORD must be set'
exit 1
fi
if [ ! -d "${POOL_PATH}/${APPS_PATH}/${WP_SOURCE}" ]; then
echo
echo "You made a WordpressBackup-config error the WP_SOURCE directory ${POOL_PATH}/${APPS_PATH}/${WP_SOURCE} does not exist"
mkdir -p ${POOL_PATH}/${APPS_PATH}/${WP_SOURCE}
echo
echo "The WP_SOURCE directory ${POOL_PATH}/${APPS_PATH}/${WP_SOURCE} has been created for you"
echo
echo "Please run the script again to use that directory or edit the WordpressBackup-config"
exit 1
fi
if [ ${POOL_PATH} == "/" ]; then
POOL_PATH=""
fi
echo $POOL_PATH
#cron="yes"
#POOL_PATH="/mnt/v1"
#APPS_PATH="apps"
#WP_SOURCE="wordpresspass"
#WP_DESTINATION="wordpresspass2"
#BACKUP_PATH="apps"
#BACKUP_NAME="wordpressbackup.tar.gz"
if [ "$cron" != "yes" ]; then
read -p "Enter '(B)ackup' to backup Wordpress or '(R)estore' to restore Wordpress: " choice
fi
echo
if [ "${cron}" == "yes" ]; then
choice="B"
#echo "cron = $cron"
fi
echo
if [ ${choice} == "B" ] || [ ${choice} == "b" ]; then
if [ ! -d "${POOL_PATH}/${BACKUP_PATH}" ]; then
mkdir -p ${POOL_PATH}/${BACKUP_PATH}
echo "mkdir -p ${POOL_PATH}/${BACKUP_PATH}"
fi
# to backup
#tar --exclude=./Plex\ Media\ Server/Cache -zcvf /mnt/v1/apps/wordpressbackup.tar.gz ./
#iocage exec ${JAIL_NAME} "mysqldump --single-transaction -h localhost -u "root" -p"${DB_PASSWORD}" "${DATABASE_NAME}" > "/${POOL_PATH}/${APPS_PATH}/${WP_SOURCE}/${DB_BACKUP_NAME}""
iocage exec ${JAIL_NAME} "mysqldump --single-transaction -h localhost -u "root" -p"${DB_PASSWORD}" "${DATABASE_NAME}" > "/${CONFIG_PATH}/${DB_BACKUP_NAME}""
#iocage exec ${JAIL_NAME} "mysqldump --defaults-extra-file=.my.cnf -u root "${DATABASE_NAME}" --single-transaction --quick --lock-tables=false > "/${CONFIG_PATH}/${DB_BACKUP_NAME}""
echo "Wordpress database backup ${DB_BACKUP_NAME} complete"
#ls -l /${POOL_PATH}/${BACKUP_PATH}/
cd ${POOL_PATH}/${APPS_PATH}/${WP_SOURCE}
echo "cd ${POOL_PATH}/${APPS_PATH}/${WP_SOURCE}"
tar -zcf ${POOL_PATH}/${BACKUP_PATH}/${BACKUP_NAME} ./
echo "tar-zcvf ${POOL_PATH}/${BACKUP_PATH}/${BACKUP_NAME} ./"
echo "Backup complete file located at ${POOL_PATH}/${BACKUP_PATH}/${BACKUP_NAME}"
chmod 400 ${POOL_PATH}/${BACKUP_PATH}/${BACKUP_NAME}
elif [ $choice == "R" ] || [ $choice == "r" ]; then
# to restore wordpressbackup to directory wordpresspass2
if [ ! -d "${POOL_PATH}/${APPS_PATH}/${WP_DESTINATION}" ]; then
mkdir -p ${POOL_PATH}/${APPS_PATH}/${WP_DESTINATION}
echo "mkdir -p ${POOL_PATH}/${APPS_PATH}/${WP_DESTINATION}"
fi
#tar xf wordpressbackup.tar.gz -C /mnt/v1/apps/wordpresspass2/
tar zvxf ${POOL_PATH}/${BACKUP_PATH}/${BACKUP_NAME} -C ${POOL_PATH}/${APPS_PATH}/${WP_DESTINATION}
echo "tar xf ${POOL_PATH}/${BACKUP_PATH}/${BACKUP_NAME} -C ${POOL_PATH}/${APPS_PATH}/${WP_DESTINATION}"
echo "restore database"
#iocage exec ${JAIL_NAME} "mysqldump --single-transaction -h localhost -u "root" -p"${DB_PASSWORD}" --databases "${DATABASE}" < "/${CONFIG_PATH}/${DB_BACKUP_NAME}""
iocage exec ${JAIL_NAME} "mysql -u "root" -p"${DB_PASSWORD}" "${DATABASE_NAME}" < "/${CONFIG_PATH}/${DB_BACKUP_NAME}""
#iocage exec wordpress "mysql -u "root" -p "wordpress" < "/${CONFIG_PATH}/wordpressorig.sql""
echo "mysql -u root -p wordpress < /${CONFIG_PATH}/${DB_BACKUP_NAME}"
echo "Restore completed at ${POOL_PATH}/${APPS_PATH}/${WP_DESTINATION}"
else
echo "Must enter '(B)ackup' to backup Wordpress or '(R)estore' to restore Wordpress: "
fi