2021-04-12 15:20:00 +02:00
|
|
|
|
#!/bin/sh
|
|
|
|
|
|
|
|
|
|
# This script will backup /etc/pve content :
|
2021-04-12 19:06:47 +02:00
|
|
|
|
# 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
|
2021-04-12 19:37:17 +02:00
|
|
|
|
# Easy to monitor (eg. this path can be expected).
|
|
|
|
|
# 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,…).
|
|
|
|
|
#
|
2021-04-12 15:20:00 +02:00
|
|
|
|
|
|
|
|
|
# This script can be call by a cronjob (eg. daily).
|
|
|
|
|
|
|
|
|
|
# Vars {{{
|
|
|
|
|
readonly PROGNAME=$(basename "${0}")
|
|
|
|
|
readonly PROGDIR=$(readlink -m $(dirname "${0}"))
|
|
|
|
|
readonly ARGS="${*}"
|
|
|
|
|
readonly NBARGS="${#}"
|
|
|
|
|
[ -z "${DEBUG}" ] && DEBUG=1
|
|
|
|
|
|
2021-04-12 19:16:03 +02:00
|
|
|
|
readonly DEFAULT_FIRST_BKP_DIR="/etc/proxmox.pve/backup"
|
2021-04-12 16:18:34 +02:00
|
|
|
|
readonly TODAY_VAR=$(date +%Y%m%d)
|
2021-04-12 19:06:47 +02:00
|
|
|
|
readonly DEFAULT_RETENTION_TIME="7"
|
2021-04-12 15:20:00 +02:00
|
|
|
|
|
|
|
|
|
## Colors
|
|
|
|
|
readonly PURPLE='\033[1;35m'
|
|
|
|
|
readonly RED='\033[0;31m'
|
|
|
|
|
readonly RESET='\033[0m'
|
|
|
|
|
readonly COLOR_DEBUG="${PURPLE}"
|
|
|
|
|
# }}}
|
|
|
|
|
|
|
|
|
|
usage() { # {{{
|
|
|
|
|
|
|
|
|
|
cat <<- EOF
|
2021-04-12 19:37:17 +02:00
|
|
|
|
usage: $PROGNAME [-d|-f|-h|-r|-s]
|
2021-04-12 15:20:00 +02:00
|
|
|
|
|
2021-04-12 19:16:03 +02:00
|
|
|
|
Backup /etc/pve content.
|
2021-04-12 15:20:00 +02:00
|
|
|
|
|
|
|
|
|
EXAMPLES :
|
2021-04-12 19:16:03 +02:00
|
|
|
|
- Backup /etc/pve content to ${DEFAULT_FIRST_BKP_DIR} directory
|
2021-04-12 15:20:00 +02:00
|
|
|
|
${PROGNAME}
|
|
|
|
|
|
2021-04-12 19:06:47 +02:00
|
|
|
|
- Backup /etc/pve content to /var/backups/pve directory
|
2021-04-12 19:37:17 +02:00
|
|
|
|
${PROGNAME} --first-directory /var/backups/pve
|
2021-04-12 19:06:47 +02:00
|
|
|
|
|
|
|
|
|
- Backup to default path and keep backups for 14 days
|
|
|
|
|
${PROGNAME} --retention 14
|
|
|
|
|
|
2021-04-12 19:37:17 +02:00
|
|
|
|
- Duplicate backups to a second directory (/mnt/nfs/pve)
|
|
|
|
|
${PROGNAME} --second-directory /mnt/nfs/pve
|
|
|
|
|
|
2021-04-12 15:20:00 +02:00
|
|
|
|
OPTIONS :
|
|
|
|
|
-d,--debug
|
|
|
|
|
Enable debug messages.
|
|
|
|
|
|
2021-04-12 19:16:03 +02:00
|
|
|
|
-f,--first,--first-directory
|
|
|
|
|
Path to a first directory to store backup
|
|
|
|
|
And override default path ${DEFAULT_FIRST_BKP_DIR}.
|
|
|
|
|
|
2021-04-12 15:20:00 +02:00
|
|
|
|
-h,--help
|
|
|
|
|
Print this help message.
|
2021-04-12 15:41:18 +02:00
|
|
|
|
|
2021-04-12 19:06:47 +02:00
|
|
|
|
-r,--retention,--retention-time
|
|
|
|
|
Backups older than retention time (default: ${DEFAULT_RETENTION_TIME})
|
|
|
|
|
will be delete.
|
|
|
|
|
|
2021-04-12 19:37:17 +02:00
|
|
|
|
-s,--second,--second-directory
|
|
|
|
|
Path to a second directory to duplicate backups (default: not set).
|
|
|
|
|
|
2021-04-12 15:20:00 +02:00
|
|
|
|
EOF
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
# }}}
|
|
|
|
|
debug_message() { # {{{
|
|
|
|
|
|
|
|
|
|
local_message="${1}"
|
|
|
|
|
|
|
|
|
|
## Print message if DEBUG is enable (=0)
|
|
|
|
|
[ "${DEBUG}" -eq "0" ] && printf '\e[1;35m%-6b\e[m\n' "DEBUG − ${PROGNAME} : ${local_message}"
|
|
|
|
|
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
# }}}
|
|
|
|
|
define_vars() { # {{{
|
|
|
|
|
|
2021-04-12 19:16:03 +02:00
|
|
|
|
## If first_bkp_dir wasn't defined {{{
|
|
|
|
|
if [ -z "${first_bkp_dir}" ]; then
|
2021-04-12 15:41:18 +02:00
|
|
|
|
## Use default path to store backup
|
2021-04-12 19:16:03 +02:00
|
|
|
|
first_bkp_dir="${DEFAULT_FIRST_BKP_DIR}"
|
2021-04-12 15:20:00 +02:00
|
|
|
|
fi
|
|
|
|
|
## }}}
|
2021-04-12 19:06:47 +02:00
|
|
|
|
## If retention_time wasn't defined {{{
|
|
|
|
|
if [ -z "${retention_time}" ]; then
|
|
|
|
|
## Use default retention time to clean backups
|
|
|
|
|
retention_time="${DEFAULT_RETENTION_TIME}"
|
|
|
|
|
fi
|
|
|
|
|
## }}}
|
2021-04-12 15:20:00 +02:00
|
|
|
|
|
2021-04-12 15:41:18 +02:00
|
|
|
|
}
|
|
|
|
|
# }}}
|
|
|
|
|
is_directory_absent() { # {{{
|
|
|
|
|
|
|
|
|
|
local_directory_absent="${1}"
|
|
|
|
|
|
|
|
|
|
## Directory exists by default
|
|
|
|
|
return_is_directory_absent="1"
|
|
|
|
|
|
|
|
|
|
### Check if the directory exists
|
|
|
|
|
# shellcheck disable=SC2086
|
2021-04-12 15:54:21 +02:00
|
|
|
|
if test -d "${local_directory_absent}"; then
|
2021-04-12 15:41:18 +02:00
|
|
|
|
return_is_directory_absent="1"
|
|
|
|
|
debug_message "is_directory_absent − \
|
|
|
|
|
The directory ${RED}${local_directory_absent}${COLOR_DEBUG} exists."
|
|
|
|
|
else
|
|
|
|
|
return_is_directory_absent="0"
|
|
|
|
|
debug_message "is_directory_absent − \
|
|
|
|
|
The directory ${RED}${local_directory_absent}${COLOR_DEBUG} doesn't exist."
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
return "${return_is_directory_absent}"
|
|
|
|
|
|
2021-04-12 15:20:00 +02:00
|
|
|
|
}
|
|
|
|
|
# }}}
|
|
|
|
|
main() { # {{{
|
|
|
|
|
|
|
|
|
|
## Define all vars
|
|
|
|
|
define_vars
|
|
|
|
|
|
2021-04-12 16:19:03 +02:00
|
|
|
|
## Verify if /etc/pve directory is absent {{{
|
|
|
|
|
### Display an explicit error message
|
|
|
|
|
### AND exit with error code 1
|
|
|
|
|
is_directory_absent /etc/pve \
|
|
|
|
|
&& 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
|
|
|
|
|
## }}}
|
2021-04-12 19:37:17 +02:00
|
|
|
|
## Verify if the first destination directory is absent {{{
|
2021-04-12 15:41:18 +02:00
|
|
|
|
### AND create it
|
2021-04-12 19:16:03 +02:00
|
|
|
|
is_directory_absent "${first_bkp_dir}" \
|
2021-04-12 19:37:17 +02:00
|
|
|
|
&& mkdir -p -- "${first_bkp_dir}"
|
2021-04-12 15:41:18 +02:00
|
|
|
|
## }}}
|
|
|
|
|
|
2021-04-12 19:16:03 +02:00
|
|
|
|
## Create an archive of /etc/pve to $first_bkp_dir {{{
|
2021-04-12 16:18:34 +02:00
|
|
|
|
### OR exit with error code 2 if it fails
|
2021-04-12 19:16:03 +02:00
|
|
|
|
tar czf "${first_bkp_dir}/pve.${TODAY_VAR}.tar.gz" -C /etc/ pve/ \
|
2021-04-12 16:18:34 +02:00
|
|
|
|
|| exit 2
|
|
|
|
|
## }}}
|
|
|
|
|
## Create an hard link to pve.latest.tar.gz {{{
|
|
|
|
|
### OR exit with error code 3 if it fails
|
2021-04-12 19:16:03 +02:00
|
|
|
|
ln --force -- "${first_bkp_dir}/pve.${TODAY_VAR}.tar.gz" "${first_bkp_dir}/pve.latest.tar.gz" \
|
2021-04-12 16:18:34 +02:00
|
|
|
|
|| exit 3
|
|
|
|
|
## }}}
|
2021-04-12 16:45:18 +02:00
|
|
|
|
## Fix backups permissions {{{
|
|
|
|
|
### Only readable by backup (user) and adm (group)
|
2021-04-12 19:16:03 +02:00
|
|
|
|
chown -R backup:adm -- "${first_bkp_dir}" \
|
|
|
|
|
&& chmod 'u+rwX,g+rX,o-rwx' -R -- "${first_bkp_dir}"
|
2021-04-12 16:45:18 +02:00
|
|
|
|
## }}}
|
2021-04-12 19:06:47 +02:00
|
|
|
|
## Clean files older than $retention_time {{{
|
|
|
|
|
### OR exit with error code 4 if it fails
|
2021-04-12 19:16:03 +02:00
|
|
|
|
find "${first_bkp_dir}" -maxdepth 1 -type f -mtime +"${retention_time}" -iname "pve.*.tar.gz" -delete \
|
2021-04-12 19:06:47 +02:00
|
|
|
|
|| exit 4
|
|
|
|
|
## }}}
|
2021-04-12 16:18:34 +02:00
|
|
|
|
|
2021-04-12 19:37:17 +02:00
|
|
|
|
## 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
|
|
|
|
|
## }}}
|
2021-04-12 15:20:00 +02:00
|
|
|
|
}
|
|
|
|
|
# }}}
|
|
|
|
|
|
|
|
|
|
# Manage arguments # {{{
|
|
|
|
|
# This code can't be in a function due to argument management
|
|
|
|
|
|
|
|
|
|
if [ ! "${NBARGS}" -eq "0" ]; then
|
|
|
|
|
|
|
|
|
|
manage_arg="0"
|
|
|
|
|
|
|
|
|
|
## If the first argument is not an option
|
|
|
|
|
if ! printf -- '%s' "${1}" | grep -q -E -- "^-+";
|
|
|
|
|
then
|
|
|
|
|
## Print help message and exit
|
|
|
|
|
printf '%b\n' "${RED}Invalid option: ${1}${RESET}"
|
|
|
|
|
printf '%b\n' "---"
|
|
|
|
|
usage
|
|
|
|
|
|
|
|
|
|
exit 1
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
# Parse all options (start with a "-") one by one
|
|
|
|
|
while printf -- '%s' "${1}" | grep -q -E -- "^-+"; do
|
|
|
|
|
|
|
|
|
|
case "${1}" in
|
|
|
|
|
-d|--debug ) ## debug
|
|
|
|
|
DEBUG=0
|
|
|
|
|
;;
|
2021-04-12 19:37:17 +02:00
|
|
|
|
-f|--first|--first-directory ) ## first directory to store backup
|
2021-04-12 19:16:03 +02:00
|
|
|
|
## Move to the next argument
|
|
|
|
|
shift
|
|
|
|
|
## Define first_bkp_dir
|
|
|
|
|
first_bkp_dir="${1}"
|
|
|
|
|
;;
|
2021-04-12 15:20:00 +02:00
|
|
|
|
-h|--help ) ## help
|
|
|
|
|
usage
|
|
|
|
|
## Exit after help informations
|
|
|
|
|
exit 0
|
|
|
|
|
;;
|
2021-04-12 19:06:47 +02:00
|
|
|
|
-r|--retention,--retention-time ) ## clean backups older than retention time
|
|
|
|
|
## Move to the next argument
|
|
|
|
|
shift
|
|
|
|
|
## Define retention_time
|
|
|
|
|
retention_time="${1}"
|
|
|
|
|
;;
|
2021-04-12 19:37:17 +02:00
|
|
|
|
-s|--second|--second-directory ) ## second directory to duplicate backup
|
|
|
|
|
## Move to the next argument
|
|
|
|
|
shift
|
|
|
|
|
## Define second_bkp_dir
|
|
|
|
|
second_bkp_dir="${1}"
|
|
|
|
|
;;
|
2021-04-12 15:20:00 +02:00
|
|
|
|
* ) ## unknow option
|
|
|
|
|
printf '%b\n' "${RED}Invalid option: ${1}${RESET}"
|
|
|
|
|
printf '%b\n' "---"
|
|
|
|
|
usage
|
|
|
|
|
exit 1
|
|
|
|
|
;;
|
|
|
|
|
esac
|
|
|
|
|
|
|
|
|
|
debug_message "Arguments management − \
|
|
|
|
|
${RED}${1}${COLOR_DEBUG} option managed."
|
|
|
|
|
|
|
|
|
|
## Move to the next argument
|
|
|
|
|
shift
|
|
|
|
|
manage_arg=$((manage_arg+1))
|
|
|
|
|
|
|
|
|
|
done
|
|
|
|
|
|
|
|
|
|
debug_message "Arguments management − \
|
|
|
|
|
${RED}${manage_arg}${COLOR_DEBUG} argument(s) successfully managed."
|
|
|
|
|
else
|
|
|
|
|
debug_message "Arguments management − \
|
|
|
|
|
No arguments/options to manage."
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
# }}}
|
|
|
|
|
|
|
|
|
|
main
|
|
|
|
|
|
|
|
|
|
exit 255
|