2019-10-17 15:26:31 +02:00
#!/bin/sh
# Purpose {{{
## Backup data of a Docker container.
## To do that, the script will :
2019-10-17 18:49:03 +02:00
## Try to stop the corresponding systemd unit (or docker if no related service is found)
2019-10-18 19:20:52 +02:00
## Make a archive of the container's data to a backup path
2019-10-18 21:04:08 +02:00
## Restart the container service (or docker.service)
2019-10-18 19:20:52 +02:00
## TODO : Clean old backup
2019-10-17 15:26:31 +02:00
# }}}
# How-to use {{{
2019-10-17 18:49:03 +02:00
## First argument should be the absolut path to the container's data.
## The data directory and the container should have the same name.
2019-10-17 15:26:31 +02:00
# }}}
# Vars {{{
DEBUG=0
2019-10-17 18:49:03 +02:00
## Manage arguments {{{
## Expect at least 1 argument and 2 maximum
2019-10-17 15:26:31 +02:00
case $# in
1 )
## Absolut path to the container data
2019-10-17 18:49:03 +02:00
ct_data_path="${1}"
2019-10-17 15:26:31 +02:00
## Absolut path to store the archives.
## A sub-directory will be created for the container
docker_backup_path="/mnt/backup/docker"
;;
2 )
2019-10-17 18:49:03 +02:00
ct_data_path="${1}"
docker_backup_path="${2}"
2019-10-17 15:26:31 +02:00
;;
* )
cat << HELP
container.backup --
Backup data of a given Docker container.
EXAMPLE :
2019-10-17 18:49:03 +02:00
- 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
2019-10-17 15:26:31 +02:00
HELP
exit 1
;;
esac
2019-10-17 18:49:03 +02:00
ct_name=$(basename -- "${ct_data_path}")
2019-10-18 19:20:52 +02:00
docker_data_path=$(dirname -- "${ct_data_path}")
ct_backup_path="${docker_backup_path}/${ct_name}"
2019-10-17 18:49:03 +02:00
## }}}
2019-10-17 15:26:31 +02:00
# }}}
2019-10-17 18:49:03 +02:00
# 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} ."
2019-10-17 15:49:42 +02:00
exit 1
2019-10-17 18:49:03 +02:00
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} ."
2019-10-17 15:49:42 +02:00
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
2019-10-17 18:49:03 +02:00
else
[ "${DEBUG}" -eq "0" ] && printf '\e[1;35m%-6s\e[m\n' "DEBUG : Test arg — Path to store the backup : ${docker_backup_path} ."
2019-10-17 15:49:42 +02:00
fi
## }}}
2019-10-17 15:26:31 +02:00
# }}}
2019-10-17 16:15:47 +02:00
# Get container service name {{{
ct_service_name=$(find /etc/systemd -type f -iname "*${ct_name}*.service" -printf "%f\n")
## If no specific systemd service exists, the docker.service will be used
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
2019-10-17 18:49:03 +02:00
[ "${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} ."
2019-10-17 16:15:47 +02:00
fi
# }}}
2019-10-17 18:49:58 +02:00
# Stop the container service {{{
systemctl stop "${ct_service_name}"
2019-10-18 19:26:42 +02:00
[ "${DEBUG}" -eq "0" ] && printf '\e[1;35m%-6s\e[m\n' "DEBUG : Service management — Service ${ct_service_name} was stopped."
2019-10-17 18:49:58 +02:00
# }}}
2019-10-18 19:20:52 +02:00
# Backup container data {{{
## Create the directory to store archive if doesn't already exist
2019-10-18 19:26:42 +02:00
if [ ! -d "${ct_backup_path}" ]; then
mkdir -p -- "${ct_backup_path}"
[ "${DEBUG}" -eq "0" ] && printf '\e[1;35m%-6s\e[m\n' "DEBUG : Backup data — ${ct_backup_path} was created."
fi
2019-10-18 19:20:52 +02:00
## Backup container data
tar czf "${ct_backup_path}/${ct_name}-backup.$(date '+%Y%m%d-%H%M').tar.gz" --directory "${docker_data_path}" "${ct_name}"
2019-10-18 19:26:42 +02:00
[ "${DEBUG}" -eq "0" ] && printf '\e[1;35m%-6s\e[m\n' "DEBUG : Backup data — Data of ${ct_name} container were successfully backuped ${ct_backup_path}/${ct_name}-backup.$(date '+%Y%m%d-%H%M').tar.gz"
2019-10-18 19:20:52 +02:00
# }}}
2019-10-18 21:04:08 +02:00
# Restart the container service {{{
systemctl start "${ct_service_name}"
[ "${DEBUG}" -eq "0" ] && printf '\e[1;35m%-6s\e[m\n' "DEBUG : Service management — Service ${ct_service_name} was started."
# }}}
2019-10-17 16:15:47 +02:00
2019-10-17 15:26:31 +02:00
exit 0