Mysql database regular full database backup

The following script for scheduled backups of the whole database mysql

mysql_dump_script.sh

 

. 1  # ! / Bin / the bash 
2  
. 3  # save the backup number, up to a 4 files 
. 4 Number =. 4
 . 5  # save the backup path 
. 6 BACKUP_DIR = / DB / backup_mysql
 . 7  # date 
. 8 dd =% `DATE Y-m +% -% H-D-%%% M- S '
 . 9  # backup tool 
10 tool = the mysqldump
 . 11  # username 
12 is username = the root
 13 is  # password 
14 password yourpassword =
 15  # database to be backed up 
16 database_name = mydb
 . 17  
18 is  #If the folder does not exist, create 
19  IF [-d! $ BACKUP_DIR ]; 
 20  the then     
 21      mkdir -p $ BACKUP_DIR ; 
 22  fi
 23  
24-  # simple wording mysqldump -u root -p123456 users> / root / mysqlbackup / users- $ filename .sql 
25  $ Tool -h127.0.0.1 -u $ username -p $ password  $ database_name > $ BACKUP_DIR / $ database_name - $ dd .sql
 26  
27  # to write to create a backup log 
28 echo " the create BACKUP_DIR $ / $ $ database_name- dd.dupm" >> $ BACKUP_DIR / log.txt
 29  
30  # identify the need to remove the backup 
31 is the DelFile -l-CRT LS =`   $ BACKUP_DIR /*.sql | awk '{Print $. 9 }' | head -1 `
 32  
33 is  # Analyzing whether it is now greater than the number of backup $ number 
34 is COUNT = `-l-CRT LS   $ BACKUP_DIR /*.sql | awk '{Print $. 9 }' | WC - l`
 35  
36  iF [ $ COUNT  -gt  $ number ]
 37 [  the then
 38 is    # delete the oldest backup generation, leaving only the number of backup number 
39    RM $ delfile 
40   # Write log files to delete 
41    echo " the Delete $ delfile " >> $ BACKUP_DIR / log.txt
 42 fi

 

 

centos set crontab

 

crontabs install yum
systemctl enable crond (to boot)
systemctl Start crond (start crond service)
systemctl Status crond (View Status)

 

vi /etc/crontab

Add regular tasks

 

 

 

Load task, bring it into force:

crontab /etc/crontab

 

View tasks:

crontab -l

 

Guess you like

Origin www.cnblogs.com/DevinZhang1990/p/12162383.html