-
Notifications
You must be signed in to change notification settings - Fork 1
/
xoops-takeout.sh
executable file
·167 lines (141 loc) · 3.19 KB
/
xoops-takeout.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
157
158
159
160
161
162
163
164
165
166
#!/usr/bin/env bash
# xoops-takeout.sh
#
# "Take out XOOPS database and files fully"
#
# Usage
#
# $ /path/to/xoops-takeout.sh </path/to/mainfile.php> </path/to/export/directory>
#
# Website
#
# https://github.com/suin/xoops-takeout
#
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin
SCRIPT_NAME=$0
#
# Show help and usages
#
help(){
echo "Take out XOOPS database and files fully"
echo ""
echo "Usage:"
echo " \$ $SCRIPT_NAME <path/to/mainfile.php> <export-directory>"
echo " \$ $SCRIPT_NAME <path/to/mainfile.php> <export-directory> <rotate-limit>"
}
#
# Export XOOPS information
#
export_xoops_info() {
mainfile="$1"
php -r "
define('_LEGACY_PREVENT_LOAD_CORE_', 1);
error_reporting(0);
require '$mainfile';
echo 'export XOOPS_DB_USER=', XOOPS_DB_USER, PHP_EOL;
echo 'export XOOPS_DB_PASS=', XOOPS_DB_PASS, PHP_EOL;
echo 'export XOOPS_DB_HOST=', XOOPS_DB_HOST, PHP_EOL;
echo 'export XOOPS_DB_NAME=', XOOPS_DB_NAME, PHP_EOL;
echo 'export XOOPS_ROOT_PATH=', XOOPS_ROOT_PATH, PHP_EOL;
echo 'export XOOPS_TRUST_PATH=', XOOPS_TRUST_PATH, PHP_EOL;
"
}
#
# Make MySQL dump
#
make_mysql_dump() {
[ $# -eq 5 ] || return 1
host="$1"
user="$2"
pass="$3"
database="$4"
filename="$5"
if [ -z "$pass" ]
then
mysqldump "-h$host" "-u$user" $database > "$filename"
else
mysqldump "-h$host" "-u$user" "-p$pass" $database > "$filename"
fi
}
#
# Compress files
#
compress_files() {
directory="$1"
name="$2"
basename=$(basename $directory)
base=$(dirname $directory)
[ -d "$directory" ] || return 1
[ -z "$name" ] && name=$basename.tgz
tar czf $name -C $base $basename
}
#
# Do backup rotation
#
do_rotate() {
backup_directory=$1
name=$2
rotate_limit=$3
while [ $(ls $backup_directory/$name.*.tgz 2> /dev/null | wc -l) -gt $rotate_limit ]
do
rm -f $(ls $backup_directory/$name.*.tgz | head -1)
done
}
#
# Main function
#
main() {
if [ $# -lt 2 ]
then
help
return 1
fi
mainfile="$1"
backup_directory="$2"
date=$(date "+%y%m%d.%H%M")
do_rotate=0
if [ $# -gt 2 ]
then
if [ $3 -gt 0 ]
then
do_rotate=1
rotate_limit=$3
else
echo "Rotate limit must be more than or equals to 1"
return 1
fi
fi
if [ ! -f "$mainfile" ]
then
echo "mainfile.php not found: $mainfile"
return 1
fi
if [ ! -r "$mainfile" ]
then
echo "mainfile.php not readable: $mainfile"
return 1
fi
if [ ! -d "$backup_directory" ]
then
echo "Back up directory not found: $backup_directory"
return 1
fi
if [ ! -w "$backup_directory" ]
then
echo "Back up directory not writable: $backup_directory"
return 1
fi
$(export_xoops_info $mainfile)
mysql_backup_filename="/tmp/$XOOPS_DB_NAME.sql"
make_mysql_dump "$XOOPS_DB_HOST" "$XOOPS_DB_USER" "$XOOPS_DB_PASS" "$XOOPS_DB_NAME" "$mysql_backup_filename"
tar czvf "$backup_directory/$XOOPS_DB_NAME.$date.tgz" \
-C $(dirname "$mysql_backup_filename") $(basename "$mysql_backup_filename") \
-C $(dirname "$XOOPS_ROOT_PATH") $(basename "$XOOPS_ROOT_PATH") \
-C $(dirname "$XOOPS_TRUST_PATH") $(basename "$XOOPS_TRUST_PATH")
rm -f "$mysql_backup_filename"
if [ $do_rotate -eq 1 ]
then
do_rotate $backup_directory $XOOPS_DB_NAME $rotate_limit
fi
}
main $@