当前位置:网站首页>Linux scheduled database backup script

Linux scheduled database backup script

2022-07-23 14:52:00 Gerald pick Xie

inux Scheduled database backup script

Create script

  • Create script ( There are notes in the document )
vim dbName.sh 
  • vim Use
    i Insert
    : Enter the command
#!/bin/bash
 
# Backup directory 
BACKUP=/home/mysqlBackup/db_name
# Get the current time 
DATATIME=$(date +'%Y-%m-%d_%H%M%S')
# Print time 
echo $DATATIME
# Database address 
HOST=localhost
# Database user name 
DB_USER=root
# Database password 
DB_PW=123456
# Backup database name 
DATABASE=db_name

# Create backup directory , If it doesn't exist, create it 
[ ! -d "${BACKUP}/${DATATIME}/${DATATIME}" ] && mkdir -p "${BACKUP}/${DATATIME}"

# Backup database 
mysqldump -u${
    DB_USER} -p${
    DB_PW}  --host=${
    HOST} -q -R --databases ${
    DATABASE} | gzip > ${
    BACKUP}/${
    DATETIME}/$DATABASE.sql.gz

# Process files into tar.gz
cd ${
    BACKUP}
tar -zcvf $DATABASE.tar.gz ${
    DATATIME}
# Delete the corresponding backup directory 
rm -rf ${
    BACKUP}/${
    DATATIME}

# Delete 10 Days ago backup files 
find ${
    BACKUP} -atime +30 -name "+.tar.gz" -exec rm -rf {
    } \;
echo " Backup database ${DATABASE}  success ~"

Set the timing

  • Enter the command crontab -e / Path address
1. Expressed as a number   Time information 
00 02 *  *  *   Backup file 

2. Use special symbols to represent time information 
*     *    *   *    *    Backup file 
/ minute  / Hours  / God   / month   / Zhou 

=========================================
PS: The shortest execution cycle of a scheduled task is   Every minute  
*/5       */ 6     */3      */1    */2
 every other 5 minute    every other 6 Hours    every other 3 Japan    every other 1 month    every other 2 Zhou 

=========================================
 Other   How to write it :
01-05  02  * * *         Every time 2 Japanese 0102030405  Do it again 


 Specify a discontinuous time range :
00  14,20  *  *  *  *     Every day 14 spot ,20 Click to execute once 
原网站

版权声明
本文为[Gerald pick Xie]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/204/202207230928588754.html