LINUX environment MySQL scheduled backup script

(0) backup destination
backup: tel database
directory: / mysqlbackup /
strategy: 3:00 daily

(1) Create a backup of the user, the user name is not backup password backup
MySQL> Grant All ON tel. To 'backup' @ 'localhost' IDENTIFIED by "backup";
provide less permissions:
MySQL> Grant the SELECT, Show View, Lock . the Tables, tel ON the Trigger
to 'Backup' @ 'localhost' IDENTIFIED by "Backup";

(2) to write the backup script
shell> vim mysql_backup.sh
#! / Bin / SH
### defined variables ####
DATE = date +%Y%m%d
ago10date = date -d"10 day ago" +"%Y%m%d"
backuser = Backup
DB_HOST = localhost
backupdir = / mysqlbackup

### determines whether there is storage directory ####
IF [! -D $ the backupdir]
the then
mkdir -p $ the backupdir
Fi
### determines whether there is a second stage storage directory ####
IF [! -D $ the backupdir / DATE $]
the then
mkdir -p the backupdir $ / $ DATE
Fi

########## execute backup command #############
/ usr / bin / the mysqldump -u -H $ $ backuser DB_HOST -p'backup '--single-Transaction tel> "$ backupdir / $ date / tel.sql"

Delete the backup ######### ########## 10 days ago
IF [-d $ backupdir / $ ago10date]
the then
RM -rf $ backupdir / $ ago10date
fi

(3) add execute permission
shell> chmod + x mysql_backup.sh

(4) adding the timer task
shell> crontab -l

  • 3 * /usr/bin/sh /mysqlbackup/mysql_backup.sh

Guess you like

Origin blog.51cto.com/7603402/2438175