FAQ | This is a LIVE service | Changelog

Skip to content
Snippets Groups Projects
prepare-ignore-mysql-and-postgres 2.92 KiB
Newer Older
#!/bin/bash
# A script which runs, largely on the remote machine, to prepare for backup
# Jobs to do: 
# * Exclude files which are only found in packages
# * List all packages which are installed
# * (optionally) dump PostgreSQL into a file

# NB this is intended to be used in conjunction with a separate mysql backup
# task. To do this:
# 1) /usr/lib/chem-zfs-backup-server/new-backup-rsync $HOSTNAME mysql
# 2) add "--exclude=/var/lib/mysql --exclude=/var/lib/mysql-files" to
#    to zfs-rsync.d config for $HOSTNAME

CONFDIR=/etc/chem-zfs-backup-server/zfs-rsync.d
SSH="ssh -p ${SSHPORT:-22} -o ConnectTimeout=10"
SCP="scp -P ${SSHPORT:-22}"
#echo $SSH
set -xv
SERVER=$1
if [ -z $SERVER ] ; then
 echo $0 Server
 exit 1
fi
(
# Has the package status changed since last time we generated the list of files?
if $SSH root@$SERVER [ /var/lib/dpkg/status -nt /var/adm/backup/package-files ] ; then
 # Need to rebuild package-files
 $SSH root@$SERVER "
  umask 077
  FILELIST=\`mktemp\`
  CONFLIST=\`mktemp\`
  mkdir -p /var/adm/backup
  # Make logrotate use datestamps
  if ! grep -q dateext /etc/logrotate.conf ; then sed  -i 's/^include/dateext\ninclude/' /etc/logrotate.conf ; fi
  # Which packages are installed?
  dpkg --get-selections | awk ' { print \$1 ; } ' >/var/adm/backup/packages
  cat /var/lib/dpkg/info/*.list | while read F ; do [ -f \"\$F\" ] && echo \"\$F\" ; done | sort > \$FILELIST
  awk '/Description:/ { flag = 0 } ; flag == 1 { print \$1 ; }  ; /Conffiles:/ { flag = 1  } ; ' </var/lib/dpkg/status |  sort >\$CONFLIST
  diff -u \$FILELIST \$CONFLIST | grep ^-/ | sed s/^-// | xargs -n1 realpath >/var/adm/backup/package-files
  rm \$FILELIST \$CONFLIST
 "
 mkdir -p $CONFDIR/$SERVER
 $SCP -q root@$SERVER:/var/adm/backup/package-files $CONFDIR/$SERVER/exclude
fi
# Ensure that the excludes file exists, even if we haven't updated it this time
if [ ! -f $CONFDIR/$SERVER/exclude ] ; then
  mkdir -p $CONFDIR/$SERVER
 $SCP -q root@$SERVER:/var/adm/backup/package-files $CONFDIR/$SERVER/exclude
fi

# If we can identify this as a xen VM, add some extra dirs to the exclude list
# because they include some files built by the kernel package but are not
# provided by it.
# systemd-detect-virt is present on all our Ubuntu VMs so is hopefully a
# reliable method to use
VIRT_TYPE=$(ssh root@$SERVER systemd-detect-virt 2>/dev/null)
if [ "$VIRT_TYPE" == "xen" ] ; then
cat<<EOF >> $CONFDIR/$SERVER/exclude
/lib/modules
/lib/firmware
EOF
fi

$SSH root@$SERVER "
# Ensure that things are not readable where they ought not to be
umask 077

# Save the package configuration info
which debconf-get-selections >/dev/null 2>&1 || apt-get install debconf-utils
which rsync >/dev/null 2>&1 || apt-get install rsync
debconf-get-selections  >/var/adm/backup/debconf

# Dump LDAP, if we can
which slapcat >/dev/null 2>&1 && slapcat >/var/adm/backup/ldap

# Ignore exit status of this command
true
"
echo Prepared $SERVER `date`
) >/var/log/chem-zfs-backup-server/${SERVER}-prepare 2>&1