| 1 | #! @BASH@ |
| 2 | ### |
| 3 | ### Backup script |
| 4 | ### |
| 5 | ### (c) 2012 Mark Wooding |
| 6 | ### |
| 7 | |
| 8 | ###----- Licensing notice --------------------------------------------------- |
| 9 | ### |
| 10 | ### This file is part of the `rsync-backup' program. |
| 11 | ### |
| 12 | ### rsync-backup is free software; you can redistribute it and/or modify |
| 13 | ### it under the terms of the GNU General Public License as published by |
| 14 | ### the Free Software Foundation; either version 2 of the License, or |
| 15 | ### (at your option) any later version. |
| 16 | ### |
| 17 | ### rsync-backup is distributed in the hope that it will be useful, |
| 18 | ### but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | ### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | ### GNU General Public License for more details. |
| 21 | ### |
| 22 | ### You should have received a copy of the GNU General Public License |
| 23 | ### along with rsync-backup; if not, write to the Free Software Foundation, |
| 24 | ### Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 25 | |
| 26 | set -e |
| 27 | |
| 28 | thishost=$(hostname -s) |
| 29 | quis=${0##*/} |
| 30 | . @pkgdatadir@/lib.sh |
| 31 | |
| 32 | verbose=: |
| 33 | dryrun=nil |
| 34 | |
| 35 | ###-------------------------------------------------------------------------- |
| 36 | ### Utility functions. |
| 37 | |
| 38 | RSYNCOPTS="--verbose" |
| 39 | |
| 40 | do_rsync () { |
| 41 | ## Run rsync(1) in an appropriate manner. Configuration should ovrride |
| 42 | ## this or set $RSYNCOPTS if it wants to do something weirder. Arguments |
| 43 | ## to this function are passed on to rsync. |
| 44 | |
| 45 | rsync \ |
| 46 | --archive --hard-links --numeric-ids --del \ |
| 47 | --sparse --compress \ |
| 48 | --one-file-system \ |
| 49 | --partial \ |
| 50 | $RSYNCOPTS \ |
| 51 | --filter="dir-merge .rsync-backup" \ |
| 52 | "$@" |
| 53 | } |
| 54 | |
| 55 | log () { |
| 56 | case $dryrun in |
| 57 | t) |
| 58 | echo >&2 " *** $*" |
| 59 | ;; |
| 60 | nil) |
| 61 | now=$(date +"%Y-%m-%d %H:%M:%S %z") |
| 62 | echo >&9 "$now $*" |
| 63 | ;; |
| 64 | esac |
| 65 | } |
| 66 | |
| 67 | maybe () { |
| 68 | ## Run CMD, if this isn't a dry run. |
| 69 | |
| 70 | case $dryrun in |
| 71 | t) echo >&2 " +++ $*" ;; |
| 72 | nil) "$@" ;; |
| 73 | esac |
| 74 | } |
| 75 | |
| 76 | copy () { |
| 77 | prefix=$1 |
| 78 | ## Copy lines from stdin to stdout, adding PREFIX. |
| 79 | |
| 80 | while IFS= read -r line; do |
| 81 | printf "%s %s\n" "$prefix" "$line" |
| 82 | done |
| 83 | } |
| 84 | |
| 85 | run () { |
| 86 | stdinp=nil |
| 87 | while :; do |
| 88 | case $1 in |
| 89 | -stdin) stdinp=t; shift ;; |
| 90 | --) shift; break ;; |
| 91 | *) break ;; |
| 92 | esac |
| 93 | done |
| 94 | tag=$1 cmd=$2; shift 2 |
| 95 | ## Run CMD, logging its output in a pleasing manner. |
| 96 | |
| 97 | case $dryrun in |
| 98 | t) |
| 99 | echo >&2 " *** RUN $tag" |
| 100 | echo >&2 " +++ $cmd $*" |
| 101 | rc=0 |
| 102 | ;; |
| 103 | nil) |
| 104 | log "BEGIN $tag" |
| 105 | rc=$( |
| 106 | case $stdinp in nil) exec </dev/null ;; esac |
| 107 | { { { ( set +e |
| 108 | "$cmd" "$@" 3>&- 4>&- 5>&- 9>&- |
| 109 | echo $? >&5; ) | |
| 110 | copy "|" >&4; } 2>&1 | |
| 111 | copy "*" >&4; } 4>&1 | |
| 112 | cat >&9; } 5>&1 |
| 113 | ) |
| 114 | case $rc in |
| 115 | 0) log "END $tag" ;; |
| 116 | *) log "FAIL $tag (rc = $rc)" ;; |
| 117 | esac |
| 118 | ;; |
| 119 | esac |
| 120 | return $rc |
| 121 | } |
| 122 | |
| 123 | run_diff () { |
| 124 | out=$1 old=$2 new=$3 |
| 125 | ## Write a unified diff from OLD to NEW, to OUT. |
| 126 | |
| 127 | set +e; diff -u "$old" "$new" >"$out"; rc=$?; set -e |
| 128 | case $rc in 1) cat "$out" ;; esac |
| 129 | return $rc |
| 130 | } |
| 131 | |
| 132 | localp () { |
| 133 | h=$1 |
| 134 | ## Answer whether H is a local host. |
| 135 | |
| 136 | case $h in |
| 137 | "$thishost") return 0 ;; |
| 138 | *) return 1 ;; |
| 139 | esac |
| 140 | } |
| 141 | |
| 142 | hostrun () { |
| 143 | tag=$1 cmd=$2 |
| 144 | ## Run CMD on the current host. If the host seems local then run the |
| 145 | ## command through a local shell; otherwise run it through ssh(1). Either |
| 146 | ## way it will be processed by a shell. |
| 147 | |
| 148 | if localp $host; then run "@$host: $tag" sh -c "$cmd" |
| 149 | else run "@$host: $tag" ssh $userat$host "$cmd" |
| 150 | fi |
| 151 | } |
| 152 | |
| 153 | _hostrun () { |
| 154 | h=$1 cmd=$2 |
| 155 | ## Like hostrun, but without the complicated logging, and targetted at a |
| 156 | ## specific host. |
| 157 | |
| 158 | if localp $h; then sh -c "$cmd" |
| 159 | else ssh $h "$cmd" |
| 160 | fi |
| 161 | } |
| 162 | |
| 163 | hostpath () { |
| 164 | path=$1 |
| 165 | ## Output (to stdout) either PATH or HOST:PATH, choosing the former if the |
| 166 | ## current host is local. |
| 167 | |
| 168 | if localp $host; then echo $path |
| 169 | else echo $userat$host:$path |
| 170 | fi |
| 171 | } |
| 172 | |
| 173 | defhook () { |
| 174 | hook=$1 |
| 175 | ## Define a hook called HOOK. |
| 176 | |
| 177 | eval hk_$hook= |
| 178 | } |
| 179 | |
| 180 | addhook () { |
| 181 | hook=$1 cmd=$2 |
| 182 | ## Add command CMD to the hook HOOK. |
| 183 | |
| 184 | eval old=\$hk_$hook; new="$old $cmd" |
| 185 | eval hk_$hook=\$new |
| 186 | } |
| 187 | |
| 188 | runhook () { |
| 189 | hook=$1; shift 1 |
| 190 | ## Invoke HOOK, passing it the remaining arguments. |
| 191 | |
| 192 | eval cmds=\$hk_$hook |
| 193 | for cmd in $cmds; do |
| 194 | if ! $cmd "$@"; then return $?; fi |
| 195 | done |
| 196 | } |
| 197 | |
| 198 | remove_old_logfiles () { |
| 199 | base=$1 |
| 200 | ## Remove old logfiles with names of the form BASE.DATE#N, so that there |
| 201 | ## are at most $MAXLOG of them. |
| 202 | |
| 203 | ## Count up the logfiles. |
| 204 | nlog=0 |
| 205 | for i in "$base".*; do |
| 206 | if [ ! -f "$i" ]; then continue; fi |
| 207 | nlog=$(( nlog + 1 )) |
| 208 | done |
| 209 | |
| 210 | ## If there are too many, go through and delete some early ones. |
| 211 | if [ $dryrun = nil ] && [ $nlog -gt $MAXLOG ]; then |
| 212 | n=$(( nlog - MAXLOG )) |
| 213 | for i in "$base".*; do |
| 214 | if [ ! -f "$i" ]; then continue; fi |
| 215 | rm -f "$i" |
| 216 | n=$(( n - 1 )) |
| 217 | if [ $n -eq 0 ]; then break; fi |
| 218 | done |
| 219 | fi |
| 220 | } |
| 221 | |
| 222 | ###-------------------------------------------------------------------------- |
| 223 | ### Database operations. |
| 224 | |
| 225 | insert_index () { |
| 226 | host=$1 fs=$2 date=$3 vol=$4 |
| 227 | |
| 228 | if [ -f "$INDEXDB" ]; then |
| 229 | sqlite3 "$INDEXDB" <<EOF |
| 230 | INSERT INTO idx (host, fs, date, vol) |
| 231 | VALUES ('$host', '$fs', '$date', '$vol'); |
| 232 | EOF |
| 233 | fi |
| 234 | } |
| 235 | |
| 236 | delete_index () { |
| 237 | host=$1 fs=$2 date=$3 |
| 238 | |
| 239 | if [ -f "$INDEXDB" ]; then |
| 240 | sqlite3 "$INDEXDB" <<EOF |
| 241 | DELETE FROM idx WHERE |
| 242 | host = '$host' AND fs = '$fs' AND date = '$date'; |
| 243 | EOF |
| 244 | fi |
| 245 | } |
| 246 | |
| 247 | ###-------------------------------------------------------------------------- |
| 248 | ### Snapshot handling. |
| 249 | |
| 250 | ## Snapshot protocol. Each snapshot type has a pair of functions snap_TYPE |
| 251 | ## and unsnap_TYPE. Each is given the current snapshot arguments and the |
| 252 | ## filesystem name to back up. The snap_TYPE function should create and |
| 253 | ## mount the snapshot and output an rsync(1) path to where the filesystem can |
| 254 | ## be copied; the unsnap_TYPE function should unmount and tear down the |
| 255 | ## snapshot. |
| 256 | |
| 257 | ## Fake snapshot by not doing anything. Use only if you have no choice. |
| 258 | snap_live () { hostpath "$2"; } |
| 259 | unsnap_live () { :; } |
| 260 | |
| 261 | ## Fake snapshot by remounting a live filesystem read-only. Useful if the |
| 262 | ## underlying storage isn't in LVM. |
| 263 | |
| 264 | snap_ro () { |
| 265 | fs=$1 mnt=$2 |
| 266 | |
| 267 | ## Place a marker in the filesystem so we know why it was made readonly. |
| 268 | ## (Also this serves to ensure that the filesystem was writable before.) |
| 269 | hostrun "snap-ro $mnt" " |
| 270 | echo rsync-backup >$mnt/.lock |
| 271 | mount -oremount,ro $mnt" || return $? |
| 272 | |
| 273 | ## Done. |
| 274 | hostpath $mnt |
| 275 | } |
| 276 | |
| 277 | unsnap_ro () { |
| 278 | fs=$1 mnt=$2 |
| 279 | |
| 280 | ## Check that the filesystem still has our lock marker. |
| 281 | hostrun "unsnap-ro $mnt" " |
| 282 | case \$(cat $mnt/.lock) in |
| 283 | rsync-backup) ;; |
| 284 | *) echo unlocked by someone else; exit 31 ;; |
| 285 | esac |
| 286 | mount -oremount,rw $mnt |
| 287 | rm $mnt/.lock" || return $? |
| 288 | } |
| 289 | |
| 290 | ## Snapshot using LVM. |
| 291 | |
| 292 | SNAPSIZE="-l10%ORIGIN" |
| 293 | |
| 294 | snap_lvm () { |
| 295 | vg=$1 lv=$2 |
| 296 | |
| 297 | ## Make the snapshot. |
| 298 | hostrun "snap-lvm $vg/$lv" " |
| 299 | lvcreate --snapshot -n$lv.bkp $SNAPSIZE $vg/$lv |
| 300 | mkdir -p $SNAPDIR/$lv |
| 301 | mount -oro /dev/$vg/$lv.bkp $SNAPDIR/$lv" || return $? |
| 302 | |
| 303 | ## Done. |
| 304 | hostpath $SNAPDIR/$lv |
| 305 | } |
| 306 | |
| 307 | unsnap_lvm () { |
| 308 | vg=$1 lv=$2 |
| 309 | |
| 310 | ## Remove the snapshot. Sometimes LVM doesn't notice that the snapshot is |
| 311 | ## no longer in open immdiately, so try several times. |
| 312 | hostrun "unsnap-lvm $vg/$lv" " |
| 313 | umount $SNAPDIR/$lv |
| 314 | rc=1 |
| 315 | for i in 1 2 3 4; do |
| 316 | if lvremove -f $vg/$lv.bkp; then rc=0; break; fi |
| 317 | sleep 2 |
| 318 | done |
| 319 | exit $rc" || return $? |
| 320 | } |
| 321 | |
| 322 | ## Complicated snapshot using LVM, where the volume group and filesystem are |
| 323 | ## owned by different machines, so they need to be synchronized during the |
| 324 | ## snapshot. |
| 325 | |
| 326 | do_rfreezefs () { |
| 327 | lvhost=$1 vg=$2 lv=$3 fshost=$4 fsdir=$5 |
| 328 | |
| 329 | ## Engage in the rfreezefs protocol with the filesystem host. This |
| 330 | ## involves some hairy plumbing. We want to get exit statuses out of both |
| 331 | ## halves. |
| 332 | set +e |
| 333 | ssh $fshost rfreezefs $fsdir | { |
| 334 | set -e |
| 335 | |
| 336 | ## Read the codebook from the remote end. |
| 337 | ready=nil |
| 338 | while read line; do |
| 339 | set -- $line |
| 340 | case "$1" in |
| 341 | PORT) port=$2 ;; |
| 342 | TOKEN) eval tok_$2=$3 ;; |
| 343 | READY) ready=t; break ;; |
| 344 | *) |
| 345 | echo >&2 "$quis: unexpected keyword $1 (rfreezefs to $rhost)" |
| 346 | exit 1 |
| 347 | ;; |
| 348 | esac |
| 349 | done |
| 350 | case $ready in |
| 351 | nil) |
| 352 | echo >&2 "$quis: unexpected eof (rfreezefs to $rhost)" |
| 353 | exit 1 |
| 354 | ;; |
| 355 | esac |
| 356 | |
| 357 | ## Connect to the filesystem host's TCP port and get it to freeze its |
| 358 | ## filesystem. |
| 359 | exec 3<>/dev/tcp/$fshost/$port |
| 360 | echo $tok_FREEZE >&3 |
| 361 | read tok <&3 |
| 362 | case $tok in |
| 363 | "$tok_FROZEN") ;; |
| 364 | *) |
| 365 | echo >&2 "$quis: unexpected token $tok (rfreezefs $fsdir on $fshost)" |
| 366 | exit 1 |
| 367 | ;; |
| 368 | esac |
| 369 | |
| 370 | ## Get the volume host to create the snapshot. |
| 371 | set +e |
| 372 | _hostrun >&2 3>&- $userat$lvhost \ |
| 373 | "lvcreate --snapshot -n$lv.bkp $SNAPSIZE $vg/$lv" |
| 374 | snaprc=$? |
| 375 | set -e |
| 376 | |
| 377 | ## The filesystem can thaw now. |
| 378 | echo $tok_THAW >&3 |
| 379 | read tok <&3 |
| 380 | case $tok in |
| 381 | "$tok_THAWED") ;; |
| 382 | *) |
| 383 | _hostrun >&2 3>&- $userat$lvhost "lvremove -f $vg/$lv.bkp" || : |
| 384 | echo >&2 "$quis: unexpected token $tok (rfreezefs $fsdir on $fshost)" |
| 385 | exit 1 |
| 386 | ;; |
| 387 | esac |
| 388 | |
| 389 | ## Done. |
| 390 | exit $snaprc |
| 391 | } |
| 392 | |
| 393 | ## Sift through the wreckage to find out what happened. |
| 394 | rc_rfreezefs=${PIPESTATUS[0]} rc_snapshot=${PIPESTATUS[1]} |
| 395 | set -e |
| 396 | case $rc_rfreezefs:$rc_snapshot in |
| 397 | 0:0) |
| 398 | ;; |
| 399 | 112:*) |
| 400 | echo >&2 "$quis: EMERGENCY failed to thaw $fsdir on $fshost!" |
| 401 | exit 112 |
| 402 | ;; |
| 403 | *) |
| 404 | echo >&2 "$quis: failed to snapshot $vg/$lv ($fsdir on $fshost)" |
| 405 | exit 1 |
| 406 | ;; |
| 407 | esac |
| 408 | |
| 409 | ## Mount the snapshot on the volume host. |
| 410 | _hostrun >&2 $userat$lvhost " |
| 411 | mkdir -p $SNAPDIR/$lv |
| 412 | mount -oro /dev/$vg/$lv.bkp $SNAPDIR/$lv" |
| 413 | } |
| 414 | |
| 415 | snap_rfreezefs () { |
| 416 | rhost=$1 vg=$2 lv=$3 rfs=$4 |
| 417 | |
| 418 | set -e |
| 419 | run "snap-rfreezefs $host:$vg/$lv $rhost:$rfs" \ |
| 420 | do_rfreezefs $host $vg $lv $rhost $rfs || return $? |
| 421 | hostpath $SNAPDIR/$lv |
| 422 | } |
| 423 | |
| 424 | unsnap_rfreezefs () { |
| 425 | |
| 426 | ## Unshapping is the same as for plain LVM. |
| 427 | rhost=$1 vg=$2 lv=$3 rfs=$4 |
| 428 | unsnap_lvm $vg $lv |
| 429 | } |
| 430 | |
| 431 | ###-------------------------------------------------------------------------- |
| 432 | ### Expiry computations. |
| 433 | |
| 434 | expire () { |
| 435 | ## Read dates on stdin; write to stdout `EXPIRE date' for dates which |
| 436 | ## should be expired and `RETAIN date' for dates which should be retained. |
| 437 | |
| 438 | ## Get the current date and convert it into useful forms. |
| 439 | now=$(date +%Y-%m-%d) |
| 440 | parsedate $now |
| 441 | now_jdn=$(julian $now) now_year=$year now_month=$month now_day=$day |
| 442 | kept=: |
| 443 | |
| 444 | ## Work through each date in the input. |
| 445 | while read date; do |
| 446 | keep=nil |
| 447 | |
| 448 | ## Convert the date into a useful form. |
| 449 | jdn=$(julian $date) |
| 450 | parsedate $date |
| 451 | |
| 452 | ## Work through the policy list. |
| 453 | if [ $jdn -le $now_jdn ]; then |
| 454 | while read ival age; do |
| 455 | |
| 456 | ## Decide whether the policy entry applies to this date. |
| 457 | apply=nil |
| 458 | case $age in |
| 459 | forever) |
| 460 | apply=t |
| 461 | ;; |
| 462 | year) |
| 463 | if [ $year -eq $now_year ] || |
| 464 | ([ $year -eq $(( $now_year - 1 )) ] && |
| 465 | [ $month -ge $now_month ]) |
| 466 | then apply=t; fi |
| 467 | ;; |
| 468 | month) |
| 469 | if ([ $month -eq $now_month ] && [ $year -eq $now_year ]) || |
| 470 | ((([ $month -eq $(( $now_month - 1 )) ] && |
| 471 | [ $year -eq $now_year ]) || |
| 472 | ([ $month -eq 12 ] && [ $now_month -eq 1 ] && |
| 473 | [ $year -eq $(( $now_year - 1 )) ])) && |
| 474 | [ $day -ge $now_day ]) |
| 475 | then apply=t; fi |
| 476 | ;; |
| 477 | week) |
| 478 | if [ $jdn -ge $(( $now_jdn - 7 )) ]; then apply=t; fi |
| 479 | ;; |
| 480 | *) |
| 481 | echo >&2 "$quis: unknown age symbol \`$age'" |
| 482 | exit 1 |
| 483 | ;; |
| 484 | esac |
| 485 | case $apply in nil) continue ;; esac |
| 486 | |
| 487 | ## Find the interval marker for this date. |
| 488 | case $ival in |
| 489 | daily) |
| 490 | marker=$date |
| 491 | ;; |
| 492 | weekly) |
| 493 | ydn=$(julian $year-01-01) |
| 494 | wk=$(( ($jdn - $ydn)/7 + 1 )) |
| 495 | marker=$year-w$wk |
| 496 | ;; |
| 497 | monthly) |
| 498 | marker=$year-$month |
| 499 | ;; |
| 500 | annually | yearly) |
| 501 | marker=$year |
| 502 | ;; |
| 503 | *) |
| 504 | echo >&2 "$quis: unknown interval symbol \`$ival'" |
| 505 | exit 1 |
| 506 | ;; |
| 507 | esac |
| 508 | |
| 509 | ## See if we've alredy retained something in this interval. |
| 510 | case $kept in |
| 511 | *:"$marker":*) ;; |
| 512 | *) keep=t kept=$kept$marker: ;; |
| 513 | esac |
| 514 | |
| 515 | done <<EOF |
| 516 | $expire_policy |
| 517 | EOF |
| 518 | fi |
| 519 | |
| 520 | case $keep in |
| 521 | t) echo RETAIN $date ;; |
| 522 | *) echo EXPIRE $date ;; |
| 523 | esac |
| 524 | |
| 525 | done |
| 526 | } |
| 527 | |
| 528 | ###-------------------------------------------------------------------------- |
| 529 | ### Actually taking backups of filesystems. |
| 530 | |
| 531 | MAXLOG=14 |
| 532 | HASH=sha256 |
| 533 | unset VOLUME |
| 534 | |
| 535 | bkprc=0 |
| 536 | |
| 537 | hash_file () { |
| 538 | file=$1 |
| 539 | |
| 540 | case $HASH in |
| 541 | md5 | sha1 | sha224 | sha256 | sha384 | sha512) |
| 542 | set -- $(${HASH}sum <"$file") |
| 543 | echo "$1" |
| 544 | ;; |
| 545 | *) |
| 546 | set -- $(openssl dgst -$HASH <"$file") |
| 547 | echo "$2" |
| 548 | ;; |
| 549 | esac |
| 550 | } |
| 551 | |
| 552 | remote_fshash () { |
| 553 | _hostrun $userat$host " |
| 554 | umask 077 |
| 555 | mkdir -p $fshashdir |
| 556 | cd ${snapmnt#*:} |
| 557 | echo \"*** $host $fs $date\"; echo |
| 558 | rsync -rx --filter='dir-merge .rsync-backup' ./ | |
| 559 | fshash -c$fshashdir/$fs.bkp -a -H$HASH -frsync |
| 560 | " >new.fshash |
| 561 | } |
| 562 | |
| 563 | local_fshash () { |
| 564 | { echo "*** $host $fs $date"; echo |
| 565 | fshash -c$STOREDIR/fshash.cache -H$HASH new/ |
| 566 | } >$localmap |
| 567 | } |
| 568 | |
| 569 | expire_backups () { |
| 570 | { seen=: |
| 571 | for i in *-*-*; do |
| 572 | i=${i%%.*} |
| 573 | case $i in *[!-0-9]*) continue ;; esac |
| 574 | case $seen in *:"$i":*) continue ;; esac |
| 575 | seen=$seen$i: |
| 576 | echo $i |
| 577 | done; } | |
| 578 | expire | |
| 579 | while read op date; do |
| 580 | case $op,$dryrun in |
| 581 | RETAIN,t) |
| 582 | echo >&2 " --- keep $date" |
| 583 | ;; |
| 584 | EXPIRE,t) |
| 585 | echo >&2 " --- delete $date" |
| 586 | ;; |
| 587 | RETAIN,nil) |
| 588 | echo "keep $date" |
| 589 | ;; |
| 590 | EXPIRE,nil) |
| 591 | echo "delete $date" |
| 592 | $verbose -n " expire $date..." |
| 593 | rm -rf $date $date.* |
| 594 | delete_index $host $fs $date |
| 595 | $verbose " done" |
| 596 | ;; |
| 597 | esac |
| 598 | done |
| 599 | } |
| 600 | |
| 601 | ## Backup hooks. |
| 602 | defhook setup |
| 603 | defhook precommit |
| 604 | defhook postcommit |
| 605 | |
| 606 | backup_precommit_hook () { |
| 607 | host=$1 fs=$2 date=$3 |
| 608 | ## Compatibility: You can override this hook in the configuration file for |
| 609 | ## special effects; but it's better to use `addhook precommit'. |
| 610 | |
| 611 | : |
| 612 | } |
| 613 | addhook precommit backup_precommit_hook |
| 614 | |
| 615 | backup_commit_hook () { |
| 616 | host=$1 fs=$2 date=$3 |
| 617 | ## Compatibility: You can override this hook in the configuration file for |
| 618 | ## special effects; but it's better to use `addhook commit'. |
| 619 | |
| 620 | : |
| 621 | } |
| 622 | addhook commit backup_commit_hook |
| 623 | |
| 624 | do_backup () { |
| 625 | date=$1 fs=$2 fsarg=$3 |
| 626 | ## Back up FS on the current host. |
| 627 | |
| 628 | set -e |
| 629 | attempt=0 |
| 630 | fshash_diff=nil |
| 631 | |
| 632 | ## Run a hook beforehand. |
| 633 | set +e; runhook setup $host $fs $date; rc=$?; set -e |
| 634 | case $? in |
| 635 | 0) ;; |
| 636 | 99) log "BACKUP of $host:$fs SKIPPED by hook"; return 0 ;; |
| 637 | *) log "BACKUP of $host:$fs FAILED (hook returns $?)"; return $? ;; |
| 638 | esac |
| 639 | |
| 640 | ## Report the start of this attempt. |
| 641 | log "START BACKUP of $host:$fs" |
| 642 | |
| 643 | ## Maybe we need to retry the backup. |
| 644 | while :; do |
| 645 | |
| 646 | ## Rig checksum variables to mismatch unless they're set later. |
| 647 | hrfs=REMOTE hlfs=LOCAL |
| 648 | |
| 649 | ## Create and mount the remote snapshot. |
| 650 | case $dryrun in |
| 651 | t) |
| 652 | maybe snap_$snap $fs $fsarg |
| 653 | snapmnt="<snapshot>" |
| 654 | ;; |
| 655 | nil) |
| 656 | snapmnt=$(snap_$snap $snapargs $fs $fsarg) || return $? |
| 657 | ;; |
| 658 | esac |
| 659 | $verbose " create snapshot" |
| 660 | |
| 661 | ## If we had a fshash-mismatch, then clear out the potentially stale |
| 662 | ## entries, both locally and remotely. |
| 663 | case $fshash_diff in |
| 664 | nil) ;; |
| 665 | *) |
| 666 | $verbose " prune cache" |
| 667 | run -stdin "local prune fshash" \ |
| 668 | fshash -u -c$STOREDIR/fshash.cache -H$HASH new/ <$fshash_diff |
| 669 | run -stdin "@$host: prune fshash" \ |
| 670 | _hostrun $userat$host <$fshash_diff \ |
| 671 | "fshash -u -c$fshashdir/$fs.bkp -H$HASH ${snapmnt#*:}" |
| 672 | ;; |
| 673 | esac |
| 674 | |
| 675 | ## Build the list of hardlink sources. |
| 676 | linkdests="" |
| 677 | for i in $host $like; do |
| 678 | d=$STOREDIR/$i/$fs/last/ |
| 679 | if [ -d $d ]; then linkdests="$linkdests --link-dest=$d"; fi |
| 680 | done |
| 681 | |
| 682 | ## Copy files from the remote snapshot. |
| 683 | maybe mkdir -p new/ |
| 684 | case $dryrun in |
| 685 | t) $verbose " running rsync" ;; |
| 686 | nil) $verbose -n " running rsync..." ;; |
| 687 | esac |
| 688 | set +e |
| 689 | run "RSYNC of $host:$fs (snapshot on $snapmnt)" do_rsync \ |
| 690 | $linkdests \ |
| 691 | $rsyncargs \ |
| 692 | $snapmnt/ new/ |
| 693 | rc_rsync=$? |
| 694 | set -e |
| 695 | case $dryrun in nil) $verbose " done" ;; esac |
| 696 | |
| 697 | ## Collect a map of the snapshot for verification purposes. |
| 698 | set +e |
| 699 | case $dryrun in |
| 700 | t) $verbose " remote fshash" ;; |
| 701 | nil) $verbose -n " remote fshash..." ;; |
| 702 | esac |
| 703 | run "@$host: fshash $fs" remote_fshash |
| 704 | rc_fshash=$? |
| 705 | set -e |
| 706 | case $dryrun in |
| 707 | nil) |
| 708 | hrfs=$(hash_file "$fshashdir/$fs.bkp") |
| 709 | $log "remote fshash $HASH checksum: $hlfs" |
| 710 | $verbose " done" |
| 711 | ;; |
| 712 | t) |
| 713 | hrfs=UNSET |
| 714 | ;; |
| 715 | esac |
| 716 | |
| 717 | ## Remove the snapshot. |
| 718 | maybe unsnap_$snap $snapargs $fs $fsarg |
| 719 | $verbose " remove snapshot" |
| 720 | |
| 721 | ## If we failed to copy, then give up. |
| 722 | case $rc_rsync:$rc_fshash in |
| 723 | 0:0) ;; |
| 724 | 0:*) return $rc_fshash ;; |
| 725 | *) return $rc_rsync ;; |
| 726 | esac |
| 727 | |
| 728 | ## Get a matching map of the files received. |
| 729 | maybe mkdir -m750 -p $STOREDIR/tmp/ |
| 730 | localmap=$STOREDIR/tmp/fshash.$host.$fs.$date |
| 731 | case $dryrun in |
| 732 | t) $verbose " local fshash" ;; |
| 733 | nil) $verbose -n " local fshash..." ;; |
| 734 | esac |
| 735 | run "local fshash $host:$fs" local_fshash || return $? |
| 736 | case $dryrun in |
| 737 | nil) |
| 738 | hlfs=$(hash_file "$localmap") |
| 739 | $log "local fshash $HASH checksum: $hlfs" |
| 740 | $verbose " done" |
| 741 | ;; |
| 742 | t) |
| 743 | hlfs=UNSET |
| 744 | ;; |
| 745 | esac |
| 746 | |
| 747 | ## Compare the two maps. |
| 748 | set +e |
| 749 | fshash_diff=$STOREDIR/tmp/fshash-diff.$host.$fs.$date |
| 750 | run "compare fshash maps for $host:$fs" \ |
| 751 | run_diff $fshash_diff new.fshash $localmap |
| 752 | rc_diff=$? |
| 753 | set -e |
| 754 | case $rc_diff in |
| 755 | 0) |
| 756 | break |
| 757 | ;; |
| 758 | 1) |
| 759 | if [ $attempt -ge $retry ]; then return $rc; fi |
| 760 | $verbose " fshash mismatch; retrying" |
| 761 | attempt=$(( $attempt + 1 )) |
| 762 | ;; |
| 763 | *) |
| 764 | return $rc_diff |
| 765 | ;; |
| 766 | esac |
| 767 | done |
| 768 | |
| 769 | ## Double-check the checksums. |
| 770 | if [ $hrfs != $hlfs ]; then |
| 771 | cat >&2 <<EOF |
| 772 | $0: INTERNAL ERROR: fshash $HASH checksum mismatch -- aborting |
| 773 | remote fshash checksum = $hrfs |
| 774 | local fshash checksum = $hlfs |
| 775 | EOF |
| 776 | exit 127 |
| 777 | fi |
| 778 | |
| 779 | ## Glorious success. |
| 780 | maybe rm -f $localmap |
| 781 | case $fshash_diff in nil) ;; *) maybe rm -f $fshash_diff ;; esac |
| 782 | $verbose " fshash match" |
| 783 | |
| 784 | ## Commit this backup. |
| 785 | case $dryrun in |
| 786 | nil) |
| 787 | runhook precommit $host $fs $date |
| 788 | mv new $date |
| 789 | mv new.fshash $date.fshash |
| 790 | insert_index $host $fs $date $VOLUME |
| 791 | runhook commit $host $fs $date |
| 792 | mkdir hack |
| 793 | ln -s $date hack/last |
| 794 | mv hack/last . |
| 795 | rmdir hack |
| 796 | ;; |
| 797 | esac |
| 798 | $verbose " commit" |
| 799 | |
| 800 | ## Expire old backups. |
| 801 | case "${expire_policy+t},${default_policy+t}" in |
| 802 | ,t) expire_policy=$default_policy ;; |
| 803 | esac |
| 804 | case "${expire_policy+t},$dryrun" in |
| 805 | t,nil) run "expiry for $host:$fs" expire_backups ;; |
| 806 | t,t) expire_backups ;; |
| 807 | esac |
| 808 | clear_policy=t |
| 809 | |
| 810 | ## Report success. |
| 811 | case $dryrun in |
| 812 | t) log "END BACKUP of $host:$fs" ;; |
| 813 | nil) log "SUCCESSFUL BACKUP of $host:$fs" ;; |
| 814 | esac |
| 815 | } |
| 816 | |
| 817 | run_backup_cmd () { |
| 818 | fs=$1 date=$2 cmd=$3; shift 3 |
| 819 | ## try_backup FS DATE COMMAND ARGS ... |
| 820 | ## |
| 821 | ## Run COMMAND ARGS to back up filesystem FS on the current host, |
| 822 | ## maintaining a log, and checking whether it worked. The caller has |
| 823 | ## usually worked out the DATE in order to set up the filesystem, and we |
| 824 | ## need it to name the log file properly. |
| 825 | |
| 826 | ## Find a name for the log file. In unusual circumstances, we may have |
| 827 | ## deleted old logs from today, so just checking for an unused sequence |
| 828 | ## number is insufficient. Instead, check all of the logfiles for today, |
| 829 | ## and use a sequence number that's larger than any of them. |
| 830 | case $dryrun in |
| 831 | t) |
| 832 | log=/dev/null |
| 833 | ;; |
| 834 | nil) |
| 835 | seq=1 |
| 836 | for i in "$logdir/$host/$fs.$date#"*; do |
| 837 | tail=${i##*#} |
| 838 | case "$tail" in [!1-9]* | *[!0-9]*) continue ;; esac |
| 839 | if [ -f "$i" -a $tail -ge $seq ]; then seq=$(( tail + 1 )); fi |
| 840 | done |
| 841 | log="$logdir/$host/$fs.$date#$seq" |
| 842 | ;; |
| 843 | esac |
| 844 | |
| 845 | ## Run the backup command. |
| 846 | case $dryrun in nil) mkdir -p $logdir/$host ;; esac |
| 847 | if ! "$cmd" "$@" 9>$log 1>&9; then |
| 848 | echo >&2 |
| 849 | echo >&2 "$quis: backup of $host:$fs FAILED!" |
| 850 | bkprc=1 |
| 851 | fi |
| 852 | |
| 853 | ## Clear away any old logfiles. |
| 854 | remove_old_logfiles "$logdir/$host/$fs" |
| 855 | } |
| 856 | |
| 857 | backup () { |
| 858 | ## backup FS[:ARG] ... |
| 859 | ## |
| 860 | ## Back up the filesystems on the currently selected host using the |
| 861 | ## currently selected snapshot type. |
| 862 | |
| 863 | ## Make sure that there's a store volume. We must do this here rather than |
| 864 | ## in the main body of the script, since the configuration file needs a |
| 865 | ## chance to override STOREDIR. |
| 866 | if ! [ -r $STOREDIR/.rsync-backup-store ]; then |
| 867 | echo >&2 "$quis: no backup volume mounted" |
| 868 | exit 15 |
| 869 | fi |
| 870 | |
| 871 | ## Read the volume name if we don't have one already. Again, this allows |
| 872 | ## the configuration file to provide a volume name. |
| 873 | case "${VOLUME+t}${VOLUME-nil}" in |
| 874 | nil) VOLUME=$(cat $METADIR/volume) ;; |
| 875 | esac |
| 876 | |
| 877 | ## Back up each requested file system in turn. |
| 878 | for fs in "$@"; do |
| 879 | |
| 880 | ## Parse the argument. |
| 881 | case $fs in |
| 882 | *:*) fsarg=${fs#*:} fs=${fs%%:*} ;; |
| 883 | *) fsarg="" ;; |
| 884 | esac |
| 885 | $verbose " filesystem $fs" |
| 886 | |
| 887 | ## Move to the store directory and set up somewhere to put this backup. |
| 888 | cd $STOREDIR |
| 889 | case $dryrun in |
| 890 | nil) |
| 891 | if [ ! -d $host ]; then |
| 892 | mkdir -m755 $host |
| 893 | chown root:root $host |
| 894 | fi |
| 895 | if [ ! -d $host/$fs ]; then |
| 896 | mkdir -m750 $host/$fs |
| 897 | chown root:backup $host/$fs |
| 898 | fi |
| 899 | ;; |
| 900 | esac |
| 901 | cd $host/$fs |
| 902 | |
| 903 | ## Find out if we've already copied this filesystem today. |
| 904 | date=$(date +%Y-%m-%d) |
| 905 | if [ $dryrun = nil ] && [ -d $date ]; then |
| 906 | $verbose " already dumped" |
| 907 | continue |
| 908 | fi |
| 909 | |
| 910 | ## Do the backup of this filesystem. |
| 911 | run_backup_cmd $fs $date do_backup $date $fs $fsarg |
| 912 | done |
| 913 | } |
| 914 | |
| 915 | ###-------------------------------------------------------------------------- |
| 916 | ### Configuration functions. |
| 917 | |
| 918 | defhook start |
| 919 | defhook end |
| 920 | |
| 921 | done_first_host_p=nil |
| 922 | |
| 923 | host () { |
| 924 | host=$1 |
| 925 | like= userat= |
| 926 | case $done_first_host_p in |
| 927 | nil) runhook start; done_first_host_p=t ;; |
| 928 | esac |
| 929 | case "${expire_policy+t},${default_policy+t}" in |
| 930 | t,) default_policy=$expire_policy ;; |
| 931 | esac |
| 932 | unset expire_policy |
| 933 | $verbose "host $host" |
| 934 | } |
| 935 | |
| 936 | snaptype () { snap=$1; shift; snapargs="$*"; retry=1; } |
| 937 | rsyncargs () { rsyncargs="$*"; } |
| 938 | like () { like="$*"; } |
| 939 | retry () { retry="$*"; } |
| 940 | user () { userat="$*@"; } |
| 941 | |
| 942 | retain () { |
| 943 | case $clear_policy in t) unset expire_policy; clear_policy=nil ;; esac |
| 944 | expire_policy="${expire_policy+$expire_policy |
| 945 | }$*" |
| 946 | } |
| 947 | |
| 948 | ###-------------------------------------------------------------------------- |
| 949 | ### Read the configuration and we're done. |
| 950 | |
| 951 | usage () { |
| 952 | echo "usage: $quis [-nv] [-c CONF]" |
| 953 | } |
| 954 | |
| 955 | version () { |
| 956 | echo "$quis version $VERSION" |
| 957 | } |
| 958 | |
| 959 | whine () { echo >&8 "$@"; } |
| 960 | |
| 961 | while getopts "hVvc:n" opt; do |
| 962 | case "$opt" in |
| 963 | h) usage; exit 0 ;; |
| 964 | V) version; config; exit 0 ;; |
| 965 | v) verbose=whine ;; |
| 966 | c) conf=$OPTARG ;; |
| 967 | n) dryrun=t ;; |
| 968 | *) exit 1 ;; |
| 969 | esac |
| 970 | done |
| 971 | shift $((OPTIND - 1)) |
| 972 | case $# in 0) ;; *) usage >&2; exit 1 ;; esac |
| 973 | exec 8>&1 |
| 974 | |
| 975 | . "$conf" |
| 976 | |
| 977 | runhook end $bkprc |
| 978 | case "$bkprc" in |
| 979 | 0) $verbose "All backups successful" ;; |
| 980 | *) $verbose "Backups FAILED" ;; |
| 981 | esac |
| 982 | |
| 983 | ###----- That's all, folks -------------------------------------------------- |
| 984 | |
| 985 | exit $bkprc |