Advertisement
Guest User

Untitled

a guest
Aug 5th, 2016
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.68 KB | None | 0 0
  1. #!/bin/bash
  2. # Author: Boris Guéry <guery.b@gmail.com>
  3.  
  4. usage() {
  5. cat <<EOF
  6. usage: $0 <command> [-h] [-y] [-vvv] [-d] [<args>]
  7. Currently supported commands are:
  8. push Push current database data to S3
  9. pull Download latest database snapshot and remove existing one
  10. EOF
  11. }
  12.  
  13. return_with_error() {
  14. echo "fatal:" $@ >&2
  15. exit 1
  16. }
  17.  
  18. run() {
  19. if [[ "$VERBOSE" -ge 2 ]]; then
  20. echo "->" $@
  21. fi
  22.  
  23. if [[ "$DRY_RUN" -eq 0 ]]; then
  24. if [[ "$VERBOSE" -ge 2 ]]; then
  25. $@
  26. else
  27. $@ > /dev/null 2>&1
  28. fi
  29. if [[ "$?" -ne 0 ]]; then
  30. if [[ "$VERBOSE" -lt 2 ]]; then
  31. return_with_error "an error occured, run with -vv to debug"
  32. else
  33. exit 1
  34. fi
  35. fi
  36. fi
  37. }
  38.  
  39. log() {
  40. if [[ "${VERBOSE}" -ge 1 ]]; then
  41. echo "->" $@
  42. fi
  43. }
  44.  
  45. parse_options() {
  46. while getopts "hvdy" OPTION
  47. do
  48. case ${OPTION} in
  49. h)
  50. usage
  51. exit 1
  52. ;;
  53. v)
  54. VERBOSE=$((VERBOSE+1))
  55. ;;
  56. d)
  57. DRY_RUN=1
  58. ;;
  59. y)
  60. ASSUME_YES=1
  61. ;;
  62. esac
  63. done
  64. }
  65.  
  66. print_version() {
  67. local version="${VERSION}"
  68. if [[ ${REVISION} != '$rev$' ]]; then
  69. version="${version}r${REVISION}"
  70. fi
  71.  
  72. echo "version: ${version}"
  73. }
  74.  
  75. confirm() {
  76. if [[ ${ASSUME_YES} -eq 0 ]]; then
  77. read -r -p "-? $1" response
  78. case ${response} in
  79. $2)
  80. true
  81. ;;
  82. *)
  83. false
  84. ;;
  85. esac
  86. else
  87. true
  88. fi
  89. }
  90.  
  91. exit_if_not_root() {
  92. [ "$UID" -ne 0 ] && return_with_error "you must be root"
  93. }
  94.  
  95. VERBOSE=0
  96. DRY_RUN=0
  97. ASSUME_YES=0
  98. VERSION="0.1"
  99. REVISION='$rev$'
  100. DATABASE_REVISION="latest"
  101. DATABASE_SNAPSHOT_S3_URI="s3://productreview-au-dev/mysql-backup/database-dev-${DATABASE_REVISION}.tar.gz"
  102. DATABASE_SERVER_USER="root"
  103. DATABASE_SERVER_PASSWORD=""
  104.  
  105. push() {
  106. local temp_dir=$(mktemp -d)
  107. log Created temp directory: ${temp_dir}
  108. log Creating backup
  109. local innobackupex_output=$(innobackupex --user=${DATABASE_SERVER_USER} --password=${DATABASE_SERVER_PASSWORD} --compress --compress-threads=4 --no-timestamp ${temp_dir} 2>&1)
  110. if [[ $? -ne 0 || ${innobackupex_output} != *"completed OK!"* ]]; then
  111. if [[ ${VERBOSE} -le 1 ]]; then
  112. return_with_error "innobackupex errored, run with -vv to debug"
  113. else
  114. return_with_error "innobackupex errored: ${innobackupex_output}"
  115. fi
  116. fi
  117.  
  118. run cd ${temp_dir}
  119. temp_archive_name=$(mktemp)
  120. run tar -cz . -f ${temp_archive_name}
  121. log Archive created ${temp_archive_name}
  122.  
  123. log Pushing archive to S3
  124. run aws s3 cp ${temp_archive_name} ${DATABASE_SNAPSHOT_S3_URI}
  125.  
  126. log Cleaning temp files and directories
  127. run rm -fr ${temp_dir}
  128. run rm -f ${temp_archive_name}
  129. }
  130.  
  131. pull() {
  132. local temp_dir=$(mktemp -d)
  133. log Created temp directory: ${temp_dir}
  134. local temp_downloaded_archive_name=$(mktemp)
  135. log Downloading archive...
  136. run aws s3 cp ${DATABASE_SNAPSHOT_S3_URI} ${temp_downloaded_archive_name}
  137. log Extracting archive...
  138. run tar -C ${temp_dir} -xzf ${temp_downloaded_archive_name}
  139. run cd ${temp_dir}
  140. log Decompressing backup
  141. run innobackupex --decompress .
  142. log Preparing backup
  143. run innobackupex --apply-log .
  144. log Stopping MySQL services
  145. run service mysql stop
  146. confirm "Are you TOTALLY sure, it will delete all the existing databases? [y/N] " "[yY]" || { echo "Bye chicken."; exit 0; }
  147. local existing_datadir_backup_dir=$(mktemp -d)
  148. run mkdir -p /var/lib/mysql/
  149. run mv /var/lib/mysql/* ${existing_datadir_backup_dir}
  150. log I created a backup of the existing datadir in ${existing_datadir_backup_dir}
  151. run find /var/lib/mysql/ -mindepth 1 -delete
  152. run innobackupex --copy-back ${temp_dir}
  153. run chown -R mysql: /var/lib/mysql
  154. log Restarting MySQL services
  155. run service mysql start
  156. confirm "If everything ran properly, you may want to remove existing backup, do you? [y/N] " "[yY]" && { run rm -fr ${existing_datadir_backup_dir}; }
  157. run rm -fr ${temp_dir}
  158. run rm -f ${temp_downloaded_archive_name}
  159. }
  160.  
  161. case $1 in
  162. push)
  163. exit_if_not_root
  164. shift
  165. parse_options $@
  166. shift $((OPTIND-1)); OPTIND=1
  167. push $@
  168. exit 0
  169. ;;
  170. pull)
  171. exit_if_not_root
  172. shift
  173. parse_options $@
  174. shift $((OPTIND-1)); OPTIND=1
  175. pull $@
  176. exit 0
  177. ;;
  178.  
  179. usage)
  180. usage
  181. exit 0
  182. ;;
  183. --version)
  184. print_version
  185. exit 0
  186. ;;
  187. *)
  188. usage
  189. exit 1
  190. ;;
  191. esac
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement