Repository navigation
Expand file tree
/
Copy pathdumpdatabase
More file actions
executable file
·239 lines (215 loc) · 7.6 KB
/
Copy pathdumpdatabase
File metadata and controls
executable file
·239 lines (215 loc) · 7.6 KB
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
#!/usr/local/bin/zsh
STARTTIME=$(date +%s)
FileName=$(date +"%Y%m%d")
FileDateDir="$(date +%F)"
FileDateDir1=$(date +%F --date="1 day ago")
FileDateDir7=$(date +%F --date="7 days ago")
FileName1=$(date +%Y%m%d --date="1 day ago")
FileName7=$(date +%Y%m%d --date="7 days ago")
DestDrive="DropBox"
DestDir="mariadb-Backups"
terrors=0
localDir="${DestDir}/${FileDateDir}"
LOG_FILE="/tmp/rclone_move.log"
DBFileName="mariadb-dir-${FileName}.tgz"
# Remove localDir Directory if it Exists
printf "Checking if /${localDir} exists..."
if [[ -d "/${localDir}" ]]; then
rm -rf "/${localDir}" >/dev/null 2>&1
fi
echo "Done."
# Verify the folder exists on the remote
printf "Checking if ${DestDrive}:/${DestDir}/${FileDateDir} exists..."
rclone lsd "${DestDrive}:/${DestDir}/${FileDateDir}" >/dev/null 2>&1
if [[ $? -eq 0 ]]; then
{ rclone purge "${DestDrive}:/${DestDir}/${FileDateDir}" \
--log-file="${LOG_FILE}" \
--log-level INFO >/dev/null 2>&1 } && echo "Done." || { echo "Errors"; ((terrors += 1)); }
else
echo "Done."
fi
rm -f "/${LOG_FILE}" >/dev/null 2>&1
# Create directory with error checking
printf "\nMaking /${localDir}/ Directory\n\n"
mkdir -p "/${localDir}" || { echo "Failed to create directory ${localDir}"; exit 1; }
cd "/${DestDir}" || { echo "Failed to cd to /${DestDir}"; exit 1; }
backupnow () {
local db=${1}
ThisFile="${db}-${FileName}.sql.gz"
printf "*** %s\n" "${db}"
printf "Exporting ${db} database(s) to %s..." "/${localDir}/${ThisFile}"
if [ "${db}" = "all" ]; then
{ mariadb-dump -uroot --all-databases \
--single-transaction \
--quick \
--lock-tables=false |\
gzip -c > /${localDir}/${ThisFile}; } >/dev/null 2>&1 && \
printf "Done.\n" || \
{ printf " Errors.\n"
((terrors+1))
printf "\n%s DB Backup with errors.\n\n" "${db}"; }
else
{ mariadb-dump -uroot --databases ${db} \
--single-transaction \
--quick \
--lock-tables=false |\
gzip -c > /${localDir}/${ThisFile}; } >/dev/null 2>&1 && \
printf "Done.\n" || \
{ printf " Errors.\n"
((terrors+1))
printf "\n%s DB Backup with errors.\n\n" "${db}"; }
fi
}
backupmysql () {
echo -e "\n*** Backing up /var/lib/mysql."
# Stop cPanel's service monitoring (tailwatchd or chkservd)
printf "Stopping cPanel service monitoring..."
if systemctl is-active --quiet tailwatchd; then
/scripts/restartsrv_tailwatchd --stop >/dev/null 2>&1 && echo "Done." || { echo "Errors"; ((terrors += 1)); }
elif systemctl is-active --quiet chkservd; then
/scripts/restartsrv_chkservd --stop >/dev/null 2>&1 && echo "Done." || { echo "Errors"; ((terrors += 1)); }
else
echo "Warning: Neither tailwatchd nor chkservd is running"
fi
# Verify monitoring is stopped
if systemctl is-active --quiet tailwatchd || systemctl is-active --quiet chkservd; then
echo "Error: Failed to stop cPanel service monitoring"
((terrors += 1))
fi
# Stop MariaDB service
printf "Stopping MariaDB service..."
systemctl stop mariadb >/dev/null 2>&1
if [[ $? -ne 0 ]]; then
echo "Error: Failed to stop MariaDB."
((terrors += 1))
printf "Restarting cpanel services..."
# Attempt to restart monitoring before exiting
/scripts/restartsrv_tailwatchd --start >/dev/null 2>&1
/scripts/restartsrv_chkservd --start >/dev/null 2>&1
((terrors += 1))
echo "Error."
exit 1
else
echo "Done."
fi
# Verify MariaDB is stopped
if systemctl is-active --quiet mariadb; then
echo "Error: MariaDB is still running."
((terrors += 1))
printf "\nRestarting cpanel monitoring before exiting.\n"
printf "Starting cpanel tailwatchd..."
/scripts/restartsrv_tailwatchd --start >/dev/null 2>&1 && echo "Done." || { echo "Errors"; ((terrors += 1)); }
printf "Starting cpanel chkservd..."
/scripts/restartsrv_chkservd --start >/dev/null 2>&1 && echo "Done." || { echo "Errors"; ((terrors += 1)); }
fi
# Back up the data directory
cd /var/lib || { echo "Failed to cd to /var/lib"; ((terrors += 1)); return 1; }
echo "Making ${DBFileName} tarbal in /${localDir}/"
tar -czf "/${localDir}/${DBFileName}" mysql >/dev/null 2>&1
if [[ $? -eq 0 ]]; then
echo "${DBFileName} tarbal created successfully."
else
# Restart cpanel monitoring before exiting
printf "\n%s Tarbal Errors. Restarting MariaDB and cpanel services.\n" "${localDir}/${DBFileName}"
((terrors += 1))
printf "Starting cpanel tailwatchd..."
/scripts/restartsrv_tailwatchd --start >/dev/null 2>&1 && echo "Done." || { echo "Errors"; ((terrors += 1)); }
printf "Starting cpanel chkservd..."
/scripts/restartsrv_chkservd --start >/dev/null 2>&1 && echo "Done." || { echo "Errors"; ((terrors += 1)); }
fi
# Restart MariaDB service
printf "Starting MariaDB service..."
systemctl start mariadb
if [[ $? -ne 0 ]]; then
# Restart MariaDB and monitoring before exiting
echo "Error: Failed to start MariaDB."
((terrors += 1))
else
echo "Done."
fi
# Verify MariaDB is running
if ! systemctl is-active --quiet mariadb; then
echo "Error: MariaDB failed to start"
((terrors += 1))
fi
echo "Restarting cPanel service monitoring."
printf "Starting cpanel tailwatchd..."
/scripts/restartsrv_tailwatchd --start >/dev/null 2>&1 && echo "Done." || { echo "Errors"; ((terrors += 1)); }
printf "Starting cpanel chkservd..."
/scripts/restartsrv_chkservd --start >/dev/null 2>&1 && echo "Done." || { echo "Errors"; ((terrors += 1)); }
}
movebackup() {
# Ensure source directory exists
if [[ ! -d "/${localDir}" ]]; then
echo "Error: Source directory /${localDir} does not exist"
exit 1
fi
# Move directory contents to remote repository
echo "*** Moving /${localDir} to ${DestDrive}:${DestDir}"
rclone move "/${localDir}" "${DestDrive}:/${DestDir}/${FileDateDir}" \
--cache-db-purge \
--buffer-size 64M \
--transfers 3 \
--drive-use-trash=true \
--log-file="${LOG_FILE}" \
--log-level INFO \
--dropbox-chunk-size 150M \
>/dev/null 2>&1
# Check if the move was successful
if [[ $? -eq 0 ]]; then
echo "Successfully moved /${localDir} to ${DestDrive}:/${DestDir}"
echo "Log file: ${LOG_FILE}"
rm -rf ${localDir}
else
echo "Error moving directory. Check ${LOG_FILE} for details"
exit 1
fi
echo "Move operation completed"
}
deleteoldbackups() {
local errors=0
local maxNrOfBackups=8
echo "\n**** Delete Old DB Backups"
nrOfBackups=$(rclone lsf "${DestDrive}:/${DestDir}" --include "/2*/" --max-depth 1 | wc -l)
if [ ${nrOfBackups} -gt ${maxNrOfBackups} ]; then
echo " ** Removing old backups"
rclone lsf "${DestDrive}:/${DestDir}" --include "/2*/" | \
sort -r | tail -n $((nrOfBackups - maxNrOfBackups)) | \
while read -r dirToRemove; do
printf " rclone purge %s..." "${DestDrive}:/${DestDir}/${dirToRemove}"
rclone purge "${DestDrive}:/${DestDir}/${dirToRemove}" \
--log-file="${LOG_FILE}" \
--log-level INFO \
--drive-use-trash=true
if [ $? -eq 0 ]; then
echo "Done"
else
echo "Errors."
((errors += 1))
((terrors += 1))
fi
done
else
echo " ** No Old Backups to Delete"
fi
echo
rm -rf /${DestDir}/*
}
for DB in $(mariadb -e 'show databases' -s --skip-column-names); do
if [[ "$DB" != "information_schema" && "$DB" != "performance_schema" ]]; then
backupnow "${DB}"
fi
done
backupnow "all"
backupmysql
movebackup
deleteoldbackups
printf "*** rclone lsf for %s:/%s.\n" "${DestDrive}" "${DestDir}"
rclone tree "${DestDrive}:/${DestDir}" --exclude "/Save*/" --human-readable
printf "\n*** About/Size of %s:%s/%s:\n" "${DestDrive}" "${DestDir}" "${FileDateDir}"
rclone about "${DestDrive}:"
# Show time to do the transfer
printf "\nDB Backups completed with ** %d ** total errors.\n\n" "${terrors}"
STOPTIME=$(( $(date +%s) - STARTTIME ))
show_time ${STOPTIME} "**** mariadb ${DestDrive}:/${DestDir}/${FileDateDir} completed in"
echo