Merge branch 'staging'
[rsync-backup] / rsync-backup.in
index 292c6eb..ac9cfdc 100644 (file)
@@ -130,7 +130,7 @@ hostrun () {
 
 _hostrun () {
   h=$1 cmd=$2
-  ## Like hostrun, but without the complicated logging, but targetted at a
+  ## Like hostrun, but without the complicated logging, and targetted at a
   ## specific host.
 
   if localp $h; then sh -c "$cmd"
@@ -149,6 +149,33 @@ hostpath () {
 }
 
 ###--------------------------------------------------------------------------
+### Database operations.
+
+INDEXDB=@pkglocalstatedir@/index.db
+
+insert_index () {
+  host=$1 fs=$2 date=$3 vol=$4
+
+  if [ -f "$INDEXDB" ]; then
+    sqlite3 "$INDEXDB" <<EOF
+INSERT INTO idx (host, fs, date, vol)
+       VALUES ('$host', '$fs', '$date', '$vol');
+EOF
+  fi
+}
+
+delete_index () {
+  host=$1 fs=$2 date=$3
+
+  if [ -f "$INDEXDB" ]; then
+    sqlite3 "$INDEXDB" <<EOF
+DELETE FROM idx WHERE
+       host = '$host' AND fs = '$fs' AND $date = '$date';
+EOF
+  fi
+}
+
+###--------------------------------------------------------------------------
 ### Snapshot handling.
 
 ## Snapshot protocol.  Each snapshot type has a pair of functions snap_TYPE
@@ -481,8 +508,10 @@ EOF
 ### Actually taking backups of filesystems.
 
 STOREDIR=@mntbkpdir@/store
+METADIR=@mntbkpdir@/meta
 MAXLOG=14
 HASH=sha256
+unset VOLUME
 
 bkprc=0
 
@@ -528,6 +557,7 @@ expire_backups () {
        echo "delete $date"
        $verbose -n "   expire $date..."
        rm -rf $date $date.*
+       delete_index $host $fs $date
        $verbose " done"
        ;;
     esac
@@ -657,6 +687,7 @@ do_backup () {
       backup_precommit_hook $host $fs $date
       mv new $date
       mv new.fshash $date.fshash
+      insert_index $host $fs $date $VOLUME
       backup_commit_hook $host $fs $date
       mkdir hack
       ln -s $date hack/last
@@ -667,10 +698,14 @@ do_backup () {
   $verbose "   commit"
 
   ## Expire old backups.
+  case "${expire_policy+t},${default_policy+t}" in
+    ,t) expire_policy=$default_policy ;;
+  esac
   case "${expire_policy+t},$dryrun" in
     t,nil) run "expiry for $host:$fs" expire_backups ;;
     t,t) expire_backups ;;
   esac
+  clear_policy=t
 
   ## Report success.
   case $dryrun in
@@ -693,6 +728,12 @@ backup () {
     exit 15
   fi
 
+  ## Read the volume name if we don't have one already.  Again, this allows
+  ## the configuration file to provide a volume name.
+  case "${VOLUME+t}${VOLUME-nil}" in
+    nil) VOLUME=$(cat $METADIR/volume) ;;
+  esac
+
   ## Back up each requested file system in turn.
   for fs in "$@"; do
 
@@ -776,13 +817,23 @@ backup () {
 ###--------------------------------------------------------------------------
 ### Configuration functions.
 
-host () { host=$1; like=; $verbose "host $host"; }
+host () {
+  host=$1
+  like=
+  case "${expire_policy+t},${default_policy+t}" in
+    t,) default_policy=$expire_policy ;;
+  esac
+  unset expire_policy
+  $verbose "host $host"
+}
+
 snaptype () { snap=$1; shift; snapargs="$*"; retry=0; }
 rsyncargs () { rsyncargs="$*"; }
 like () { like="$*"; }
 retry () { retry="$*"; }
 
 retain () {
+  case $clear_policy in t) unset expire_policy; clear_policy=nil ;; esac
   expire_policy="${expire_policy+$expire_policy
 }$*"
 }