backup-docker/m2v.sh

50 lines
1.0 KiB
Bash
Executable File

#!/bin/bash
set -o xtrace
if [[ $# < 2 ]]; then
echo "First argument is compose prefix. File with list of 'volumes binddir' needed as second argument"
exit 1
fi
if [[ ! -f "${2}" ]]; then
echo "File with list of 'volumes binddir' missing (${2})."
exit 1
fi
if [[ ! -d "../@${1}" ]]; then
echo "Compose directory not valid (${1})"
exit 1
fi
sudo docker rm -f helper
if [[ -f log.txt ]]; then
mv log.txt "log-$(date +%N).txt"
else
touch log.txt
fi
VOLUMES="$(sudo docker volume ls -q |grep ${1}_)a"
touch log.txt
sleep 2
while read -r F1 F2; do
VOLUME="${1}_${F1}"
if grep -wq "${VOLUME}" - <<< $VOLUMES; then
COMMAND="rm -rvf /volume/{*,.*} && cp -a ../bindmnt/. /volume"
BINDMNT=$(realpath "../cndata/${F2}")
sudo docker run --name helper \
-v "${VOLUME}:/volume" \
-v "${BINDMNT}:/bindmnt" \
busybox \
sh -c "${COMMAND}"
sudo docker logs helper >> log.txt
sudo docker rm -f helper
sleep 2
else
echo "$VOLUME is not available for restoration"
exit 1
fi
done < "${2}"
set +o xtrace