You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
90 lines
2.8 KiB
90 lines
2.8 KiB
2 years ago
|
#!/bin/bash
|
||
|
|
||
|
#//// GREG POUR LE GARAGE NUMERIQUE \\\\#
|
||
|
|
||
|
# Script automatisant le push de conteneur vers un registre privé de backups ainsi que les volumes correspondants.
|
||
|
# Le docker login au registre doit être effectué au moins une fois manuellement.
|
||
|
# Configurer l'adresse du registre ainsi que les informations sur le serveur de backups
|
||
|
# Les volumes et labels sont archivés et transférés par ssh vers le serveur de backup une fois par mois (le 1er).
|
||
|
|
||
|
############################## CONFIG ##############################
|
||
|
|
||
|
date=`date +%d-%m`
|
||
|
# dockerregistry="monregistre.mondomaine.com"
|
||
|
dockerregistry=""
|
||
|
# sshHost="user@server-IP"
|
||
|
sshHost=""
|
||
|
# sshPort=9999
|
||
|
sshPort=
|
||
|
backupFolder="~/docker-volumes-backup/"
|
||
|
# exceptions=( "container1" "container2")
|
||
|
#exceptions=( "" )
|
||
|
|
||
|
############################## CONTAINERS BACKUP ##############################
|
||
|
|
||
|
# LISTING DES CONTAINERS
|
||
|
stack=$(docker ps -aq)
|
||
|
|
||
|
for container in $stack
|
||
|
do
|
||
|
echo "backup du conteneur $container"
|
||
|
# GET CONTAINER NAME
|
||
|
docker inspect --format='{{.Name}}' $container > containers.txt
|
||
|
containerName=$(sed 's/[/]*//' containers.txt)
|
||
|
|
||
|
for exception in ${exceptions[@]}
|
||
|
do
|
||
|
if [ $container == $exception ]
|
||
|
then
|
||
|
break
|
||
|
else
|
||
|
# COMMIT AND PUSH TO REGSTRY
|
||
|
docker container commit $container $dockerregistry$containerName-backup:$date
|
||
|
docker push $dockerregistry$containerName-backup:$date
|
||
|
fi
|
||
|
done
|
||
|
done
|
||
|
|
||
|
# CLEANING
|
||
|
rm -rf containers.txt
|
||
|
|
||
|
############################## VOLUMES & LABELS BACKUP ##############################
|
||
|
|
||
|
if [ ${date:0:02} == "01" ]
|
||
|
then
|
||
|
# LISTING DES VOLUMES
|
||
|
volumes=$(docker volume ls -q)
|
||
|
|
||
|
for volume in $volumes
|
||
|
do
|
||
|
echo "backup du volume $volume"
|
||
|
|
||
|
# GET CONTAINER ID
|
||
|
containerId=$(docker ps -a -q --filter volume=$volume)
|
||
|
# GET CONTAINER NAME
|
||
|
docker inspect --format='{{.Name}}' $containerId > volumes.txt
|
||
|
container=$(sed 's/[/]*//' volumes.txt)
|
||
|
|
||
|
# STOP THE CONTAINER (FOR VOLUMES INTEGRITY)
|
||
|
docker stop $container
|
||
|
# BACKUP VOLUME IN ARCHIVE
|
||
|
docker run -v $volume:/volume -v $(pwd)/backup:/backup --rm loomchild/volume-backup backup volumes/$container-volume:$date
|
||
|
# BACKUP DOCKER VOLUMES LABELS
|
||
|
docker inspect $volume -f "{{json .Labels}}" > backup/labels/$container-labels.json
|
||
|
# RESTART THE CONTAINER
|
||
|
docker start $container
|
||
|
|
||
|
# CLEANING
|
||
|
rm -rf volumes.txt
|
||
|
done
|
||
|
|
||
|
# MV VOLUMES & LABELS BACKUP TO BACKUP HOST
|
||
|
scp -r -P $sshPort $(pwd)/backup/* $sshHost:$backupFolder
|
||
|
# CLEANING
|
||
|
rm -rf containers.txt; rm -rf $(pwd)/backup/volumes/*; rm -rf $(pwd)/backup/labels/*
|
||
|
|
||
|
echo "copie des archives de volumes vers le serveur de backup ok"
|
||
|
fi
|
||
|
|
||
|
echo "backup des containers terminée"
|