Advertisement
Guest User

Untitled

a guest
Feb 24th, 2016
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. #!/bin/bash
  2. #
  3. # MySQL Backup Script
  4. # Dumps mysql databases to a file for another backup tool to pick up.
  5. #
  6. # MySQL code:
  7. # GRANT SELECT, RELOAD, LOCK TABLES ON *.* TO 'user'@'localhost'
  8. # IDENTIFIED BY 'password';
  9. # FLUSH PRIVILEGES;
  10. #
  11. ##### START CONFIG ###################################################
  12.  
  13. USER=backup
  14. DIR=/var/lib/mysql/backup
  15. ROTATE=6
  16.  
  17. # Use a pwd file. See https://dev.mysql.com/doc/refman/5.1/en/password-security-user.html
  18. PWD_FILE=$(mktemp ${DIR}/backup.cnf.XXXX)
  19. echo "
  20. [client]
  21. password=sirugrwiuwiuwguisrkogos
  22. " >> $PWD_FILE
  23.  
  24. PREFIX=mysql_backup_
  25.  
  26. ADDITIONAL_OPTIONS="--ignore-table=mysql.event"
  27.  
  28.  
  29. ##### STOP CONFIG ####################################################
  30. PATH=/usr/bin:/usr/sbin:/bin:/sbin
  31.  
  32.  
  33.  
  34. set -o pipefail
  35.  
  36. cleanup()
  37. {
  38. find "${DIR}/" -maxdepth 1 -type f -name "${PREFIX}*.sql*" -mtime +${ROTATE} -print0 | xargs -0 -r rm -f
  39. }
  40.  
  41. mysqldump --defaults-file=${PWD_FILE} -u${USER} --opt --flush-logs --single-transaction
  42. ${ADDITIONAL_OPTIONS}
  43. --all-databases | bzcat -zc > ${DIR}/${PREFIX}`date +%Y%m%d-%H%M%S`.sql.bz2
  44.  
  45. if [ $? -eq 0 ] ; then
  46. cleanup
  47. fi
  48.  
  49.  
  50. rm -rf $PWD_FILE
  51.  
  52. # time /usr/local/sbin/mysqlbackup.sh
  53.  
  54. real 21m35.647s
  55. user 20m47.059s
  56. sys 0m7.843s
  57.  
  58. # du -sh mysql_backup_20160223-123920.sql
  59. 4.9G mysql_backup_20160223-123920.sql
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement