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