FAQ | This is a LIVE service | Changelog

Skip to content
Snippets Groups Projects
new-backup-rsnapshot 3.5 KiB
Newer Older
Dr. Frank Lee's avatar
Dr. Frank Lee committed
#!/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
 ZPOOL=`zfs list -H -oname -S avail -d0 | grep -v zroot | head -n1`
Dr. Frank Lee's avatar
Dr. Frank Lee committed
 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/chem-zfs-backup-server/zfs-rsync.d/${FQDN}_${DIR//\//.} ] && [ -f /etc/chem-zfs-backup-server/zfs-rsync.d/template_${DIR//\//.} ] ; then
  cp /etc/chem-zfs-backup-server/zfs-rsync.d/template_${DIR//\//.} /etc/chem-zfs-backup-server/zfs-rsync.d/${FQDN}_${DIR//\//.}
Dr. Frank Lee's avatar
Dr. Frank Lee committed
 fi
 # Root backup
 ensurezfs $TARGET
 BTID=`backuptask $FQDN $TARGET "$DIR"`
 dopruning $BTID
 echo Backup Task for $FQDN:$DIR is $BTID
}

  ssh-keyscan $FQDN 2>/dev/null >> /root/.ssh/known_hosts
Dr. Frank Lee's avatar
Dr. Frank Lee committed
HOSTID=`ensurehost $FQDN`
echo Host ID is $HOSTID
ensurezfs $TGT
zfs set sharenfs=ro=${FQDN},no_root_squash $TGT
Dr. Frank Lee's avatar
Dr. Frank Lee committed

# Do root backup
backupdir $FQDN $TGT/zfs-rsnap /
# set ssh key in known_hosts
getsshkey $FQDN
Dr. Frank Lee's avatar
Dr. Frank Lee committed

# 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';"