1、mysql数据库备份脚本

  1. #!/bin/bash
  2. username="root"
  3. password="GDnxFd@123#!"
  4. backupToBasePath=/data1/mysqldump_databases_backup/
  5. if [ ! -d ${backupToBasePath} ];then (mkdir ${backupToBasePath}) fi
  6. retentionDays=30
  7. databases=(information_schema
  8. am
  9. cm
  10. hue
  11. metastore
  12. mysql
  13. nav_as
  14. nav_ms
  15. oozie
  16. performance_schema
  17. rm
  18. sentry
  19. sys
  20. t_demo
  21. t_resource
  22. t_tempo)
  23. datetime=`date +%Y_%m_%d`
  24. for db in ${databases[@]};do
  25. if [ ! -d ${backupToBasePath}/${db} ];then (mkdir ${backupToBasePath}/${db}) fi
  26. echo "mysqldump -u${username} -p${password} ${db} -d > ${backupToBasePath}/${db}/${datetime}_structure_${db}.sql"
  27. (mysqldump -u${username} -p${password} ${db} -d > ${backupToBasePath}/${db}/${datetime}_structure_${db}.sql)
  28. echo "mysqldump -u${username} -p${password} ${db} -t > ${backupToBasePath}/${db}/${datetime}_data_${db}.sql"
  29. (mysqldump -u${username} -p${password} ${db} -t > ${backupToBasePath}/${db}/${datetime}_data_${db}.sql)
  30. done
  31. echo "find ${backupToBasePath} -mtime +${retentionDays} -name \"*.sql\" -exec rm -rf {} \;"
  32. (find ${backupToBasePath} -mtime +${retentionDays} -name "*.sql" -exec rm -rf {} \;)

2、cron表达式

2.1 每天23点执行一次

  1. crontab -l
  2. crontab -e
  3. 0 23 * * * sh mysqldump_databases_backup.sh &
  4. :wq