chiark / gitweb /
Maintain an index of backup artifacts.
[rsync-backup] / update-bkp-index.in
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 mkdir -p @pkglocalstatedir@
29 INDEXDB=@pkglocalstatedir@/index.db
30 : ${STOREDIR=@mntbkpdir@/store}
31 : ${METADIR=@mntbkpdir@/meta}
32
33 if [ ! -f $STOREDIR/.rsync-backup-store ]; then
34   echo >&2 "$quis: no backup volume mounted"
35   exit 15
36 fi
37 : ${VOLUME=$(cat $METADIR/volume)}
38
39 ## If the database exists then we're OK.  (This will turn into a version
40 ## check and upgrade if the schema changes.)
41 if [ ! -f "$INDEXDB" ]; then
42
43   ## Create the database.
44   rm -f "$INDEXDB.new"
45   sqlite3 "$INDEXDB.new" <<EOF
46 CREATE TABLE meta (
47         version INTEGER NOT NULL);
48 INSERT INTO meta (version) VALUES (0);
49
50 CREATE TABLE idx (
51         host TEXT NOT NULL,
52         fs TEXT NOT NULL,
53         date TEXT NOT NULL,
54         vol TEXT NOT NULL,
55         PRIMARY KEY (host, fs, date));
56 CREATE INDEX idx_byvol ON idx (vol);
57 EOF
58
59   ## Done.
60   mv "$INDEXDB.new" "$INDEXDB"
61 fi
62
63 {
64   ## Do everything in a single transaction.  SQLite is pretty good at this,
65   ## and also it'll avoid updating the database until it sees a `COMMIT'
66   ## command, so if we fail halfway through we're still OK.  So it's safe to
67   ## start by removing all of the current records referring to this volume.
68   cat <<EOF
69 BEGIN;
70 DELETE FROM idx WHERE vol = '$VOLUME';
71 EOF
72
73   ## Now work through the various filesystems.  This is a slightly cheesy way
74   ## of finding them.
75   for i in $STOREDIR/*/*/last; do
76
77     ## Parse out the host and filesystem names.
78     i=${i%/*}
79     fs=${i##*/} i=${i%/*}
80     host=${i##*/} i=${i%/*}
81
82     ## And work through the date list.
83     for j in $STOREDIR/$host/$fs/*; do
84       if [ -L "$j" ] || [ ! -d "$j" ]; then continue; fi
85       j=${j%/}
86       date=${j##*/}
87       cat <<EOF
88 INSERT INTO idx (host, fs, date, vol)
89         VALUES ('$host', '$fs', '$date', '$VOLUME');
90 EOF
91     done
92   done
93
94   ## Done.
95   cat <<EOF
96 COMMIT;
97 EOF
98 } | sqlite3 "$INDEXDB"
99
100 ###----- That's all, folks --------------------------------------------------