Created
October 15, 2019 03:26
-
-
Save kwonghung-YIP/a72e358c0c0a1c89cba0eb9ce4018046 to your computer and use it in GitHub Desktop.
Shell script for export all secrets defined in docker swarm
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 | |
service_name="backup-all-secrets" | |
secret_list=( `docker secret ls --format "{{ .Name }}"` ) | |
cmd="docker service create \ | |
--name $service_name \ | |
--constraint node.hostname==`hostname` " | |
for secret in "${secret_list[@]}" | |
do | |
cmd=$cmd" --secret ${secret}" | |
done | |
cmd=$cmd" nginx" | |
echo $cmd | |
$cmd | |
tar_file="backup_`date +\"%Y%m%d-%H%M%S\"`.tar" | |
container_id=`docker ps -f "label=com.docker.swarm.service.name=$service_name" -q` | |
echo $tar_file | |
echo $container_id | |
docker exec -w /usr/local $container_id sh -c "tar -cvf $tar_file -C /run/secrets ." | |
docker cp $container_id:/usr/local/$tar_file . | |
docker service rm $service_name |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you for creating this