Last active
June 14, 2023 15:18
-
-
Save akira345/1a5f9f677b0c9c1116dd293c12982ba4 to your computer and use it in GitHub Desktop.
Docker-Composeで作成したコンテナのボリューム、DBをバックアップするシェルスクリプト。DBのコンテナ名は命名規則を適用する前提
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#/bin/bash | |
# | |
# Cronで動かすときはPATHを設定すること。 | |
# 例:)PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin | |
get_contenner_service_name(){ | |
echo $1 | cut -d"_" -f2- | rev | cut -c 3- | rev | |
} | |
mysql_contenner_backup(){ | |
target_contenner=$1 | |
mysql_contenner_service_name=`get_contenner_service_name ${target_contenner}` | |
mysql_dump_name=${mysql_contenner_service_name}_dumpall.sql | |
echo "MySQLバックアップ開始:${mysql_contenner_service_name}" | |
docker exec -i ${target_contenner} /bin/sh -c 'exec mysqldump --all-databases -uroot -p"$MYSQL_ROOT_PASSWORD"' > ${backup_dir}/${mysql_dump_name} | |
if [ $? -ne 0 -o ! -e ${backup_dir}/${mysql_dump_name} -o ! -s ${backup_dir}/${mysql_dump_name} ]; then | |
echo "mysqlバックアップに失敗:${mysql_contenner_service_name}" | |
exit 1 | |
fi | |
echo "MySQLバックアップ終了:${mysql_contenner_service_name}" | |
} | |
pgsql_contenner_backup(){ | |
target_contenner=$1 | |
pgsql_contenner_service_name=`get_contenner_service_name ${target_contenner}` | |
pgsql_dump_name=${pgsql_contenner_service_name}_dumpall.sql | |
echo "PostgreSQLバックアップ開始:${pgsql_contenner_service_name}" | |
docker exec -i ${target_contenner} /bin/sh -c 'pg_dumpall -U $POSTGRES_USER' > ${backup_dir}/${pgsql_dump_name} | |
if [ $? -ne 0 -o ! -e ${backup_dir}/${pgsql_dump_name} -o ! -s ${backup_dir}/${pgsql_dump_name} ]; then | |
echo "postgresqlバックアップに失敗:${pgsql_contenner_service_name}" | |
exit 1 | |
fi | |
echo "PostgreSQLバックアップ終了:${pgsql_contenner_service_name}" | |
} | |
contenner_backup(){ | |
target_contenner=$1 | |
target_contenner_service_name=`get_contenner_service_name ${target_contenner}` | |
echo "Dockerコンテナ終了:${target_contenner_service_name}" | |
docker-compose stop ${target_contenner_service_name} | |
if [ $? -ne 0 ]; then | |
echo "Dockerコンテナ終了に失敗:${target_contenner_service_name}" | |
exit 1 | |
fi | |
docker run --rm --volumes-from ${target_contenner} -v ${backup_dir}:/backup -e TAR_OPTS="--verbose" akira345/docker-backup backup ${target_contenner}.tar.xz | |
if [ $? -ne 0 ]; then | |
echo "Dockerコンテナバックアップに失敗:${target_contenner_serivce_name}" | |
exit 1 | |
fi | |
echo "Dockerコンテナ起動:${target_contenner_service_name}" | |
docker-compose start ${target_contenner_service_name} | |
} | |
### Main | |
target_contenners=$(docker-compose ps | tail -n +3 | cut -d " " -f1) | |
backup_dir=$(pwd)/backup | |
mkdir -p ${backup_dir} | |
echo ${target_contenners} | |
echo "start : `date +'%Y/%m/%d %H:%M:%S'`" | |
if [ -n "${target_contenners}" ]; then | |
echo "バックアップ開始" | |
for target_contenner in ${target_contenners}; | |
do | |
#PostgreSQL、MySQLコンテナ名に一致するように修正すること | |
if [[ ${target_contenner} =~ pgsql|postgresql ]]; then | |
pgsql_contenner_backup ${target_contenner} | |
elif [[ ${target_contenner} =~ mysql ]]; then | |
mysql_contenner_backup ${target_contenner} | |
else | |
contenner_backup ${target_contenner} | |
fi | |
done | |
echo "END" | |
fi | |
echo "end : `date +'%Y/%m/%d %H:%M:%S'`" | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment