-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackup.sh
executable file
·134 lines (114 loc) · 3.38 KB
/
backup.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
#!/bin/sh
# @file:
# Automatic backup of database and files (code + upload)
# for WordPress
#
# This needs to be run in the webroot
# Any command which fail will cause the shell script to exit immediately
set -e
# BACKUP_DIR should be specified before running this command
if [ -z $BACKUP_DIR ]
then
echo 'BACKUP_DIR variable is not set' 1>&2;
echo 'You can set this by running the command like this:' 1>&2
echo " BACKUP_DIR=/tmp $0" 1>&2;
exit 1;
fi;
# 'php' is needed to extract database connection info
if ! which php 1>/dev/null
then
echo 'Error: php (php-cli) is not available' 1>&2
exit 1;
fi;
# tar is needed to archive
if ! which tar 1>/dev/null
then
echo 'Error: tar utility is not available' 1>&2
exit 1;
fi;
# gzip is needed to compress the archive
if ! which gzip 1>/dev/null
then
echo 'Error: gzip utility is not available' 1>&2;
exit 1;
fi;
# mysqldump is needed to export the database
if ! which mysqldump 1>/dev/null
then
echo 'Error: mysqldump is not available';
exit 1;
fi;
# Check if configuration file is present
if [ ! -f 'wp-config.php' ]
then
echo 'Error: could not find the wp-config.php file' 1>&2;
exit 1;
fi;
# Check if configuration file contains php closing tags
# PHP closing tags don't work with current method of
# extracting database credentials and are not recommended
if cat 'wp-config.php' | grep -q '?>'
then
echo 'Error: wp-config.php contains PHP closing tags ?>. Please remove them' 1>&2;
exit 1;
fi;
# Remove trailing / from BACKUP_DIR
BACKUP_DIR=${BACKUP_DIR%/};
# Variables
SITENAME="lopan.ch";
NOW=$(date +"%Y-%m-%d-%H%M");
FILE="$SITENAME.$NOW.tar";
WWW_DIR=$(pwd);
# Extract database connection details from wp-config.php
WPDB_USER=$(echo "`cat wp-config.php`; echo DB_USER;" | php -d error_reporting=0);
WPDB_PASS=$(echo "`cat wp-config.php`; echo DB_PASSWORD;" | php -d error_reporting=0);
WPDB_NAME=$(echo "`cat wp-config.php`; echo DB_NAME;" | php -d error_reporting=0);
WPDB_HOST=$(echo "`cat wp-config.php`; echo DB_HOST;" | php -d error_reporting=0);
WPDB_FILE="$SITENAME.$NOW.sql"
# Archive all the www folder
# -C $WWW_DIR:
# -s '/./www/gs': rename '.' with www inside the archive
# --format=ustar: prevent tar from creatign PaxHeader folder inside archives
tar \
-C $WWW_DIR \
-s '/./www/' \
--format=ustar \
--exclude=.git \
--exclude=.svn \
--exclude=cgi-bin \
--exclude=wp-content/updraft \
-cf $BACKUP_DIR/$FILE .
# Create a directory for the SQL dump
mkdir -p "$BACKUP_DIR/sql";
# Call mysqldump and ignore errors
# For example: 'Warning: Using a password on the command line ...
mysqldump \
--user="$WPDB_USER" \
--password="$WPDB_PASS" \
--host="$WPDB_HOST" \
$WPDB_NAME \
> "$BACKUP_DIR/sql/$WPDB_FILE" \
2>/dev/null;
# Add SQL dump to the archive
tar \
--format=ustar \
-C $BACKUP_DIR \
-rf $BACKUP_DIR/$FILE sql/
# Remove the sql file and folder since it's added to archive
rm -rf "${BACKUP_DIR}/sql"
# Compress the archive
# --best: best compressiong level
# --force: in case backup file already exists, overwrite it
gzip --best --force $BACKUP_DIR/$FILE;
# Print the backup file name
if [ -f "${BACKUP_DIR}/${FILE}.gz" ]
then
echo "${BACKUP_DIR}/${FILE}.gz";
elif [ -f "{$BACKUP_DIR}/{$FILE}" ]
then
echo "${BACKUP_DIR}/${FILE}";
else
echo "Backup file it's missing: ${BACKUP_DIR}/${FILE}[.gz]" 1>&2;
exit 1;
fi
exit 0;