-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathWordPressBackup.sh
executable file
·106 lines (89 loc) · 3.37 KB
/
WordPressBackup.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
#!/bin/bash
# Author Roger Gentry (jamesrascal) - Host Kraken
# WP BACKUPS V1.9
# Build date 07/14/2014
# Parse flags
quiet=0
while getopts ":hq" opt; do
case ${opt} in
h )
echo "Usage:"
echo " WordPressBackup.sh -h Display this help message."
echo " WordPressBackup.sh -q Run quietly-output only on errors."
exit 0
;;
q )
quiet=1
;;
\? )
echo "Invalid Option: -$OPTARG" 1>&2
exit 1
;;
esac
done
shift $((OPTIND -1))
# Path where backup profiles are stored
FINDDIR=/home/centos/backups/profiles/
# Searches for the backup.profile
profile=$(find ${FINDDIR} -wholename "*backup.profile" )
for backupprofile in $profile ; do
if [ "${quiet}" = "0" ]; then
echo "********************************************************************";
echo "Using Profile: ${backupprofile}";
fi
. $backupprofile
if [ "${backup_enabled}" = "1" ]; then
wp_config=${wp_root}/wp-config.php
# Verifing that wp-config is in the location
if [ ! -f "$wp_config" ]; then
echo "No WP-Config.php Found Attempting to find one";
# Checks the directory above WP_root
cd $wp_root
wp_config=../wp-config.php
# IF wp-configstill not found
if [ ! -f "$wp_config" ]; then
echo "FATAL ERROR: wp-config.php is missing from a readable state";
echo "Does $wp_domain still work?";
fi
fi
# Verifing wp_config exists
if [ -f "$wp_config" ]; then
# BackupName Date and time
backupname=$(date +%Y-%m-%d)
# Pulls Database info from WP-config
db_name=$(grep DB_NAME "${wp_config}" | cut -f4 -d"'")
db_user=$(grep DB_USER "${wp_config}" | cut -f4 -d"'")
db_pass=$(grep DB_PASSWORD "${wp_config}" | cut -f4 -d"'")
table_prefix=$(grep table_prefix "${wp_config}" | cut -f2 -d"'")
# Creates a Backup Directory if one does not exist.
mkdir -p ${backup_location}/${user}/${wp_domain}/
# Make Backup location the current directory
cd ${backup_location}/${user}/${wp_domain}
# MySQL Takes a Dump and compress the Home Directory
mysqldump -u ${db_user} -p${db_pass} ${db_name} | gzip > ./${backupname}-DB.sql.gz &&
tar zcPf ./${backupname}-FILES.tar.gz ${wp_root}
# Compresses the MySQL Dump and the Home Directory
tar zcPf ./${wp_domain}-${backupname}.tar.gz ./${backupname}-FILES.tar.gz ./${backupname}-DB.sql.gz
chmod 600 ./${wp_domain}-${backupname}.tar.gz
# Generates the Backup Size
FILENAME=${backup_location}/${user}/${wp_domain}/${wp_domain}-${backupname}.tar.gz
FILESIZE=$(du -h "$FILENAME")
if [ "${quiet}" = "0" ]; then
echo "$FILESIZE"
fi
#Removes the SQL dump and Home DIR to conserve space
rm -rf ./${backupname}-FILES.tar.gz ./${backupname}-DB.sql.gz
#Deletes any Backup older than X days
find ${backup_location}/${user}/${wp_domain}/ -type f -mtime +${keepdays} -exec rm {} \;
fi
fi
if [ "${backupenabled}" = "0" ]; then
echo "Backups NOT enabled for ${wp_root}";
fi
done
if [ "${quiet}" = "0" ]; then
echo " ";
echo "********************************************************************";
echo "This script is licensed under GPL https://github.com/jamesrascal/wordpress-backup/";
echo "Run Date: $(date +%Y-%m-%d-%k-%M)";
fi