Skip to content

Instantly share code, notes, and snippets.

@kwonghung-YIP
Created October 15, 2019 03:26
Show Gist options
  • Save kwonghung-YIP/a72e358c0c0a1c89cba0eb9ce4018046 to your computer and use it in GitHub Desktop.
Save kwonghung-YIP/a72e358c0c0a1c89cba0eb9ce4018046 to your computer and use it in GitHub Desktop.
Shell script for export all secrets defined in docker swarm
#!/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
@abalhomaid
Copy link

Thank you for creating this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment