3 #==============================================================================
5 # File ID: 99575e5c-5d3e-11df-9eee-90e6ba3022ac
9 # Author: Øyvind A. Holm <sunny@sunbase.org>
10 # License: GNU General Public License version 2 or later.
11 #==============================================================================
22 while test -n "$1"; do
24 -d|
--dbname) opt_dbname
="$2"; shift 2 ;;
25 -h|
--help) opt_help
=1; shift ;;
26 --host) opt_host
="$2"; shift 2 ;;
27 -q|
--quiet) opt_quiet
=$
(($opt_quiet + 1)); shift ;;
28 --upgrade-db) opt_upgrade_db
=1; shift ;;
29 -v|
--verbose) opt_verbose
=$
(($opt_verbose + 1)); shift ;;
30 --version) echo $progname $VERSION; exit 0 ;;
33 if printf '%s\n' "$1" |
grep -q ^
-; then
34 echo "$progname: $1: Unknown option" >&2
42 opt_verbose
=$
(($opt_verbose - $opt_quiet))
44 if test "$opt_help" = "1"; then
45 test $opt_verbose -gt 0 && { echo; echo $progname $VERSION; }
50 Usage: $progname [options]
54 -d DBFILE, --dbname DBFILE
55 Use DBFILE as database file for SQLite.
59 Use HOSTNAME as value in the host column.
61 Be more quiet. Can be repeated to increase silence.
63 Upgrade database to the current schema without adding new entries.
64 Can be used when converting old databases.
66 Increase level of verbosity. Can be repeated.
68 Print version information.
75 echo "$progname: $*" >&2
81 db
="$logdir/df.sqlite"
82 if test -n "$opt_host"; then
87 if test -n "$opt_dbname"; then
89 logdir
="$(dirname "$db")"
92 [ -d "$logdir" ] || mkdir
-p "$logdir" ||
{
93 msg
$logdir: Could not create directory
97 sql_create_table_meta
=$
(cat <<SQL_END
105 INSERT INTO meta VALUES ('dbversion', '$dbversion');
109 sql_create_table_df
=$
(cat <<SQL_END
112 CONSTRAINT df_date_length
113 CHECK (length(date) = 19)
114 CONSTRAINT df_date_valid
115 CHECK (datetime(date) IS NOT NULL)
119 CONSTRAINT df_host_length
120 CHECK (length(host) > 0)
124 CONSTRAINT df_mountp_length
125 CHECK (length(mountp) > 0)
129 CONSTRAINT df_device_length
130 CHECK (length(device) > 0)
134 CONSTRAINT df_size_length
135 CHECK (length(size) > 0)
136 CONSTRAINT df_size_positive
141 CONSTRAINT df_used_length
142 CHECK (length(used) > 0)
143 CONSTRAINT df_used_positive
148 CONSTRAINT df_free_length
149 CHECK (length(free) > 0)
150 CONSTRAINT df_free_positive
154 UNIQUE (date, host, mountp)
160 sql_create_trigger_nodups
=$
(cat <<SQL_END
161 CREATE TRIGGER nodups
164 SELECT RAISE (IGNORE)
167 WHERE mountp = NEW.mountp
174 if test ! -f "$db"; then
175 msg Creating database
$db
176 cat <<SQL_END | $SQLITE "$db"
177 BEGIN EXCLUSIVE TRANSACTION;
178 $sql_create_table_meta
180 $sql_create_trigger_nodups
186 $SQLITE "$db" "SELECT value FROM meta WHERE key = 'dbversion';"
190 if test "$curr_db_version" = "$dbversion"; then
191 msg Database
$db is already version
$dbversion
194 backup
="$db.$(date -u +"%Y
%m
%dT
%H
%M
%SZ
").db-upgrade.bck"
195 if test -e "$backup"; then
196 # Shouldn't happen, but keep it safe
197 msg Database backup
$backup already exists
, aborting
200 cp -p "$db" "$backup" ||
{
201 msg Could not backup database
$db to
$backup
204 msg Database backup stored as
$backup
206 sql
="BEGIN EXCLUSIVE TRANSACTION;
208 if test -z "$curr_db_version"; then
209 # First version without meta table
210 msg Upgrading database to version
1
211 sql
="$sql$(cat <<SQL_END
212 $sql_create_table_meta
217 if test $curr_db_version -lt 2; then
219 # - add host column and set all earlier entries to the current
221 # - delete percent column
222 msg Upgrading database to version
2
223 sql
="$sql$(cat <<SQL_END
224 CREATE TABLE tmp_df AS
225 SELECT date, '$host' AS host, mountp, device, size, used, free FROM df;
228 $sql_create_trigger_nodups
233 UPDATE meta SET value = 2
234 WHERE key = 'dbversion';
245 echo "$sql" |
$SQLITE "$db" ||
return 1
246 msg Database
$db successfully upgraded to version
$dbversion
250 if test "$opt_upgrade_db" = "1"; then
255 if test -z "$curr_db_version"; then
256 msg dbversion not found
, upgrading database
$db
258 msg Database upgrade failed
263 if test $curr_db_version -lt $dbversion; then
264 msg Database
$db is version
$curr_db_version, needs upgrade
266 msg Database upgrade failed
277 if (/^(\\S+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\S+)%\\s+(\\S+)\$/) {
278 my (\$device, \$size, \$used, \$free, \$mountp) =
279 ( \$1, \$2, \$3, \$4, \$6);
281 \"INSERT INTO df \" .
282 \"(date, host, mountp, \" .
283 \" device, size, used, free) \" .
285 \"datetime('now'), '$host', '\$mountp', \" .
286 \"'\$device', \$size, \$used, \$free\" .
292 warn(\"$progname: Invalid line: \\"\
$_\\"\\n\");
297 # vim: set ts=8 sw=8 sts=8 noet fo+=w tw=79 fenc=UTF-8 :