centos6.5 mysql scheduled backup

Edit the sh file and write a shell script to back up the database
    [root@localhost /]# vi mysql-backup.sh
    #!bin/sh
    db_user="username"  
    db_passwd="password"  
    db_host="address"  
    db_name="database name"  
    # the directory for story your backup file.  
    backup_dir="/backup"  
    # date format for backup file (dd-mm-yyyy)  
    time="$(date +"%Y-%m-%d-%H-%M-%S")"  
    # mysql, mysqldump and some other bin's path  
    MYSQL="/usr/local/mysql/bin/mysql"  
    MYSQLDUMP="/usr/local/mysql/bin/mysqldump"  
    GZIP="/bin/gzip"  
      
    $MYSQLDUMP -u $db_user -h $db_host -p$db_passwd $db_name | $GZIP -9 > "$backup_dir/$db_name"_"$time.gz"  

 

 

1. First chmod 700 MySQL -backup.sh // Only allow administrators to run this script

2. ./mysql-backup.sh executes the shell and sees no errors

3. Create a directory

mkdir  backup

4. crontab –e

5. Pop up a vi editor to join

# 00 03 * * * /mysql-backup.sh // Execute every day at 3 am

6. crontab –ls to see if the task has been added

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326772616&siteId=291194637