WIP: Take full path to container data as first arg

The maximum number of args is now : 2
This commit is contained in:
Jeremy Gardais 2019-10-17 18:49:03 +02:00
parent 379e29c5b7
commit 917bf83c59
Signed by: jegardai
GPG Key ID: E759BAA22501AF32
1 changed files with 26 additions and 34 deletions

View File

@ -3,41 +3,32 @@
# Purpose {{{
## Backup data of a Docker container.
## To do that, the script will:
## Try to stop the corresponding container (or docker if no related service is found)
## Try to stop the corresponding systemd unit (or docker if no related service is found)
## Make a archive of the container's data to a backup path
## Clean old backup
## Restart the container service (or docker.service)
# }}}
# How-to use {{{
## The container and it's data directory should have the same name.
## First argument should be the absolut path to the container's data.
## The data directory and the container should have the same name.
# }}}
# Vars {{{
DEBUG=0
# }}}
# Tests {{{
## Expect at least 1 argument and 3 maximum {{{
## Manage arguments {{{
## Expect at least 1 argument and 2 maximum
case $# in
1 )
ct_name="${1}"
## Absolut path to the container data
docker_data_path="/srv/docker"
ct_data_path="${1}"
## Absolut path to store the archives.
## A sub-directory will be created for the container
docker_backup_path="/mnt/backup/docker"
;;
2 )
ct_name="${1}"
docker_data_path="${2}"
docker_backup_path="/mnt/backup/docker"
;;
3 )
ct_name="${1}"
docker_data_path="${2}"
docker_backup_path="${3}"
ct_data_path="${1}"
docker_backup_path="${2}"
;;
* )
cat << HELP
@ -46,8 +37,10 @@ container.backup --
Backup data of a given Docker container.
EXAMPLE:
- Backup data of FIRST_DOCK container
container.backup FIRST_DOCK
- Backup data of FIRST_CT container require the absolut path to data
container.backup /srv/docker/FIRST_CT
- Select the directory to store the backup of hello_world container
container.backup /srv/docker/hello_world /media/usb/docker.backup
HELP
@ -55,30 +48,29 @@ HELP
;;
esac
[ "${DEBUG}" -eq "0" ] && printf '\e[1;35m%-6s\e[m\n' "DEBUG: Args management — Container name to backup: ${ct_name} ."
[ "${DEBUG}" -eq "0" ] && printf '\e[1;35m%-6s\e[m\n' "DEBUG: Args management — Path to Docker data: ${docker_data_path} ."
[ "${DEBUG}" -eq "0" ] && printf '\e[1;35m%-6s\e[m\n' "DEBUG: Args management — Path to store the backup: ${docker_backup_path} ."
ct_name=$(basename -- "${ct_data_path}")
## }}}
# }}}
## Ensure Docker data directory exists {{{
if [ ! -d "${docker_data_path}" ]; then
printf '\e[0;31m%-6s\e[m\n' "Docker data directory doesn't seems available: ${docker_data_path} ."
# Tests {{{
## Ensure container data directory exists {{{
if [ ! -d "${ct_data_path}" ]; then
printf '\e[0;31m%-6s\e[m\n' "Docker data directory doesn't seems available: ${ct_data_path} ."
exit 1
else
[ "${DEBUG}" -eq "0" ] && printf '\e[1;35m%-6s\e[m\n' "DEBUG: Test arg — Path to Docker data: ${ct_data_path} ."
[ "${DEBUG}" -eq "0" ] && printf '\e[1;35m%-6s\e[m\n' "DEBUG: Test arg — Container name to backup: ${ct_name} ."
fi
## }}}
## Ensure backup directory exists {{{
if [ ! -d "${docker_backup_path}" ]; then
printf '\e[0;31m%-6s\e[m\n' "Backup directory doesn't seems available: ${docker_backup_path} ."
exit 1
else
[ "${DEBUG}" -eq "0" ] && printf '\e[1;35m%-6s\e[m\n' "DEBUG: Test arg — Path to store the backup: ${docker_backup_path} ."
fi
## }}}
## Ensure container data directory exists {{{
if [ ! -d "${docker_data_path}/${ct_name}" ]; then
printf '\e[0;31m%-6s\e[m\n' "Container directory doesn't seems available: ${docker_data_path}/${ct_name} ."
exit 1
fi
## }}}
# }}}
# Get container service name {{{
@ -89,7 +81,7 @@ if [ "${ct_service_name}" = "" ]; then
ct_service_name="docker.service"
[ "${DEBUG}" -eq "0" ] && printf '\e[1;35m%-6s\e[m\n' "DEBUG: Service name — No specific service found for ${ct_name} container, the ${ct_service_name} will be stopped."
else
[ "${DEBUG}" -eq "0" ] && printf '\e[1;35m%-6s\e[m\n' "DEBUG: Service name — Service name of the container to backup: ${ct_service_name} ."
[ "${DEBUG}" -eq "0" ] && printf '\e[1;35m%-6s\e[m\n' "DEBUG: Service name — Service name of ${ct_name} container to stop: ${ct_service_name} ."
fi
# }}}