LiuYuan's Blog
This is LiuYuan's Blog
Toggle navigation
LiuYuan's Blog
Home
Docker相关
MySQL相关
Ansible相关
维护脚本相关
Windows系统脚本
Python相关
Nginx相关
About Me
Archives
Tags
MySQL备份脚本
2024-06-19 16:04:41
137
0
0
admin
#MySQL备份脚本 #!/bin/sh #定义数据库host及备份目录 backup_host="10.0.0.1" backup_dir=/data/mysql_bak # 定义数据库账号密码及备份语句 mysql_user="user" mysql_password="passwd" mysqldump_opts="-h $backup_host -u $mysql_user -p$mysql_password --default-character-set=utf8" date=$(date '+%Y%m%d%H%M') #以下为定义数据库库名,二选一 # 定义数据库库名 databases='databasename1 databasename2' # 自动获取数据库库名 databases=$(mysql -h $backup_host -u $mysql_user -p$mysql_password -e 'SHOW DATABASES;' | grep -Ev "(Database|information_schema|performance_schema|mysql|sys)") #测试数据库 umask 0077 test ! -d "$backup_dir" && mkdir -p "$backup_dir" test ! -w "$backup_dir" && echo "Error: $backup_dir is un-writeable." && exit 0 # 循环备份 for database in $databases do /usr/local/mysql/bin/mysqldump $mysqldump_opts $database > "$backup_dir/$database-$date.sql" done # 打包备份文件并删除7天前的备份 cd "$backup_dir" tar zcvf "mysqlbak_$date.tar.gz" *.sql rm -rf *.sql find "$backup_dir" -type f -mtime +7 -name "*.tar.gz" -delete #find "$backup_dir" -mtime +7 -name "*.tar.gz" -exec rm -rf {} \;
Pre:
命令备忘
Next:
ansible安装
0
likes
137
Weibo
Wechat
Tencent Weibo
QQ Zone
RenRen
Footer
Table of content