Browse Source

fcn-backup: add configuration options to disable remote backup, exclude PostgreSQL databases from backup

Guillaume 7 years ago
parent
commit
c9e810711e
2 changed files with 35 additions and 15 deletions
  1. 3 0
      backup.cfg.example
  2. 32 15
      fcn-backup

+ 3 - 0
backup.cfg.example

@@ -2,6 +2,9 @@
 LOCAL_DIR=/path/to/backups
 LOCAL_DIR=/path/to/backups
 
 
 # Remote backup destination
 # Remote backup destination
+REMOTE_ENABLE=true
 REMOTE_USER=backupuser
 REMOTE_USER=backupuser
 REMOTE_HOSTNAME=backupotron5000.example.net
 REMOTE_HOSTNAME=backupotron5000.example.net
 
 
+# PostgreSQL backup options
+#PGSQL_EXCLUDE_DATNAME=['template_']

+ 32 - 15
fcn-backup

@@ -9,14 +9,27 @@ if [ -z "${LOCAL_DIR}" ]; then
   exit 1;
   exit 1;
 fi
 fi
 
 
-if [ -z "${REMOTE_USER}" ]; then
-  echo >&2 "REMOTE_USER is not set.  Aborting.";
+if [ -z "${REMOTE_ENABLE}" ]; then
+
+  echo >&2 "REMOTE_ENABLE is not set.  Aborting.";
   exit 1;
   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
+
 fi
 fi
 
 
-if [ -z "${REMOTE_HOSTNAME}" ]; then
-  echo >&2 "REMOTE_HOSTNAME is not set.  Aborting.";
-  exit 1;
+if [ -z "${PGSQL_EXCLUDE_DATNAME}" ]; then
+  PGSQL_EXCLUDE_DATNAME="['template_']"
 fi
 fi
 
 
 HOSTNAME=`hostname`
 HOSTNAME=`hostname`
@@ -50,7 +63,7 @@ fi
 # Backup PostgreSQL data if pg_dump is available, and user postgres exists
 # Backup PostgreSQL data if pg_dump is available, and user postgres exists
 if type "pg_dump" > /dev/null; then
 if type "pg_dump" > /dev/null; then
   if id -u "postgres" > /dev/null 2>&1; 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 datname NOT LIKE 'template_'"`
+    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
     for PG_DB in $PG_DBS
     do
     do
       PG_BACKUP=`sudo -u postgres mktemp -t pg_dump_XXXX`
       PG_BACKUP=`sudo -u postgres mktemp -t pg_dump_XXXX`
@@ -71,15 +84,19 @@ borg create --exclude-from /etc/fcn-toolbox/backup.exclude ${BACKUP_DEST}::${BAC
 # Prune local archives to keep only 7 daily, and 4 weekly, and 3 monthly ones
 # 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 ${BACKUP_DEST}
 borg prune --keep-daily=7 --keep-weekly=4 --keep-monthly=3 ${BACKUP_DEST}
 
 
-# Quick check of the remote repository before doing a remote backup
-if borg check --repository-only ${REMOTE_USER}@${REMOTE_HOSTNAME}:${BACKUP_DEST}; then
-  # Do remote backup
-  borg create --exclude-from /etc/fcn-toolbox/backup.exclude ${REMOTE_USER}@${REMOTE_HOSTNAME}:${BACKUP_DEST}::${BACKUP_NAME} "/etc" "${BACKUP_TEMP}"
-  # 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}:${BACKUP_DEST}
-else
-  echo >&2 "Remote repository is missing of corrupted. Try running: "
-  echo >&2 "borg init --encryption=none ${REMOTE_USER}@${REMOTE_HOSTNAME}:${BACKUP_DEST}"
+if [ ${REMOTE_ENABLE} = true ]; then
+
+  # Quick check of the remote repository before doing a remote backup
+  if borg check --repository-only ${REMOTE_USER}@${REMOTE_HOSTNAME}:${BACKUP_DEST}; then
+    # Do remote backup
+    borg create --exclude-from /etc/fcn-toolbox/backup.exclude ${REMOTE_USER}@${REMOTE_HOSTNAME}:${BACKUP_DEST}::${BACKUP_NAME} "/etc" "${BACKUP_TEMP}"
+    # 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}:${BACKUP_DEST}
+  else
+    echo >&2 "Remote repository is missing of corrupted. Try running: "
+    echo >&2 "borg init --encryption=none ${REMOTE_USER}@${REMOTE_HOSTNAME}:${BACKUP_DEST}"
+  fi
+
 fi
 fi
 
 
 # Delete temporary directory
 # Delete temporary directory