s4ros

backup_mysql_docker

Oct 31st, 2020
843
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2.  
  3. if [[ -z $2 ]]; then
  4.     cat <<- EOT
  5. Usage: $0 [dst_dir] <container_name>
  6.  
  7. dst_dir - destination dir, where to save backups
  8.  
  9. container_name - name of the mysql docker container
  10.  
  11. EOT
  12. fi
  13.  
  14. BASEDIR=$(dirname $0)
  15. DEST_DIR=${1:-${BASEDIR}}
  16. CONTAINER_NAME=$2
  17. TIMESTAMP=$(date "+%s")
  18. DB_NAME=$(docker exec ${CONTAINER_NAME} env | grep MYSQL_DATABASE | cut -d '=' -f 2)
  19.  
  20. # dump command chain
  21. docker exec ${CONTAINER_NAME} \
  22.     bash -c "mysqldump -u root --password=\${MYSQL_ROOT_PASSWORD} \${MYSQL_DATABASE}" 2> /dev/null | \
  23.     bzip2 -c -9 > ${DEST_DIR}/${CONTAINER_NAME%%_*}_${DB_NAME}_${TIMESTAMP}.sql.bz2
RAW Paste Data