Guest User

Untitled

a guest
Jan 26th, 2019
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # Do non-blocking MySQL DB backups. For InnoDB, the backup user does not need the LOCK_TABLES privilege.
  4.  
  5. excludeDB="'information_schema', 'performance_schema'" # Which DBs NOT to backup
  6.  
  7. # User needs the SELECT privilege, optionally EVENTS to avoid a warning
  8. # introduced in MySQL 5.5 when dumping the DB "mysql" (Warning: Skipping the data of table mysql.event.)
  9. # CREATE USER 'backup'@'localhost' IDENTIFIED BY 'PASSWORD';
  10. # GRANT LOCK TABLES, SELECT, EVENT ON *.* TO 'backup'@'localhost' IDENTIFIED BY 'PASSWORD';
  11.  
  12. user="backup"
  13. pass="PASSWORD" # Well..
  14. path="/backups/mysql" # Where to write the backups?
  15. keep="30" # How long to keep old backups around, in days.
  16. optParam="--events" # Optional parameters for mysqldump. Hint: Specify --events if the user has the privilege
  17.  
  18. # Get database names, exclude those defined earlier.
  19. databases=`mysql -u $user -p$pass -B -N -e "select schema_name from information_schema.schemata where schema_name not in ($excludeDB)"`
  20.  
  21. # Backup every database to a separate file.
  22. for curDB in $databases; do
  23. mysqldump --single-transaction --opt $optParam --databases "$curDB" -u$user -p$pass \
  24. | gzip -3 --rsyncable > "$path/$curDB-$(date +%FT%H%M%S).sql.gz"
  25. done
  26.  
  27. if [ ${PIPESTATUS[0]} = "0" ];then
  28. :
  29. else
  30. echo "Error, could not backup MySQL databases"
  31. exit 1
  32. fi
  33.  
  34. # Delete old backups
  35. find $path -mtime +$keep -exec rm {} \;
Add Comment
Please, Sign In to add comment