-
Notifications
You must be signed in to change notification settings - Fork 0
/
wpbackup
268 lines (220 loc) · 6.7 KB
/
wpbackup
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#!/bin/bash
sudo sync; sync;
# ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- -----
#
# WP BackUp Script
# https://github.com/PRyC/WPBackUp
#
# ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- -----
#
# Required software:
# bash, sha512sum, p7zip, p7zip-full, wp-cli, sudo and optional s3cmd
#
# Using the script:
# sudo ./wpbackup [site name] [job]
# eg.:
# sudo ./wpbackup example.com full
#
# Remember to set the appropriate attributes for the script file:
# sudo chmod +x ./wpbackup
#
# ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- -----
# Chceck argument: site name (domain)
if [[ $1 == *"."* ]]; then
echo "----- ----- ----- ----- -----"
echo "\$1: OK ($1)"
else
echo ""
echo "\$1: Correct syntax: sudo wpbackup [SITE] [TYPE: major, full, db or pswd to show passwords]"
echo ""
exit 1
fi
# Chceck argument: job type (backup type)
if [[ $2 == "major" ]] || [[ $2 == "full" ]] || [[ $2 == "db" ]] || [[ $2 == "pswd" ]]; then
echo "\$2: OK ($2)"
echo "----- ----- ----- ----- -----"
else
echo ""
echo "\$2: Correct syntax: sudo wpbackup [SITE] [TYPE: major, full, db or pswd to show passwords]"
echo ""
exit 1
fi
# Chceck WP-CLI installed (for DB BackUp)
if ! [[ -x "$(command -v wp)" ]]; then
echo 'ERROR: WP-CLI is not installed!' >&2
exit 1
fi
# Chceck 7z installed
if ! [[ -x "$(command -v 7z)" ]]; then
echo 'ERROR: p7zip & p7zip-full is not installed!' >&2
exit 1
fi
SITE="$1"
BACKUP_TYPE="$2"
# +-+-+-+ +-+-+-+
# |S|e|t| |I|t|!| 1/9
# +-+-+-+ +-+-+-+
# Delete (0) or leave (1) the archive on the server
# "0" only works when backup on S3 is active!
KEEP_LOCAL=0
# Update WP + themes + plugins AFTER full or major backup (1 - yes):
UPDATE=1
# ----- ----- SITE DIR schema ----- -----
# +-+-+-+ +-+-+-+
# |S|e|t| |I|t|!| 2/9
# +-+-+-+ +-+-+-+
SITE_DIR="/var/www/"$SITE"/public_html"
# ----- ----- SITE DIR schema ----- -----
if [ $BACKUP_TYPE != "pswd" ] && [[ ! -f "$SITE_DIR"/wp-config.php ]]; then
echo ""
echo "Missing wp-config.php file. Are You sure this is a WordPress site?"
echo ""
exit 0
fi
# ----- ----- SPLIT TAR ARCHIVE ----- -----
# +-+-+-+ +-+-+-+
# |S|e|t| |I|t|!| 3/9
# +-+-+-+ +-+-+-+
# Set 1 to split:
SPLIT_ARCH=0
# Split archive size, eg 20M = 20 MB
SPLIT_ARCH_SIZE=20M
# Restore:
# cat FILE_BAME.tar.gz_split-* | tar xzvf -
# ----- ----- SPLIT TAR ARCHIVE ----- -----
# ----- ----- TAR ARCHIVE EXCLUDE ----- -----
# +-+-+-+ +-+-+-+
# |S|e|t| |I|t|!| 4/9
# +-+-+-+ +-+-+-+
EXCLUDE="--exclude=$SITE_DIR/wp-content/upgrade --exclude=$SITE_DIR/wp-content/cache --exclude=$SITE_DIR/wp-content/litespeed --exclude=$SITE_DIR/wp-content/.snapshots --exclude=$SITE_DIR/.git"
# ----- ----- TAR ARCHIVE EXCLUDE ----- -----
# ----- ----- S3 CONFIGURATION ----- -----
# !!! REMEMBER TO INSTALL AND CONFIGURE S3CMD !!!
# +-+-+-+ +-+-+-+
# |S|e|t| |I|t|!| 5/9
# +-+-+-+ +-+-+-+
# Set to 1 to activate S3
BACKUP2S3=0
# +-+-+-+ +-+-+-+
# |S|e|t| |I|t|!| 6/9
# +-+-+-+ +-+-+-+
# For example.com S3 cfg file:
# /root/.s3cfg-oss-wpbackup/.example.com
S3CFG=/root/.s3cfg-oss-wpbackup/.$SITE
# +-+-+-+ +-+-+-+
# |S|e|t| |I|t|!| 7/9
# +-+-+-+ +-+-+-+
# s3://[BUCKET-NAME]/$SITE/
S3DIR=s3://wpbackup/$SITE/
# !!! REMEMBER TO INSTALL AND CONFIGURE S3CMD !!!
# ----- ----- S3 CONFIGURATION ----- -----
function f_pswd() {
# +-+-+-+ +-+-+-+
# |S|e|t| |I|t|!| 8/9
# +-+-+-+ +-+-+-+
# Set BASE PASSWORD for archive (eg. 512 a-z A-Z 0-9):
PSWD1=
# SHA512: site name
PSWD2="$(echo -n $SITE | sha512sum | cut -c1-128)"
# MASTER PASSWORD for archive (BASE + SHA512):
BACKUP_ARCHIVE_PASSWORD=$PSWD1$PSWD2
}
# Date and time generation
function f_time() {
DATA_D="$(date +"%Y%m%d")"
DATA_H="$(date +"%H%M%S")"
FILE_TIME=$DATA_D"-"$DATA_H
}
# File and directory names
function f_dir() {
f_time
# ----- ----- Set MAIN working folder ----- -----
# +-+-+-+ +-+-+-+
# |S|e|t| |I|t|!| 9/9
# +-+-+-+ +-+-+-+
BACKUP_MAIN_DIR="/backup/"
# ----- ----- Set MAIN working folder ----- -----
BACKUP_TEMP_DIR=$BACKUP_MAIN_DIR$SITE"_"$FILE_TIME"/"
BACKUP_ARCH_DIR=$BACKUP_MAIN_DIR"archive/"
}
# Create the required folders
function f_makedir() {
f_dir
if [[ ! -d "$BACKUP_MAIN_DIR" ]]; then
sudo mkdir $BACKUP_MAIN_DIR
fi
if [[ ! -d "$BACKUP_TEMP_DIR" ]]; then
sudo mkdir $BACKUP_TEMP_DIR
fi
if [[ ! -d "$BACKUP_ARCH_DIR" ]]; then
sudo mkdir $BACKUP_ARCH_DIR
fi
}
function f_backup {
f_makedir
echo "----- ----- ----- ----- -----"
echo "SITE: "$SITE
echo "Typ: "$BACKUP_TYPE
echo "----- ----- ----- ----- -----"
echo "Database dump..."
sudo wp db export $BACKUP_TEMP_DIR$SITE"_db_"$FILE_TIME.sql --allow-root --path=$SITE_DIR/
if [[ $BACKUP_TYPE == full ]] || [[ $BACKUP_TYPE == major ]]; then
echo "Copying files..."
sudo tar czPf $BACKUP_TEMP_DIR$SITE"_www_"$FILE_TIME.tar.gz $EXCLUDE $SITE_DIR/
fi
if [[ $BACKUP_TYPE == full ]] && [[ $SPLIT_ARCH == 1 ]] || [[ $BACKUP_TYPE == major ]] && [[ $SPLIT_ARCH == 1 ]]; then
echo "Split file..."
sudo split -b $SPLIT_ARCH_SIZE $BACKUP_TEMP_DIR$SITE"_www_"$FILE_TIME.tar.gz "$BACKUP_TEMP_DIR$SITE"_www_"$FILE_TIME.tar.gz_split-"
echo "Deleting base ARCH (no split)"
sudo rm -rf $BACKUP_TEMP_DIR$SITE"_www_"$FILE_TIME.tar.gz
fi
echo "Packing files..."
f_pswd
# 7zip MAN:
# 7z a [zipfile-name] [files-to-be-zipped]
# -p : Set Password
# -mx=9 : Ultra compression
# -mx=9 : normal --//--
# -mmt=1 : 1 core CPU
# -mhe : Encrypt file names
# -t7z : File type .7z
sudo 7z a $BACKUP_ARCH_DIR$SITE"_wpbackup_"$BACKUP_TYPE"_"$FILE_TIME.7z $BACKUP_TEMP_DIR -t7z -mhe -mx=5 -mmt=1 -p$BACKUP_ARCHIVE_PASSWORD
if [[ $BACKUP2S3 == 1 ]]; then
echo "Sending to S3..."
sudo s3cmd put $BACKUP_ARCH_DIR$SITE"_wpbackup_"$BACKUP_TYPE"_"$FILE_TIME.7z $S3DIR$BACKUP_TYPE"/" --disable-multipart -c $S3CFG
fi
# ----- ----- ----- ----- ----- ----- ----- -----
# Cleaning :
# Delete temporary folder
sudo rm -rf $BACKUP_TEMP_DIR
# Deleting the file (only if the copy on S3 and KEEP_LOCAL=0, otherwise stays)
if [[ $KEEP_LOCAL == 0 ]] && [[ $BACKUP2S3 == 1 ]]; then
echo "Deleting a local file (backup archive)"
sudo rm -rf $BACKUP_ARCH_DIR$SITE"_wpbackup_"$BACKUP_TYPE"_"$FILE_TIME.7z
fi
# ----- ----- ----- ----- ----- ----- ----- -----
# ----- ----- ----- ----- ----- ----- ----- -----
if [[ $UPDATE == 1 ]]; then
if [[ $BACKUP_TYPE == full ]] || [[ $BACKUP_TYPE == major ]]; then
echo "Update..."
cd $SITE_DIR
sudo wp core update --allow-root
sudo wp plugin update --all --allow-root
sudo wp theme update --all --allow-root
fi
fi
# ----- ----- ----- ----- ----- ----- ----- -----
}
if [[ $BACKUP_TYPE == pswd ]]; then
f_pswd
echo ""
echo "PSWD1/2: "$PSWD1
echo ""
echo "PSWD2/2: "$PSWD2
echo ""
echo "PSWD: "$BACKUP_ARCHIVE_PASSWORD
echo ""
else
f_backup
fi
exit