| 1 |
#!/bin/bash |
|---|
| 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 |
set -e |
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 |
. /etc/alternc/local.sh |
|---|
| 41 |
. /etc/alternc/sqlbackup.conf |
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 |
DATE=`date +"%Y%m%d"` |
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 54 |
|
|---|
| 55 |
|
|---|
| 56 |
|
|---|
| 57 |
|
|---|
| 58 |
|
|---|
| 59 |
print() { |
|---|
| 60 |
|
|---|
| 61 |
|
|---|
| 62 |
|
|---|
| 63 |
log_level="" |
|---|
| 64 |
if [ "$1" == "error" ] || [ "$1" == "info" ] || [ "$1" == "debug" ]; |
|---|
| 65 |
then |
|---|
| 66 |
|
|---|
| 67 |
log_level="$1" |
|---|
| 68 |
shift |
|---|
| 69 |
fi |
|---|
| 70 |
|
|---|
| 71 |
|
|---|
| 72 |
|
|---|
| 73 |
|
|---|
| 74 |
|
|---|
| 75 |
|
|---|
| 76 |
|
|---|
| 77 |
|
|---|
| 78 |
|
|---|
| 79 |
if [ -z "$log_level" ] || |
|---|
| 80 |
[ "$log_level" == "error" ] || |
|---|
| 81 |
[ "$DEBUG" == "ON" -a "$log_level" == "debug" ] || |
|---|
| 82 |
[ "$log_level" == "info" -a "$VERBOSE" == "ON" ] ; |
|---|
| 83 |
then |
|---|
| 84 |
if [ "$DAEMON" == "ON" ] ; then |
|---|
| 85 |
|
|---|
| 86 |
|
|---|
| 87 |
if [ -z "$log_level" ] || [ "$log_level" == "error" ]; |
|---|
| 88 |
then |
|---|
| 89 |
echo "$EXEC_CMD $log_level: $*" |
|---|
| 90 |
fi |
|---|
| 91 |
echo "`date +"%b %d %T"` `hostname` $EXEC_CMD $log_level: $*" >> $F_LOG |
|---|
| 92 |
else |
|---|
| 93 |
if [ -z "$log_level" ]; |
|---|
| 94 |
then |
|---|
| 95 |
echo "$*" |
|---|
| 96 |
else |
|---|
| 97 |
echo "$log_level: $*" |
|---|
| 98 |
fi |
|---|
| 99 |
fi |
|---|
| 100 |
fi |
|---|
| 101 |
|
|---|
| 102 |
} |
|---|
| 103 |
|
|---|
| 104 |
error() { |
|---|
| 105 |
print "error" $* |
|---|
| 106 |
} |
|---|
| 107 |
|
|---|
| 108 |
info() { |
|---|
| 109 |
print "info" $* |
|---|
| 110 |
} |
|---|
| 111 |
debug() { |
|---|
| 112 |
print "debug" $* |
|---|
| 113 |
} |
|---|
| 114 |
|
|---|
| 115 |
function dobck() { |
|---|
| 116 |
local ext |
|---|
| 117 |
local i |
|---|
| 118 |
local old_ifs |
|---|
| 119 |
|
|---|
| 120 |
|
|---|
| 121 |
|
|---|
| 122 |
old_ifs="$IFS" |
|---|
| 123 |
IFS=" " |
|---|
| 124 |
|
|---|
| 125 |
while read login pass db count compressed target_dir; do |
|---|
| 126 |
|
|---|
| 127 |
debug "read $login \$pass $db $count $compressed $target_dir" |
|---|
| 128 |
|
|---|
| 129 |
IFS="$old_ifs" |
|---|
| 130 |
|
|---|
| 131 |
|
|---|
| 132 |
DO_BACKUP="YES" |
|---|
| 133 |
|
|---|
| 134 |
if [ "$compressed" -eq 1 ]; then |
|---|
| 135 |
ext=".gz" |
|---|
| 136 |
else |
|---|
| 137 |
ext="" |
|---|
| 138 |
fi |
|---|
| 139 |
|
|---|
| 140 |
|
|---|
| 141 |
|
|---|
| 142 |
|
|---|
| 143 |
|
|---|
| 144 |
|
|---|
| 145 |
|
|---|
| 146 |
|
|---|
| 147 |
|
|---|
| 148 |
|
|---|
| 149 |
|
|---|
| 150 |
|
|---|
| 151 |
if [ $TYPE_NAME_BACKUP == "rotate" ]; then |
|---|
| 152 |
|
|---|
| 153 |
i="$count" |
|---|
| 154 |
|
|---|
| 155 |
|
|---|
| 156 |
while [ $i -gt 1 ] ; do |
|---|
| 157 |
|
|---|
| 158 |
next_i=$(($i - 1)) |
|---|
| 159 |
|
|---|
| 160 |
if [ -e "${target_dir}/${db}.sql.${next_i}${ext}" ]; then |
|---|
| 161 |
mv -f "${target_dir}/${db}.sql.${next_i}${ext}" \ |
|---|
| 162 |
"${target_dir}/${db}.sql.${i}${ext}" 2>/dev/null |
|---|
| 163 |
fi |
|---|
| 164 |
i=$next_i |
|---|
| 165 |
done |
|---|
| 166 |
|
|---|
| 167 |
|
|---|
| 168 |
if [ -e "${target_dir}/${db}.sql${ext}" ]; then |
|---|
| 169 |
mv -f "${target_dir}/${db}.sql${ext}" \ |
|---|
| 170 |
"${target_dir}/${db}.sql.${i}${ext}" 2>/dev/null |
|---|
| 171 |
fi |
|---|
| 172 |
|
|---|
| 173 |
name_backup_file="${db}" |
|---|
| 174 |
else |
|---|
| 175 |
|
|---|
| 176 |
|
|---|
| 177 |
|
|---|
| 178 |
|
|---|
| 179 |
|
|---|
| 180 |
|
|---|
| 181 |
|
|---|
| 182 |
|
|---|
| 183 |
|
|---|
| 184 |
last2del=$(( ( $count + 1 ) * $coef )) |
|---|
| 185 |
|
|---|
| 186 |
|
|---|
| 187 |
|
|---|
| 188 |
|
|---|
| 189 |
|
|---|
| 190 |
|
|---|
| 191 |
|
|---|
| 192 |
|
|---|
| 193 |
|
|---|
| 194 |
|
|---|
| 195 |
debug "find ${target_dir} -name \"${db}.*sql${ext}\" -maxdepth 1 -mtime +$last2del -exec rm -f {} \; -ls" |
|---|
| 196 |
find ${target_dir} -name "${db}.*sql${ext}" -maxdepth 1 -mtime +${last2del} -exec rm -f {} \; -ls |
|---|
| 197 |
|
|---|
| 198 |
|
|---|
| 199 |
name_backup_file="${db}.${DATE}" |
|---|
| 200 |
|
|---|
| 201 |
fi |
|---|
| 202 |
|
|---|
| 203 |
|
|---|
| 204 |
if [ -f "${target_dir}/${name_backup_file}.sql${ext}" ] && [ "$ALLOW_OVERWRITE_BACKUP" == "no" ] ; then |
|---|
| 205 |
|
|---|
| 206 |
info "sqlbackup.sh: ${target_dir}/${name_backup_file}.sql${ext}: already exist" |
|---|
| 207 |
info " => no backup done as specify in allow-overwrite = $ALLOW_OVERWRITE_BACKUP" |
|---|
| 208 |
DO_BACKUP="NO" |
|---|
| 209 |
|
|---|
| 210 |
|
|---|
| 211 |
elif [ -f "${target_dir}/${name_backup_file}.sql${ext}" ] && [ "$ALLOW_OVERWRITE_BACKUP" == "rename" ] ; then |
|---|
| 212 |
|
|---|
| 213 |
info "sqlbackup.sh: ${target_dir}/${name_backup_file}.sql${ext}: already exist" |
|---|
| 214 |
info " => renaming the new file as specify in allow-overwrite = $ALLOW_OVERWRITE_BACKUP" |
|---|
| 215 |
hours=`date +"%H%M"` |
|---|
| 216 |
name_backup_file="${name_backup_file}.${hours}" |
|---|
| 217 |
|
|---|
| 218 |
|
|---|
| 219 |
elif [ -f "${target_dir}/${name_backup_file}.sql${ext}" ] && [ "$ALLOW_OVERWRITE_BACKUP" == "overwrite" ] ; then |
|---|
| 220 |
|
|---|
| 221 |
info "sqlbackup.sh: ${target_dir}/${name_backup_file}.sql${ext}: already exist" |
|---|
| 222 |
info " => overwrite file as specify in allow-overwrite = $ALLOW_OVERWRITE_BACKUP" |
|---|
| 223 |
|
|---|
| 224 |
fi |
|---|
| 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 |
if [ "$compressed" -eq 1 ] && [ "$DO_BACKUP" == "YES" ]; then |
|---|
| 250 |
debug "msqldump -h\"$MYSQL_HOST\" -u\"$login\" -p\"XXXX\" \"$db\" --add-drop-table --allow-keywords -Q -f -q -a -e \ " |
|---|
| 251 |
debug " | gzip -c > \"${target_dir}/${name_backup_file}.sql${ext}\"" |
|---|
| 252 |
|
|---|
| 253 |
mysqldump -h"$MYSQL_HOST" -u"$login" -p"$pass" "$db" \ |
|---|
| 254 |
--add-drop-table \ |
|---|
| 255 |
--allow-keywords \ |
|---|
| 256 |
--quote-names \ |
|---|
| 257 |
--force \ |
|---|
| 258 |
--quick \ |
|---|
| 259 |
--all \ |
|---|
| 260 |
--extended-insert \ |
|---|
| 261 |
| gzip -c > "${target_dir}/${name_backup_file}.sql${ext}" |
|---|
| 262 |
|
|---|
| 263 |
elif [ "$DO_BACKUP" == "YES" ] ; then |
|---|
| 264 |
debug "mysqldump -h\"$MYSQL_HOST\" -u\"$login\" -p\"XXXX\" \"$db\" --add-drop-table --allow-keywords -Q -f -q -a -e \ " |
|---|
| 265 |
debug " > \"${target_dir}/${name_backup_file}.sql\"" |
|---|
| 266 |
|
|---|
| 267 |
mysqldump -h"$MYSQL_HOST" -u"$login" -p"$pass" "$db" \ |
|---|
| 268 |
--add-drop-table \ |
|---|
| 269 |
--allow-keywords \ |
|---|
| 270 |
--quote-names \ |
|---|
| 271 |
--force \ |
|---|
| 272 |
--quick \ |
|---|
| 273 |
--all \ |
|---|
| 274 |
--extended-insert \ |
|---|
| 275 |
> "${target_dir}/${name_backup_file}.sql" |
|---|
| 276 |
fi |
|---|
| 277 |
|
|---|
| 278 |
IFS=" " |
|---|
| 279 |
done |
|---|
| 280 |
IFS="$old_ifs" |
|---|
| 281 |
} |
|---|
| 282 |
|
|---|
| 283 |
|
|---|
| 284 |
|
|---|
| 285 |
|
|---|
| 286 |
read_parameters() { |
|---|
| 287 |
|
|---|
| 288 |
|
|---|
| 289 |
while [ "$1" != "" ] ; do |
|---|
| 290 |
case "$1" in |
|---|
| 291 |
-h|--help) usage; exit ;; |
|---|
| 292 |
-v|--verbose) VERBOSE="ON" ;; |
|---|
| 293 |
-d|--debug) DEBUG="ON" ;; |
|---|
| 294 |
-t|--type) shift; TYPE="$1";; |
|---|
| 295 |
-n|--name-methode) shift; TYPE_NAME_BACKUP="$1";; |
|---|
| 296 |
-a|--allow-ovewrite) shift; ALLOW_OVERWRITE_BACKUP="$1" ;; |
|---|
| 297 |
*) |
|---|
| 298 |
error "invalide option -- $1" |
|---|
| 299 |
error "Try \`sqlbackup.sh --help' for more information." |
|---|
| 300 |
exit ;; |
|---|
| 301 |
esac |
|---|
| 302 |
|
|---|
| 303 |
|
|---|
| 304 |
|
|---|
| 305 |
[ "$1" != "" ] && shift |
|---|
| 306 |
done |
|---|
| 307 |
|
|---|
| 308 |
debug "TYPE = $TYPE" |
|---|
| 309 |
debug "TYPE_NAME_BACKUP = $TYPE_NAME_BACKUP" |
|---|
| 310 |
debug "ALLOW_OVERWRITE_BACKUP = $ALLOW_OVERWRITE_BACKUP" |
|---|
| 311 |
|
|---|
| 312 |
|
|---|
| 313 |
|
|---|
| 314 |
if [ "$TYPE" == "daily" ]; then |
|---|
| 315 |
|
|---|
| 316 |
mode=2 |
|---|
| 317 |
coef=1 |
|---|
| 318 |
elif [ "$TYPE" == "weekly" ] ; then |
|---|
| 319 |
|
|---|
| 320 |
mode=1 |
|---|
| 321 |
coef=7 |
|---|
| 322 |
elif [ -n "$TYPE" ] ; then |
|---|
| 323 |
error "missing argument: type" |
|---|
| 324 |
error "Try \`sqlbackup.sh --help' for more information." |
|---|
| 325 |
exit |
|---|
| 326 |
else |
|---|
| 327 |
error "invalide argument: type -- $TYPE" |
|---|
| 328 |
error "Try \`sqlbackup.sh --help' for more information." |
|---|
| 329 |
exit |
|---|
| 330 |
fi |
|---|
| 331 |
|
|---|
| 332 |
if ! ( [ -z "$TYPE_NAME_BACKUP" ] || |
|---|
| 333 |
[ "$TYPE_NAME_BACKUP" == "date" ] || |
|---|
| 334 |
[ "$TYPE_NAME_BACKUP" == "rotate" ] ) ; then |
|---|
| 335 |
error "invalide argument: name-methode -- $TYPE_NAME_BACKUP" |
|---|
| 336 |
error "Try \`sqlbackup.sh --help' for more information." |
|---|
| 337 |
exit |
|---|
| 338 |
fi |
|---|
| 339 |
|
|---|
| 340 |
if ! ( [ -z "$ALLOW_OVERWRITE_BACKUP" ] || |
|---|
| 341 |
[ "$ALLOW_OVERWRITE_BACKUP" == "no" ] || |
|---|
| 342 |
[ "$ALLOW_OVERWRITE_BACKUP" == "rename" ] || |
|---|
| 343 |
[ "$ALLOW_OVERWRITE_BACKUP" == "overwrite" ] ); then |
|---|
| 344 |
error "invalide argument: allow-ovewrite -- $ALLOW_OVERWRITE_BACKUP" |
|---|
| 345 |
error "Try \`sqlbackup.sh --help' for more information." |
|---|
| 346 |
exit |
|---|
| 347 |
fi |
|---|
| 348 |
|
|---|
| 349 |
} |
|---|
| 350 |
|
|---|
| 351 |
|
|---|
| 352 |
usage() { |
|---|
| 353 |
echo "Usage: sqlbackup.sh [OPTION] -t TYPE |
|---|
| 354 |
|
|---|
| 355 |
sqlbackup.sh is a script used by alternc for sql backup |
|---|
| 356 |
|
|---|
| 357 |
Mandatory arguments to long options are mandatory for short options too. |
|---|
| 358 |
-v, --verbose set verbose mode on |
|---|
| 359 |
-d, --debug set debug mode on |
|---|
| 360 |
-n, --name-method METHOD set the method type for files' name |
|---|
| 361 |
-a, --allow-override OVERRIDE specify the behaviour if backup files already exist |
|---|
| 362 |
-t, --type TYPE set backup type |
|---|
| 363 |
-h, --help display this help and exit |
|---|
| 364 |
|
|---|
| 365 |
the TYPE arguments specify type of backup. Here are the values: |
|---|
| 366 |
|
|---|
| 367 |
daily Execute a daily backup on all databases set to daily backup |
|---|
| 368 |
weekly Execute a daily backup on all databases set to weekly backup |
|---|
| 369 |
|
|---|
| 370 |
the METHOD argument the type for files' name. Here are the values: |
|---|
| 371 |
|
|---|
| 372 |
date insert in the backup file's name the date of the backup |
|---|
| 373 |
(default value) |
|---|
| 374 |
rotate rename file as file.<number><extension> where <number> |
|---|
| 375 |
is incremented |
|---|
| 376 |
|
|---|
| 377 |
the OVERRIDE argument the behaviour of the script if a backup file already exist. |
|---|
| 378 |
Here are the values: |
|---|
| 379 |
|
|---|
| 380 |
no if a backup file already exist, no backup done |
|---|
| 381 |
rename if a backup file already exist, add an extension to the new |
|---|
| 382 |
backup file |
|---|
| 383 |
|
|---|
| 384 |
overwrite if a backup file already exist, overwrite it with the new |
|---|
| 385 |
backup" |
|---|
| 386 |
|
|---|
| 387 |
} |
|---|
| 388 |
debug begin $@ |
|---|
| 389 |
|
|---|
| 390 |
read_parameters $@ |
|---|
| 391 |
debug end |
|---|
| 392 |
|
|---|
| 393 |
|
|---|
| 394 |
|
|---|
| 395 |
|
|---|
| 396 |
|
|---|
| 397 |
|
|---|
| 398 |
|
|---|
| 399 |
|
|---|
| 400 |
|
|---|
| 401 |
|
|---|
| 402 |
|
|---|
| 403 |
|
|---|
| 404 |
|
|---|
| 405 |
|
|---|
| 406 |
|
|---|
| 407 |
debug /usr/bin/mysql -h"$MYSQL_HOST" -u"$MYSQL_USER" -p"XXXX" "$MYSQL_DATABASE" -B |
|---|
| 408 |
/usr/bin/mysql -h"$MYSQL_HOST" -u"$MYSQL_USER" -p"$MYSQL_PASS" \ |
|---|
| 409 |
"$MYSQL_DATABASE" --batch << EOF | tail -n '+2' | dobck |
|---|
| 410 |
SELECT login, pass, db, bck_history, bck_gzip, bck_dir |
|---|
| 411 |
FROM db |
|---|
| 412 |
WHERE bck_mode=$mode; |
|---|
| 413 |
EOF |
|---|
| 414 |
|
|---|
| 415 |
|
|---|
| 416 |
|
|---|