Write a script to automate full and incremental backups
Full backup
vim /root/allbak.sh#!/bin/bash[ ! -e /backup ]&& mkdir /backup //新建个文件夹专门放备份文件的day=`date +%F` //定义日期是时间user=rootpass=123456innobackupex --user $user --password $pass /backup/allbak${day} --no-timestamp //用innobackupex做完全备份mysql -u$user -p$pass -e "flush logs" ``` //启用binlog日志,每次完全备份之后,刷新一遍binlog日志
vim/root/newbak.sh
#!/bin/bash
d1= date +%f
//define time, use date to differentiate
d2= date +%f-d "-1 days"
//Find yesterday's date to indicate the last backup file
Dir1=/backup/allbak${d2}//Full backup file made yesterday
Dir2=/backup/newbak${d2}//Incremental backup file made yesterday
User=root
pass=123456
[!-e/backup]&& Mkdir/backup//If the file does not exist, create a folder
If [-e ${dir1}];then//Judging yesterday Do is full backup
Innobackupex--user $user--password $pass--incremental/backup/newbak${d1}--incremental-basedir=${dir1} --no-timestamp//Specify the full backup file that was backed up yesterday
Elif [-e ${dir2}];then//To determine that the incremental backup was done yesterday
Innobackupex--user $user--password $pass-- INCREMENTAL/BACKUP/NEWBAK${D1}--incremental-basedir=${dir2}--no-timestamp//Specify the incremental backup file that was backed up yesterday
else
Innobackupex--user $user--password $pass/backup/allbak${d1}--no-timestamp
//yesterday did not make an incremental backup, and did not do a full backup, then do a full backup.
Fi
加权限:chmod +x /root/allbak.sh chmod +x /root/newbak.sh写计划任务:crontab -e 00 03 * * 1 /root/allbak.sh &>/dev/null //每周一的凌晨3点执行完全备份00 04 * * 2-7 /root/newbak.sh & >/dev/null //每周二到周7的凌晨4点执行增量备份重启crond服务systemctl restart crond systemctl enable crond
Backup MySQL with Innobackup combined with scheduled tasks