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