Newer
Older
#!/bin/bash
# To create a new host in the database
if [ -z $1 ] ; then
echo Use: $0 hostname [flag [flag [...]]]
echo Known flags: postgres
exit 1
fi
HN=$1
shift
set -e
# Find FQDN for $HN
FQDN=`getent hosts $HN | awk ' { print $2 } '`
echo Searching for existing ZFS ...
# Find an existing ZFS for this host:
# Never use zroot
ZPS=` zpool list -H -o name | grep -v zroot`
TGT=''
for ZPOOL in $ZPS ; do
if [ -z $TGT ] ; then
if zfs list -H -o name $ZPOOL/$FQDN >/dev/null 2>&1 ; then
TGT=$ZPOOL/$FQDN
fi
fi
done
# Load some settings
. /etc/default/zfs-backup
if [ -z $TGT ] ; then
echo No existing ZFS found
# Find emptiest zpool

Dr Adam Thorn
committed
ZPOOL=`zfs list -H -oname -S avail -d0 | grep -v zroot | grep -v $SOURCE | head -n1`
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
TGT=$ZPOOL/$FQDN
echo Using $ZPOOL
fi
function ensurezfs() {
ZFS=$1
if ! zfs list $ZFS >/dev/null 2>&1; then
zfs create $ZFS
fi
}
function dosql() {
psql -t -c "$1"
}
# Some SQL to create the host
#select host_id from host where hostname='database.ch.private.cam.ac.uk';
function ensurehost() {
FQDN=$1
HOSTID=`dosql "select host_id from host where hostname='$FQDN';"`
if [ -z $HOSTID ] ; then
HOSTID=`dosql "
insert into host (hostname, offline, disabled) values ('$FQDN',false,true);
select host_id from host where hostname='$FQDN';"`
fi
echo $HOSTID
}
function backuptask() {
FQDN=$1
TARGET=$2
DIR=$3
BTID=`dosql "
select backup_task_id from backup_task natural join host natural join zfs_rsync_detail where hostname='$FQDN' and directory_source='$DIR' and backup_type_id=3;"`
SN=`echo $FQDN | sed s/\\\\..*//`
if [ -z $BTID ] ; then
BTID=`dosql "
insert into backup_task(host_id,backup_task_name,timeout_soft,timeout_hard,zfs_target,backup_type_id) values
($HOSTID,'Rsync $DIR on $SN',14400,86400,'$TARGET',3);
insert into zfs_rsync_detail (backup_task_id,directory_source) values
((select backup_task_id from backup_task where backup_task_name='Rsync $DIR on $SN' and host_id=$HOSTID),'$DIR');
select backup_task_id from backup_task natural join host natural join zfs_rsync_detail where hostname='$FQDN' and directory_source='$DIR' and backup_type_id=3;"`
fi
echo $BTID
}
# Does that backup task have pruning?
function dopruning() {
BTID=$1
PRUNID=`dosql "
select count(*) from zfs_pruning where backup_task_id=$BTID;"`
if [ $PRUNID = 0 ] ; then
# Add the pruning
dosql "
insert into zfs_pruning (backup_task_id,zfs_pruning_age,zfs_pruning_count,flagname) values
($BTID,86400,6,'hourly'),
($BTID,604800,7,'daily'),
($BTID,2419200,4,'weekly'),
($BTID,14515200,6,'monthly');"
fi
}
function backupdir() {
FQDN=$1
TARGET=$2
DIR=$3
# Create settings file
if ! [ -f /etc/zfs-rsync.d/${FQDN}_${DIR//\//.} ] && [ -f /etc/zfs-rsync.d/template_${DIR//\//.} ] ; then
cp /etc/zfs-rsync.d/template_${DIR//\//.} /etc/zfs-rsync.d/${FQDN}_${DIR//\//.}
fi
# Root backup
ensurezfs $TARGET
BTID=`backuptask $FQDN $TARGET "$DIR"`
dopruning $BTID
echo Backup Task for $FQDN:$DIR is $BTID
}
HOSTID=`ensurehost $FQDN`
echo Host ID is $HOSTID
ensurezfs $TGT
# Do root backup
backupdir $FQDN $TGT/zfs-rsnap /
# Parse flags
while ! [ -z $1 ] ; do
case $1 in
postgres)
backupdir $FQDN $TGT/postgresql /var/lib/postgresql
;;
*)
echo Unknown flag $1
;;
esac
shift
done
dosql "update host set disabled=false where hostname='$FQDN';"