Centos automatically backs up mysql Databases every day

Source: Internet
Author: User

The script is as follows:
#! /Bin/bash
# Function Description: This function is used to back up a database.
# Prepared on: 2010/12/06
 
PATH =/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
Export PATH
# Database username
Dbuser = 'root'
# Database Password
 
 
Dbpasswd = '000000'
# Database name. You can define multiple databases separated by spaces, for example, test test1 test2.
Dbname = 'test1 test2'
# Backup time
Backtime = 'date + % Y % m % d % H % M % s'
# Log backup path
Logpath = '/second/backup'
# Data backup path
Datapath = '/second/backup'
# Log record header
Echo '"backup time is $ {backtime}, backup database table $ {dbname} starts" >$ {logpath}/mysqllog. log
# Formally back up the database
For table in $ dbname; do
Source = 'mysqldump-u $ {dbuser}-p $ {dbpasswd }$ {table }>$ {logpath}/$ {backtime }. SQL '2 >>$ {logpath}/mysqllog. log;
# Successful backup:
If ["$? "= 0]; then
Cd $ datapath
# Compressing the database to save hard disk space
Tar jcf zookeeper table1_zookeeper backtime=.tar.bz2 $ {backtime}. SQL>/dev/null
# Delete the original file, leaving only the compressed file
Rm-f $ {datapath}/$ {backtime}. SQL
 
Echo "database table $ {dbname} is backed up successfully !! ">>$ {Logpath}/mysqllog. log
Else
# If the backup fails, perform the following operations:
Echo "database table $ {dbname} backup failed !! ">>$ {Logpath}/mysqllog. log
Fi
 
Done
Scheduled execution script:
Method:
1,
Execute crontab-e
Enter the following content:
______________________________________________________________________________
00 00 ***/bin/bash yourpath/mysqlbak. sh
2,
Open the automatic execution File
Vi/etc/crontab
Add the following content to etc to automatically execute the task.
00 00 *** root/mysqlbak. sh
The above two 00 *** scripts are automatically executed every morning.
Hour, day, month, and week commands

M: minute (0-59 ). Each minute is represented by * or */1.

H: hour (0-23 ). (0 indicates 0)

D: Day (1-31 ).

M: Month (1-12 ).

D: days in a week (0 ~ 6, 0 is Sunday ).
3,
Redhat method:
The crontab of Redhat calls four directories by time (/etc/cron. hourly: hourly;/etc/cron. daily: Every
Days;/etc/cron. weekly: weekly;/etc/cron. monthly: monthly.
In Redhat, you only need to copy the edited script to the corresponding directory.
Cp/autobackupmysql. sh etc/cron. daily
Restart etc
/Etc/rc. d/init. d/crond restart

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.