Parcourir la source

add script concierge-backup

guillaume il y a 7 ans
Parent
commit
71961c3ff7
4 fichiers modifiés avec 149 ajouts et 4 suppressions
  1. 21 4
      README.md
  2. 14 0
      doc/examples/backup.cfg.example
  3. 4 0
      doc/examples/backup.exclude.example
  4. 110 0
      src/concierge-backup

+ 21 - 4
README.md

@@ -1,4 +1,4 @@
-**Concierge** is set of tools to help system administrator with maintenance and security of Debian systems. 
+**Concierge** is set of tools to help with the maintenance of Debian systems. 
 
 Upon installation, the package installs a daily cron task to validate the system's configuration. 
 
@@ -6,7 +6,24 @@ Upon installation, the package installs a daily cron task to validate the system
 
 Notify upon issues. Keep noise to a minimum. Keep configuration to a minimum. 
 
-## Tools and features
+## Tools
 
-* concierge-validate: validate system configuration
-* concierge-status: check system status
+### concierge-backup
+
+Create local and remote backups for file and PostgreSQL database backups. 
+
+Configuration: /etc/concierge/backup.cfg
+
+Dependency: borgbackup
+
+### concierge-validate
+
+Validate system configuration. 
+
+Configuration: none
+
+### concierge-status
+
+Check system status.
+
+Configuration: none

+ 14 - 0
doc/examples/backup.cfg.example

@@ -0,0 +1,14 @@
+# Directories to backup
+BACKUP_DIR_INCL="/etc /var/mail"
+
+# Local backup destination
+LOCAL_DIR=/var/backups
+
+# Remote backup destination
+REMOTE_ENABLE=true
+REMOTE_USER=backupuser
+REMOTE_HOSTNAME=backupotron5000.example.net
+REMOTE_DIR=/var/backups
+
+# PostgreSQL backup options
+#PGSQL_EXCLUDE_DATNAME="['template_']"

+ 4 - 0
doc/examples/backup.exclude.example

@@ -0,0 +1,4 @@
+# List of paths to exclude from backup
+
+# Example: uncommend the following line to exclude the etckeeper repository
+#/etc/.git

+ 110 - 0
src/concierge-backup

@@ -0,0 +1,110 @@
+#!/bin/sh
+# Requirements : borgbackup
+
+# Read configuration
+. /etc/concierge/backup.cfg
+
+if [ -z "${LOCAL_DIR}" ]; then
+  echo >&2 "LOCAL_DIR is not set.  Aborting.";
+  exit 1;
+fi
+
+if [ -z "${REMOTE_ENABLE}" ]; then
+
+  echo >&2 "REMOTE_ENABLE is not set.  Aborting.";
+  exit 1;
+
+elif [ ${REMOTE_ENABLE} = true ]; then
+
+  if [ -z "${REMOTE_USER}" ]; then
+    echo >&2 "REMOTE_USER is not set.  Aborting.";
+    exit 1;
+  fi
+
+  if [ -z "${REMOTE_HOSTNAME}" ]; then
+    echo >&2 "REMOTE_HOSTNAME is not set.  Aborting.";
+    exit 1;
+  fi
+
+  if [ -z "${REMOTE_DIR}" ]; then
+    echo >&2 "REMOTE_DIR is not set.  Aborting.";
+    exit 1;
+  fi
+
+fi
+
+if [ -z "${PGSQL_EXCLUDE_DATNAME}" ]; then
+  PGSQL_EXCLUDE_DATNAME="['template_']"
+fi
+
+HOSTNAME=`hostname`
+LOCAL_DEST=${LOCAL_DIR}/${HOSTNAME}.borg
+
+DATE=`date +%Y-%m-%d`
+BACKUP_NAME="${HOSTNAME}_${DATE}"
+
+# Temporary directory for backup
+BACKUP_TEMP="${LOCAL_DIR}/output-daily"
+
+if ! type "borg" > /dev/null; then
+  echo >&2 "borgbackup is not installed.  Aborting.";
+  exit 1;
+fi
+
+if [ -d "${BACKUP_TEMP}" ]; then
+  echo >&2 "Directory ${BACKUP_TEMP} already exists.  Aborting.";
+  exit 1;
+else
+  mkdir "${BACKUP_TEMP}"
+fi
+
+# Backup ejabberd data if ejabberd_ctl is available
+if type "ejabberdctl" > /dev/null; then
+  EJABBERD_BACKUP=`mktemp -p ~ejabberd/`
+  ejabberdctl backup "$EJABBERD_BACKUP"
+  mv "$EJABBERD_BACKUP" $BACKUP_TEMP/ejabberd.backup
+fi
+
+# Backup PostgreSQL data if pg_dump is available, and user postgres exists
+if type "pg_dump" > /dev/null; then
+  if id -u "postgres" > /dev/null 2>&1; then
+    PG_DBS=`sudo -i -u postgres psql template1 -t -c "SELECT datname FROM pg_database WHERE NOT datname LIKE ANY(ARRAY${PGSQL_EXCLUDE_DATNAME})"`
+    for PG_DB in $PG_DBS
+    do
+      PG_BACKUP=`sudo -u postgres mktemp -t pg_dump_XXXX`
+      sudo -u postgres -i pg_dump -Z3 -Fc "${PG_DB}" > "${PG_BACKUP}"
+      mv "${PG_BACKUP}" "${BACKUP_TEMP}/pgsql_${PG_DB}_Fc.dump"
+    done
+  fi
+fi
+
+if [ ! -d "${LOCAL_DEST}" ]; then
+  # Initialize local borg repository
+  borg init --encryption=none "${LOCAL_DEST}"
+fi
+
+# Do local backup
+borg create --exclude-from /etc/concierge/backup.exclude ${LOCAL_DEST}::${BACKUP_NAME} "${BACKUP_TEMP}" ${BACKUP_DIR_INCL}
+
+# Prune local archives to keep only 7 daily, and 4 weekly, and 3 monthly ones
+borg prune --keep-daily=7 --keep-weekly=4 --keep-monthly=3 ${LOCAL_DEST}
+
+if [ ${REMOTE_ENABLE} = true ]; then
+
+  REMOTE_DEST=${REMOTE_DIR}/${HOSTNAME}.borg
+
+  # Quick check of the remote repository before doing a remote backup
+  if borg check --repository-only ${REMOTE_USER}@${REMOTE_HOSTNAME}:${REMOTE_DEST}; then
+    # Do remote backup
+    borg create --exclude-from /etc/concierge/backup.exclude ${REMOTE_USER}@${REMOTE_HOSTNAME}:${REMOTE_DEST}::${BACKUP_NAME} "${BACKUP_TEMP}" ${BACKUP_DIR_INCL}
+    # Prune remote archives to keep only 7 daily, and 4 weekly, and 3 monthly ones
+    borg prune --keep-daily=7 --keep-weekly=4 --keep-monthly=3 ${REMOTE_USER}@${REMOTE_HOSTNAME}:${REMOTE_DEST}
+  else
+    echo >&2 "Remote repository is missing of corrupted. Try running: "
+    echo >&2 "borg init --encryption=none ${REMOTE_USER}@${REMOTE_HOSTNAME}:${REMOTE_DEST}"
+  fi
+
+fi
+
+# Delete temporary directory
+rm -rf "${BACKUP_TEMP}"