]> Frank Brehm's Git Trees - pixelpark/admin-tools.git/commitdiff
Adding cleaning up of old directories
authorFrank Brehm <frank.brehm@pixelpark.com>
Thu, 16 Nov 2017 12:20:25 +0000 (13:20 +0100)
committerFrank Brehm <frank.brehm@pixelpark.com>
Thu, 16 Nov 2017 12:20:25 +0000 (13:20 +0100)
bin/backup_pgsql.sh

index 2c8013d8aa4e9075ff36eef4ce6ad094c31b0b7f..b1a7ea3ea8da09e97db3e8431096ac053130b750 100755 (executable)
@@ -43,7 +43,7 @@ VERBOSE="n"
 DEBUG="n"
 QUIET='n'
 
-VERSION="2.1"
+VERSION="2.2"
 
 # console colors:
 RED=""
@@ -63,7 +63,7 @@ declare -a DATABASES=()
 # Modify below variables to fit your need ----
 #########################################################
 # Keep backup for how many days. Default is 90 days.
-KEEP_DAYS='90'
+KEEP_DAYS='30'
 
 # System user used to run PostgreSQL daemon.
 #   - On Linux, it's postgres.
@@ -72,7 +72,7 @@ KEEP_DAYS='90'
 PGSQL_SYS_USER="postgres"
 
 # Where to store backup copies.
-BACKUP_ROOTDIR="/var/backup"
+BACKUP_ROOTDIR="/var/backup/pgsql"
 
 # Date.
 YEAR="$( date +%Y)"
@@ -85,7 +85,7 @@ TIMESTAMP="${YEAR}-${MONTH}-${DAY}-${TIME}"
 BACKUP_SUCCESS='YES'
 
 # Define, check, create directories.
-BACKUP_DIR="${BACKUP_ROOTDIR}/pgsql/${YEAR}/${MONTH}/${DAY}"
+BACKUP_DIR="${BACKUP_ROOTDIR}/${YEAR}/${MONTH}/${DAY}"
 TMP_DIR=
 
 #-------------------------------------------------------------------
@@ -148,7 +148,7 @@ usage() {
               ${BASENAME} [-V|--version]
 
            Options:
-               -k|--keep DAYS  Keep the backup files of the last DAYS. Default: ${KEEP_DAYS} days.
+               -K|--keep DAYS  Keep the backup files of the last DAYS. Default: ${KEEP_DAYS} days.
                -d|--debug      Debug output (bash -x).
                -v|--verbose    Set verbosity on. Mutually exclusive to '--quiet'.
                -q|--quiet      Quiet execution, only errors and warnings are shown.
@@ -183,8 +183,8 @@ get_options() {
 
     while true ; do
         case "$1" in
-            -k|--keep)
-                KEEP_DAYS="$1"
+            -K|--keep)
+                KEEP_DAYS="$2"
                 shift
                 shift
                 ;;
@@ -236,6 +236,18 @@ get_options() {
         exit 1
     fi
 
+    local keep_int=$(( $KEEP_DAYS + 0 ))
+    if [[ "${keep_int}" -le "0" ]] ; then
+        error "Invalid number of days '${RED}${KEEP_DAYS}${NORMAL}' to keep backup files."
+        echo >&2
+        description >&2
+        echo
+        usage >&2
+        exit 1
+    fi
+    debug "Keeping backupfiles, which are not older than ${keep_int} days."
+    KEEP_DAYS="${keep_int}"
+
     local cur_user=$( id -u -n )
     if [[ "${cur_user}" != "${PGSQL_SYS_USER}" ]] ; then
         error "Wrong user '${RED}${cur_user}${NORMAL}'."
@@ -300,6 +312,15 @@ RM() {
     eval ${cmd} "$@"
 }
 
+#------------------------------------------------------------------------------
+RMDIR() {
+    local cmd="rmdir"
+    if [[ "${VERBOSE}" == "y" ]] ; then
+        cmd+=" --verbose"
+    fi
+    eval ${cmd} "$@"
+}
+
 #------------------------------------------------------------------------------
 empty_line() {
     if [[ "${QUIET}" == "y" ]] ; then
@@ -331,6 +352,7 @@ get_databases() {
         for db in "${DATABASES[@]}" ; do
             echo " * '${db}'"
         done
+        echo
     fi
 
 }
@@ -351,11 +373,58 @@ prepare_dirs() {
     TMP_DIR=$( mktemp -d -p "${HOME}" backup.XXXXXXXX.d )
     debug "Temporary directory is '${TMP_DIR}'."
 
-    debug "Cretaing trap to cleanup temporary directory ..."
+    debug "Creating trap to cleanup temporary directory ..."
     trap cleanup_tmp_dir INT TERM EXIT ABRT
 
 }
 
+#------------------------------------------------------------------------------
+cleanup_old_backups() {
+
+    info "Cleaning up old backup files and directories ..."
+
+    local verbose_option=""
+    if [[ "${VERBOSE}" == "y" ]] ; then
+        verbose_option="--verbose"
+    fi
+
+    find "${BACKUP_ROOTDIR}" -type f -mtime +${KEEP_DAYS} -print0 | \
+        xargs --null --no-run-if-empty rm ${verbose_option}
+
+    local year=
+    local month=
+    local day=
+
+    for year in $( ls -1 "${BACKUP_ROOTDIR}" ); do
+        local y_dir="${BACKUP_ROOTDIR}/${year}"
+        if [[ -d "${y_dir}" ]] ; then
+            for month in $( ls -1 "${y_dir}" ); do
+                local m_dir="${y_dir}/${month}"
+                if [[ -d "${m_dir}" ]] ; then
+                    for day in $( ls -1 "${m_dir}" ); do
+                        local d_dir="${m_dir}/${day}"
+                        if [[ -d "${d_dir}" && "${d_dir}" != "${BACKUP_DIR}" ]] ; then
+                            rmdir --ignore-fail-on-non-empty "${d_dir}"
+                            if [[ ! -d "${d_dir}" ]] ; then
+                                debug "Removed directory '${d_dir}'."
+                            fi
+                        fi
+                    done
+                    rmdir --ignore-fail-on-non-empty "${m_dir}"
+                    if [[ ! -d "${m_dir}" ]] ; then
+                        debug "Removed directory '${m_dir}'."
+                    fi
+                fi
+            done
+            rmdir --ignore-fail-on-non-empty "${y_dir}"
+            if [[ ! -d "${y_dir}" ]] ; then
+                debug "Removed directory '${y_dir}'."
+            fi
+        fi
+    done
+
+}
+
 ################################################################################
 ##
 ## Main
@@ -369,6 +438,8 @@ main() {
 
     get_databases
     prepare_dirs
+    cleanup_old_backups
+
     empty_line
     debug "Deactivating trap."
     trap - INT TERM EXIT ABRT