[Blog recommendations] use shell scripts to automate Database Backup

Source: Internet
Author: User

[Blog recommendations] use shell scripts to automate Database Backup

This blog post is from the Bkjia blogger Lemon. If you have any questions, go to the blog page for an interactive discussion!

Blog: http://maomaostyle.blog.51cto.com/2220531/1654994

Because personal website construction cannot guarantee high availability, and data may be easily lost, the need to back up the database is necessary. So I found some information on the Internet, based on my actual situation, you can use a script to back up the database at every day and regularly Delete the database backup files seven days ago every Sunday. Below, I will post my backup database script for your learning and reference.

First, this is the script for backing up the database.

 
 
  1. Cat/usr/local/script/BackupDatabase
  2. #! /Bin/bash
  3. # Shell Command For Backup MySQL Database Everyday Automatically By Crontab
  4. # Time 2015-5-20
  5. # Name huxianglin
  6. USER = root
  7. PASSWORD = xxxxxxxx
  8. DATABASE1 = zblog
  9. DATABASE2 = zabbix
  10. BACKUP_DIR =/data/backup/database/# path of the backup database file
  11. LOGFILE =/data/backup/database/data_backup.log # back up the log file of the database script
  12. DATE = 'date + % Y % m % d-% H % M-d-3minute '# Get the current system time-3 minutes
  13. DUMPFILE1 = $ DATE-zblog. SQL # Name of the database to be backed up
  14. DUMPFILE2 = $ DATE-zabbix. SQL
  15. Archive1=dumpfile1-tar.gz # Name of the backup database after compression
  16. Archive2w.dumpfile2-tar.gz
  17. If [! -D $ BACKUP_DIR]; # determines whether the backup path exists. If not, this path is created.
  18. Then
  19. Mkdir-p "$ BACKUP_DIR"
  20. Fi
  21. Echo-e "\ n" >>$ LOGFILE
  22. Echo "------------------------------------"> $ LOGFILE
  23. Echo "backup date: $ DATE" >>$ LOGFILE
  24. Echo "------------------------------------"> $ LOGFILE
  25. Cd $ BACKUP_DIR # Jump to the backup path
  26. /Usr/local/mysql/bin/mysqldump-u $ USER-p $ PASSWORD $ DATABASE1> $ DUMPFILE1 # Use mysqldump to back up the database
  27. If [[$? = 0]; then
  28. Tar czvf $ ARCHIVE1 $ DUMPFILE1 >>$ LOGFILE 2> & 1 # determine whether the backup is successful. If the backup is successful, compress the backup database; otherwise, write the error log to the log file.
  29. Echo "$ ARCHIVE1 backup successful! ">>> $ LOGFILE
  30. Rm-f $ DUMPFILE1
  31. Else
  32. Echo "$ ARCHIVE1 Backup Fail !" >>$ LOGFILE
  33. Fi
  34. /Usr/local/mysql/bin/mysqldump-u $ USER-p $ PASSWORD $ DATABASE2> $ DUMPFILE2
  35. If [[$? = 0]; then
  36. Tar czvf $ ARCHIVE2 $ DUMPFILE2 >>$ LOGFILE 2> & 1
  37. Echo "$ ARCHIVE2 backup successful! ">>> $ LOGFILE
  38. Rm-f $ DUMPFILE2
  39. Else
  40. Echo "$ ARCHIVE2 Backup Fail !" >>$ LOGFILE
  41. Fi

Then, write the script to delete the backup file.

 
 
  1. [Root @ localhost database] # cat/usr/local/script/CleanDatabase
  2. #! /Bin/bash
  3. # Time 2015-05-
  4. # Name huxianglin
  5. BACKUPDIR = "/data/backup/database/" # define the backup file path
  6. KEEPTIME = 7 # define the number of days before the file to be deleted
  7. DELFILE = 'Find $ BACKUPDIR-type f-mtime + $ KEEPTIME-exec ls {}\; '# find the file whose number of days is greater than 7 days
  8. For delfile in $ {DELFILE} # Delete objects that meet the condition that the number of days is greater than seven days
  9. Do
  10. Rm-f $ delfile
  11. Done

Finally, you need to write the time for automatic script execution in crontab.

 
 
  1. Cat/etc/crontab
  2. SHELL =/bin/bash
  3. PATH =/sbin:/bin:/usr/sbin:/usr/bin
  4. MAILTO = root
  5. # For details see man 4 crontabs
  6. # Example of job definition:
  7. #. ---------------- Minute (0-59)
  8. # |. ----------- Hour (0-23)
  9. # |. ---------- Day of month (1-31)
  10. # |. ------- Month (1-12) OR jan, feb, mar, apr...
  11. # |. ---- Day of week (0-6) (Sunday = 0 or 7) OR sun, mon, tue, wed, thu, fri, sat
  12. # |
  13. # ***** User-name command to be executed
  14. 01 00 *** root/usr/local/script/BackupDatabase # define to execute the backup database script at 00:01 every day
  15. 02 00 ** 0 root/usr/local/script/CleanDatabase # define to delete database backup files at 00:02 every Sunday

The above is my script for backing up the database and regularly deleting the database. I hope you can give your comments and suggestions.
 

Related Article

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.