#!/bin/bash
#***********************************=
# mysql定時備份
# mysql timed backup
#***********************************=
dump=/usr/bin/mysqldump
ipaddr=127.0.0.1
port=3306
user=typecho
passwd=569da4de2139687044bb
database=typecho
root_dir=/data
#----------------------
set -u
[ -x $dump ] || exit 100
out_dir=$/mysql/mysql-new
tar_dir=$/mysql/mysql-bak-list
log_dir=$/mysql/mysql-log
log_dir_file=$log_dir/mysql-log
date=`date +%y-%m-%d`
tar_bak="$-bak-$date.tar.gz"
bak_name=$-bak-$(date -d '-5 days' "+%y-%m-%d").tar.gz
[ -d $out_dir ] || mkdir -p $out_dir
[ -d $tar_dir ] || mkdir -p $tar_dir
[ -d $log_dir ] || mkdir -p $log_dir
cd $ ||exit 200
[[ -n "$" ]] && rm -rf $/* || echo "failed to delete buffer file" >> $
mkdir -p $/$
$ -h$ -p$ -u$ -p$ --databases $ |gzip > $/$/$-$.sql.gz
if [ $? -eq 0 ];then
echo -n "[$(date +%y-%m-%d\ %h:%m:%s)] the backup successful," >> $log_dir_file
#壓縮cd $
tar -zcf $ $ &>/dev/null && echo -n "backup file packed successfully," >> $ || echo -n " packaging failure," >> $
mv $/$ $ && echo -n "backup file packaging successfully moved to data directory successfully," >> $ || echo -n "move failure," >> $
#刪除之前的備份
rm -rf $/$ && echo "backup succeeded 5 days before deletion!" >> $ || echo "backup failed 5 days before deletion。" >> $
else
echo "[$(date +%y-%m-%d_%h:%m:%s)] backup failure!" >> $
fi
如果要insert語句分開,加入如下引數
--skip-extended-insert
如果備份報錯
mysqldump: got error: 1449: "the user specified as a definer ('abc'@'%') does not exist" when using lock tables
因為mysqldump命令預設在匯出時是要鎖定表的,所以解決方式有兩個。
1、建立相關使用者
grant all on *.* to abc@'%' identified by '123456';
2、在命令中加上 --skip-lock-tables 這個引數
$ -h$ -p$ -u$ -p$ --databases --skip-lock-tables $ > $/$/$-$.sql
如果表多建議使用分表備份。
mysql分表備份指令碼:
MySQL資料庫備份指令碼
1.指令碼內容 單獨的指令碼 bin sh if 4 then echo usage 0 db host db user db pwd db name exit fiprefix date y m d h m s filepath root dbbackup date y m 4 prefix if...
mysql資料庫備份指令碼
第一步 編寫資料庫備份指令碼database mysql shell.sh bin bash date date y m d h m every minute database springboot admin database name db username root database user...
備份MySQL資料庫指令碼
備份mysql資料庫中 mysql 庫 規定每週日24點備份 root mysql vim root mysqldump.sh bin bash backup database mysql owner weijia date 2019 04 13 mysql user root 登入使用者 mysq...