Allow second directory to duplicate backups

This commit is contained in:
Jeremy Gardais 2021-04-12 19:37:17 +02:00
parent 47cab2c053
commit 0fc60ef863
Signed by: jegardai
GPG Key ID: E759BAA22501AF32
1 changed files with 36 additions and 7 deletions

View File

@ -3,8 +3,11 @@
# This script will backup /etc/pve content : # This script will backup /etc/pve content :
# 1. Make an archive to a first directory (default: /etc/proxmox.pve/backup/). # 1. Make an archive to a first directory (default: /etc/proxmox.pve/backup/).
# 2. Hard link a fix archive name (pve.latest.tar.gz) to new archive # 2. Hard link a fix archive name (pve.latest.tar.gz) to new archive
# Easy to monitor (this path can be expected). # Easy to monitor (eg. this path can be expected).
# 3. Clean backups older than retention time (default: 7). # 3. Limit permissions to backup directory (backup:adm).
# 4. Clean backups older than retention time (default: 7).
# 5. (optionnal) Copy backup to a second directory (nfs mountpoint, other hdd,…).
#
# This script can be call by a cronjob (eg. daily). # This script can be call by a cronjob (eg. daily).
@ -29,7 +32,7 @@ readonly COLOR_DEBUG="${PURPLE}"
usage() { # {{{ usage() { # {{{
cat <<- EOF cat <<- EOF
usage: $PROGNAME [-d|-f|-h|-r] usage: $PROGNAME [-d|-f|-h|-r|-s]
Backup /etc/pve content. Backup /etc/pve content.
@ -38,11 +41,14 @@ EXAMPLES:
${PROGNAME} ${PROGNAME}
- Backup /etc/pve content to /var/backups/pve directory - Backup /etc/pve content to /var/backups/pve directory
${PROGNAME} --first /var/backups/pve ${PROGNAME} --first-directory /var/backups/pve
- Backup to default path and keep backups for 14 days - Backup to default path and keep backups for 14 days
${PROGNAME} --retention 14 ${PROGNAME} --retention 14
- Duplicate backups to a second directory (/mnt/nfs/pve)
${PROGNAME} --second-directory /mnt/nfs/pve
OPTIONS: OPTIONS:
-d,--debug -d,--debug
Enable debug messages. Enable debug messages.
@ -58,6 +64,9 @@ OPTIONS:
Backups older than retention time (default: ${DEFAULT_RETENTION_TIME}) Backups older than retention time (default: ${DEFAULT_RETENTION_TIME})
will be delete. will be delete.
-s,--second,--second-directory
Path to a second directory to duplicate backups (default: not set).
EOF EOF
} }
@ -124,10 +133,10 @@ main() { # {{{
&& printf '%b\n' "${RED}/etc/pve directory doesn't seems available. Are you sure you run this script on a Proxmox host?${RESET}" \ && printf '%b\n' "${RED}/etc/pve directory doesn't seems available. Are you sure you run this script on a Proxmox host?${RESET}" \
&& exit 1 && exit 1
## }}} ## }}}
## Verify if the local destination directory is absent {{{ ## Verify if the first destination directory is absent {{{
### AND create it ### AND create it
is_directory_absent "${first_bkp_dir}" \ is_directory_absent "${first_bkp_dir}" \
&& mkdir -p -- ${first_bkp_dir} && mkdir -p -- "${first_bkp_dir}"
## }}} ## }}}
## Create an archive of /etc/pve to $first_bkp_dir {{{ ## Create an archive of /etc/pve to $first_bkp_dir {{{
@ -151,6 +160,20 @@ main() { # {{{
|| exit 4 || exit 4
## }}} ## }}}
## If second directory is defined {{{
if [ -n "${second_bkp_dir}" ]; then
### Verify if the second destination directory is absent {{{
#### AND create it
is_directory_absent "${second_bkp_dir}" \
&& mkdir -p -- "${second_bkp_dir}"
### }}}
### Synchronize first directory to second {{{
#### OR exit with error code 12 if it fails
rsync -a -- "${first_bkp_dir}/" "${second_bkp_dir}/" \
|| exit 12
### }}}
fi
## }}}
} }
# }}} # }}}
@ -179,7 +202,7 @@ if [ ! "${NBARGS}" -eq "0" ]; then
-d|--debug ) ## debug -d|--debug ) ## debug
DEBUG=0 DEBUG=0
;; ;;
-f|--first,--first-directory ) ## first directory to store backup -f|--first|--first-directory ) ## first directory to store backup
## Move to the next argument ## Move to the next argument
shift shift
## Define first_bkp_dir ## Define first_bkp_dir
@ -196,6 +219,12 @@ if [ ! "${NBARGS}" -eq "0" ]; then
## Define retention_time ## Define retention_time
retention_time="${1}" retention_time="${1}"
;; ;;
-s|--second|--second-directory ) ## second directory to duplicate backup
## Move to the next argument
shift
## Define second_bkp_dir
second_bkp_dir="${1}"
;;
* ) ## unknow option * ) ## unknow option
printf '%b\n' "${RED}Invalid option: ${1}${RESET}" printf '%b\n' "${RED}Invalid option: ${1}${RESET}"
printf '%b\n' "---" printf '%b\n' "---"