Centos automatically backs up mysql

Source: Internet
Author: User

1. Create a backup folder [plain] view plaincopy

  1. Cd/mnt/cp_disk/
  2. Mkdirbackup
  3. Cdbackup
  4. Mkdirdatabak
  5. Mkdirdb
  6. Mkdirlogs
2. Compile the execution script [plain] view plaincopy
  1. #! /Bin/bash
  2. Backup_dir =/mnt/cp_disk/backup/databak # backup temporary file storage directory
  3. Backup_target_dir =/mnt/cp_disk/backup/db # backup file storage directory
  4. Backup_logs_dir =/mnt/cp_disk/backup/logs # backup log directory
  5. Db = gzcpc
  6. DATE = $ (date + % Y % m % d)
  7. # Get the date 10 days ago
  8. CcDATE = $ (date "-d10dayago" + % Y % m % d)
  9. Echo "Start copying data tables" >>$ backup_logs_dir/$ db $ DATE
  10. Echo "----------- 'date +" % Y-% m-% d % H: % M: % S "'------------------" >>$ backup_logs_dir/$ db $ date
  11. Cp-R/var/lib/mysql/$ db $ backup_dir/# The data directory of the mysql database is/var/lib/mysql/
  12. Echo "start compressing data tables" >>$ backup_logs_dir/$ db $ DATE
  13. Echo "------------------------"> $ backup_logs_dir/$ db $ DATE
  14. Cd $ backup_dir
  15. Tar-zcvf1_db1_date.tar.gz $ db/& echo "compressed table end" >>$ backup_logs_dir/$ db $ DATE
  16. Mv1_db1_date.tar.gz $ backup_target_dir/& echo "transfer successful" >>$ backup_logs_dir/$ db $ DATE
  17. If [$? -Eq0]
  18. Then
  19. Echo "backupsucceed" >>$ backup_logs_dir/$ db $ DATE
  20. Else
  21. Echo "backupfail" >>$ backup_logs_dir/$ db $ DATE
  22. Fi
  23. Echo "start to delete the original data table" >>$ backup_logs_dir/$ db $ DATE
  24. Echo "-------------------------"> $ backup_logs_dir/$ db $ DATE
  25. Rm-rf $ backup_dir/* & echo "delete original data table"> $ backup_logs_dir/$ db $ DATE
  26. Echo "delete data 10 days ago" >>$ backup_logs_dir/$ db $ DATE
  27. If [-e $ backup_target_dir/db1_ccdate.tar.gz]
  28. Then
  29. Rm-rf $ backup_target_dir/db1_ccdate.tar.gz
  30. Echo "Delete $ backup_target_dir/db1_ccdate.tar.gz succeed"> $ backup_logs_dir/$ db $ DATE
  31. Else
  32. Echo "Notfound $ backup_target_dir/db1_ccdate.tar.gz file" >>$ backup_logs_dir/$ db $ DATE
  33. Fi
  34. If [-e $ backup_logs_dir/$ db $ ccDATE]
  35. Then
  36. Rm-rf $ backup_logs_dir/$ db $ ccDATE
  37. Echo "Delete $ backup_logs_dir/$ db $ ccDATEsucceed" >>$ backup_logs_dir/$ db $ DATE
  38. Else
  39. Echo "Notfound $ backup_logs_dir/$ db $ ccDATEfile" >>$ backup_logs_dir/$ db $ DATE
  40. Fi

3. Grant the script permission to [plain] view plaincopy
  1. Chmod755/home/backup_mysql.sh

4. Create a scheduled task [plain] view plaincopy
  1. Crontab-e
  2. 10 ***/bin/bash/home/backup_mysql.sh

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.