DEBUG="n"
QUIET='n'
-VERSION="2.1"
+VERSION="2.2"
# console colors:
RED=""
# 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.
PGSQL_SYS_USER="postgres"
# Where to store backup copies.
-BACKUP_ROOTDIR="/var/backup"
+BACKUP_ROOTDIR="/var/backup/pgsql"
# Date.
YEAR="$( date +%Y)"
BACKUP_SUCCESS='YES'
# Define, check, create directories.
-BACKUP_DIR="${BACKUP_ROOTDIR}/pgsql/${YEAR}/${MONTH}/${DAY}"
+BACKUP_DIR="${BACKUP_ROOTDIR}/${YEAR}/${MONTH}/${DAY}"
TMP_DIR=
#-------------------------------------------------------------------
${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.
while true ; do
case "$1" in
- -k|--keep)
- KEEP_DAYS="$1"
+ -K|--keep)
+ KEEP_DAYS="$2"
shift
shift
;;
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}'."
eval ${cmd} "$@"
}
+#------------------------------------------------------------------------------
+RMDIR() {
+ local cmd="rmdir"
+ if [[ "${VERBOSE}" == "y" ]] ; then
+ cmd+=" --verbose"
+ fi
+ eval ${cmd} "$@"
+}
+
#------------------------------------------------------------------------------
empty_line() {
if [[ "${QUIET}" == "y" ]] ; then
for db in "${DATABASES[@]}" ; do
echo " * '${db}'"
done
+ echo
fi
}
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
get_databases
prepare_dirs
+ cleanup_old_backups
+
empty_line
debug "Deactivating trap."
trap - INT TERM EXIT ABRT