Advertisement
Guest User

Untitled

a guest
May 17th, 2017
592
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.51 KB | None | 0 0
  1. #!/bin/bash
  2. # -------------------------------------------------------------------------
  3. # Script to generate mysqldump from wordpress running inside a container
  4. #
  5. # Copyright (C) 2016 Aline Freitas <aline@alinefreitas.com.br>
  6. #
  7. # It has two command line arguments:
  8. # -c: the docker container name
  9. # -d: the html volume in your host pointing to the root of your wordpress installation.
  10. # It needs to be where exist the wp-config.php file.
  11. #
  12. # Usage:
  13. # wp-mysqldump.sh -c <container> -d <docker html path>
  14. # YOu may need also to set the following variables
  15.  
  16. # Where you want to save your backups. They will be created in a subdir with the same name
  17. # as the container.
  18. BACKUP_DIR="/root/mysqlbackups"
  19.  
  20. # The generated extension for the backup file.
  21. BACKUP_EXTENSION="*.mysqldump"
  22.  
  23. # The maximum amount of files to keep. If you dont want to delete any of then just comment
  24. # out this line.
  25. BACKUP_MAX_FILES=30
  26.  
  27. usage() {
  28. echo "Usage: $0 -c <container> -d <docker html path>"
  29. echo "Example: $0 -c www-mariadb -d /data/www.lambda3.com.br/html"
  30. }
  31.  
  32. while getopts ":c:d:" opt; do
  33. case "$opt" in
  34. c)
  35. CONTAINER=$OPTARG
  36. ;;
  37. d)
  38. DOCKER_HTML_PATH=$OPTARG
  39. ;;
  40. *)
  41. echo 'Invalid argument.'
  42. usage
  43. exit 1
  44. ;;
  45. esac
  46. done
  47.  
  48. if [ $# -eq 0 ]; then
  49. usage
  50. exit 1
  51. fi
  52.  
  53. if [ -z ${CONTAINER} ] || [ -z ${DOCKER_HTML_PATH} ]; then
  54. echo "Missing parameters."
  55. usage
  56. exit 1
  57. fi
  58.  
  59. if ! docker ps -q -f name=$CONTAINER &>/dev/null
  60. then
  61. echo "Docker container not running..."
  62. exit 1
  63. fi
  64.  
  65.  
  66. wp_get_value() {
  67. local wp_config_path="$DOCKER_HTML_PATH/wp-config.php"
  68. if [ -r $wp_config_path ]; then
  69. echo $(sed -n "/^ *define( *'$1', *'\([^']*\)'.*/ {s//\1/p;q;}" $wp_config_path)
  70. else
  71. echo "Missing wp-config.php."
  72. return 1
  73. fi
  74. }
  75.  
  76. backup_generate() {
  77. test -d ${BACKUP_DIR}/${CONTAINER} || mkdir ${BACKUP_DIR}/${CONTAINER}
  78. docker exec $CONTAINER /usr/bin/mysqldump --user=$(wp_get_value DB_USER) --password=$(wp_get_value DB_PASSWORD) $(wp_get_value DB_NAME) > ${BACKUP_DIR}/${CONTAINER}/$CONTAINER-$(date +"%Y%m%d-%H%M").sqldump
  79. }
  80.  
  81. remove_older() {
  82. filenum=$(ls -1 $BACKUP_DIR/$CONTAINER | grep $BACKUP_EXTENSION | wc -l)
  83. if [ $filenum -ge $BACKUP_MAX_FILES ]; then
  84. rm $(ls -1t $BACKUP_DIR/$CONTAINER | tail -n+$(($BACKUP_MAX_FILES++)))
  85. fi
  86. }
  87.  
  88. backup_generate
  89. if [ $? -ne 0 ]; then
  90. echo "Error generating backup."
  91. exit 1
  92. fi
  93.  
  94. if [ -z $BACKUP_MAX_FILES ]; then
  95. remove_older
  96. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement