Backup two databases CF_DB and CF_TEST_DB
Very simple, there are three steps:
1: Create a new directory in the system: /usr/local/BACKUP_SQL
2: Add the file backup_db.sh
The contents of the file are as follows:
#!/bin/bash
# 一小时备份一次
/usr/local/Mysql/mysql/bin/mysqldump -u root -p'数据库密码' CF_DB > /usr/local/BACKUP_SQL/CF_DB/CF_DB_$(date +%Y%m%d_%H%M%S).sql
/usr/local/Mysql/mysql/bin/mysqldump -u root -p'数据库密码' CF_LOG_DB > /usr/local/BACKUP_SQL/CF_LOG_DB/CF_LOG_DB_$(date +%Y%m%d_%H%M%S).sql
# find /usr/local/BACKUP_SQL/CF_DB -mtime +1 -name "*.sql*" //查找两天前的,所有名字为.sql的文件
# find /usr/local/BACKUP_SQL/CF_DB -name "*.sql*" //查找所有名字为.sql的文件
# 删除两天前的备份文件
# 防止系统故障,备份文件全删完了,文件数量必须大于10个才删除
filenum=`find /usr/local/BACKUP_SQL/CF_DB -name "*.sql*" | wc -l`
if [ $filenum -gt 10 ] ; then
filenum=`find /usr/local/BACKUP_SQL/CF_DB -mtime +1 -name "*.sql*" -exec rm -f {} \;`
fi
filenum2=`find /usr/local/BACKUP_SQL/CF_LOG_DB -name "*.sql*" | wc -l`
if [ $filenum2 -gt 10 ] ; then
filenum2=`find /usr/local/BACKUP_SQL/CF_LOG_DB -mtime +1 -name "*.sql*" -exec rm -f {} \;`
fi
If you don’t know the location of mysqldump, look it up globally, command: whereis mysqldump
find /usr/local/BACKUP_SQL/CF_DB -mtime +1 -name "*.**"————The command is parsed as follows:
find: Linux search command, the user searches for files with specified conditions
/usr/local/BACKUP_SQL/CF_DB: Any directory you want to clean up
-mtime : standard sentence writing
+1 0: Find files 10 days ago, where numbers represent the number of days, and +30 means find files 30 days ago
"*.*": The type of data you want to find, "*.jpg" means to find all files with the extension jpg, "*" means to find all files, this can be used flexibly, by analogy
-exec: fixed writing
rm -rf: forcibly delete files, including directories
{} \;: fixed writing, a pair of braces + space + \
3: Add Linux timing tasks to the backup machine: enter the command: crontab -e
0 */2 * * * sh /usr/local/BACKUP_SQL/backup_db.sh
Save and exit: ESC: wq
It's over, you're done
For example, it is 2019-08-15 17:48, the backup file in your system will be as follows: