chiark / gitweb /
check-bkp-status.in: Zap trailing . in footer message.
[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, vol, date));
56 CREATE INDEX idx_byhostfsdate ON idx (host, fs, date);
57 CREATE INDEX idx_byvol ON idx (vol);
58 EOF
59
60   ## Done.
61   mv "$INDEXDB.new" "$INDEXDB"
62 fi
63
64 {
65   ## Do everything in a single transaction.  SQLite is pretty good at this,
66   ## and also it'll avoid updating the database until it sees a `COMMIT'
67   ## command, so if we fail halfway through we're still OK.  So it's safe to
68   ## start by removing all of the current records referring to this volume.
69   cat <<EOF
70 BEGIN;
71 DELETE FROM idx WHERE vol = '$VOLUME';
72 EOF
73
74   ## Now work through the various filesystems.  This is a slightly cheesy way
75   ## of finding them.
76   for i in $STOREDIR/*/*/last; do
77
78     ## Parse out the host and filesystem names.
79     i=${i%/*}
80     fs=${i##*/} i=${i%/*}
81     host=${i##*/} i=${i%/*}
82
83     ## And work through the date list.
84     for j in $STOREDIR/$host/$fs/*; do
85       if [ -L "$j" ] || [ ! -d "$j" ]; then continue; fi
86       j=${j%/}
87       date=${j##*/}
88       cat <<EOF
89 INSERT INTO idx (host, fs, date, vol)
90         VALUES ('$host', '$fs', '$date', '$VOLUME');
91 EOF
92     done
93   done
94
95   ## Done.
96   cat <<EOF
97 COMMIT;
98 EOF
99 } | sqlite3 "$INDEXDB"
100
101 ###----- That's all, folks --------------------------------------------------