最近项目需要定时备份mysql数据库的数据,根据需求写了一份定时备份mysql数据库的脚本。在这儿记一下以后要用了可以直接拿来用

  -h mysql的地址  默认为localhost

  -P 端口号  默认为3306

  -u 用户  默认为root

  -p 密码 默认为123456

  -f  备份存放地址  默认为 /bak 下面

  -n 指定数据库  默认为所有数据库(除开mysql系统自带数据库)

#!/bin/bash

now=$(date "+%Y-%m-%d_%H:%M:%S")
echo "=============================$now================================="
echo "begin to backup mysql at : $now" mysqlDumpurl=$(which mysqldump)
mysqlUrl=$(which mysql)
if [ -n $mysqlDumpurl] | [ -n mysqlUrl ]; then
echo "cant't find mysql application" >&
exit
fi username="root"
dbName=""
mysql_host="localhost"
mysql_port=""
password=""
back_url="/bak/back_$now.sql" while getopts h:P:u:p:f:n: opt; do
case "$opt" in
h) mysql_host=$OPTARG ;;
n) dbName=$OPTARG ;;
P) mysql_port=$OPTARG ;;
u) username=$OPTARG ;;
p) password=$OPTARG ;;
f)
fileUrl=$OPTARG
if [ -d $fileUrl ]; then
if [[ $fileUrl == */ ]]; then
back_url="$fileUrlback_$now.sql"
else
back_url="$fileUrl/back_$now.sql"
fi
else
echo "$fileUrl is not a directory" >&
exit
fi
;;
*)
echo "$now error option there is only permmit -h host,-P port -u user,-p password ,-f fileUrl,-n dbName" >&
exit
;;
esac done result=""
if [[ -n $dbName ]]; then
result=$dbName
else
result=$($mysqlUrl -h$mysql_host -P$mysql_port -u$username -p$password -e 'show databases' | grep -v 'Warning\|Database\|information_schema\|performance_schema\|sys\|mysql')
fi if [ $? -eq ]; then
for db in $result; do
echo "begin to backup database : $db "
$mysqlDumpurl -h$mysql_host -P$mysql_port -u $username -p$password $db >>$back_url
done else
echo "$now mysql connection error" >&
exit
fi end=$(date "+%Y-%m-%d_%H:%M:%S")
echo "end to backup mysql at : $end" echo "=============================$end================================="

  例如指令如下  即可立刻进行备份

sh /root/mysql_bak/mysqlbak.sh -h 192.168.0.1 -P  -u root -p  -n bz  -f /bak/test >>/root/mysql_bak/error.log  >> /root/mysql_bak/success.log

  也可以放在linux上定时执行即可,例如每天下午7点半执行的话

[root@db-mysql mysql_bak]# crontab -e

  然后加上如下任务

  * * *  /root/mysql_bak/mysqlbak.sh -h 192.168.0.1 -P  -u root -p  -n bz  -f /bak/test >>/root/mysql_bak/error.log  >> /root/mysql_bak/success.log

  查看备份文件

[root@db-mysql mysql_bak]# ll /bak/test/
total
-rw-r--r-- root root Feb : back_2020--24_19::.sql
-rw-r--r-- root root Feb : back_2020--24_19::.sql
-rw-r--r-- root root Feb : back_2020--24_19::.sql
-rw-r--r-- root root Feb : back_2020--24_19::.sql
-rw-r--r-- root root Feb : back_2020--24_19::.sql
-rw-r--r-- root root Feb : back_2020--24_19::.sql
-rw-r--r-- root root Feb : back_2020--24_19::.sql
-rw-r--r-- root root Feb : back_2020--24_19::.sql
-rw-r--r-- root root Feb : back_2020--24_19::.sql
-rw-r--r-- root root Feb : back_2020--24_19::.sql
-rw-r--r-- root root Feb : back_2020--24_19::.sql
-rw-r--r-- root root Feb : back_2020--24_19::.sql
-rw-r--r-- root root Feb : back_2020--24_19::.sql
-rw-r--r-- root root Feb : back_2020--24_19::.sql
-rw-r--r-- root root Feb : back_2020--24_19::.sql
-rw-r--r-- root root Feb : back_2020--24_19::.sql

  查看日志

[root@db-mysql mysql_bak]# cat success.log
=============================--24_19::=================================
begin to backup mysql at : --24_19::
begin to backup database : bz
end to backup mysql at : --24_19::
=============================--24_19::=================================
=============================--24_19::=================================
begin to backup mysql at : --24_19::
begin to backup database : bz
end to backup mysql at : --24_19::
=============================--24_19::=================================
=============================--24_19::=================================
begin to backup mysql at : --24_19::
begin to backup database : bz
end to backup mysql at : --24_19::
=============================--24_19::=================================
=============================--24_19::=================================
begin to backup mysql at : --24_19::
begin to backup database : bz
end to backup mysql at : --24_19::
=============================--24_19::=================================

 

[root@db-mysql mysql_bak]# cat error.log
mysqldump: [Warning] Using a password on the command line interface can be insecure.
mysqldump: [Warning] Using a password on the command line interface can be insecure.
mysqldump: [Warning] Using a password on the command line interface can be insecure.
mysqldump: [Warning] Using a password on the command line interface can be insecure.
mysqldump: [Warning] Using a password on the command line interface can be insecure.
mysqldump: [Warning] Using a password on the command line interface can be insecure.
mysqldump: [Warning] Using a password on the command line interface can be insecure.
mysqldump: [Warning] Using a password on the command line interface can be insecure.
-bash: at: command not found
-bash: at: command not found
/bin/sh: root: command not found
mysqldump: [Warning] Using a password on the command line interface can be insecure.
mysqldump: [Warning] Using a password on the command line interface can be insecure.
mysqldump: [Warning] Using a password on the command line interface can be insecure.
mysqldump: Got error: : Can't connect to MySQL server on '192.168.3.147' (111) when trying to connect
mysqldump: [Warning] Using a password on the command line interface can be insecure.
mysqldump: [Warning] Using a password on the command line interface can be insecure.
mysqldump: Got error: : Access denied for user 'root'@'localhost' (using password: YES) when trying to connect

定时备份mysql数据库的shell脚本的更多相关文章

  1. Linux下定时备份MySQL数据库的Shell脚本

    Linux下定时备份MySQL数据库的Shell脚本   对任何一个已经上线的网站站点来说,数据备份都是必须的.无论版本更新还是服务器迁移,备份数据的重要性不言而喻.人工备份数据的方式不单耗费大量时间 ...

  2. Linux下备份MySQL数据库的Shell脚本

    数据库每天都想备份,手动备份太麻烦而又容易忘记,所以写了一个自动备份MySQL数据库的脚本,加入定时计划中,每天自运运行. 创建Shell脚本代码如下,命名为mysql_dump.sh #!/bin/ ...

  3. 每天自动备份MySQL数据库的shell脚本

    经常备份数据库是一个好习惯,虽然数据库损坏或数据丢失的概率很低,但一旦发生这种事情,后悔是没用的.一般网站或应用的后台都有备份数据库的功能按钮,但需要去手工执行.我们需要一种安全的,每天自动备份的方法 ...

  4. Linux shell实现每天定时备份mysql数据库

    每天定时备份mysql数据库任务,删除指定天数前的数据,保留指定天的数据: 需求: 1,每天4点备份mysql数据: 2,为节省空间,删除超过3个月的所有备份数据: 3,删除超过7天的备份数据,保留3 ...

  5. linux下使用crontab定时备份MYSQL数据库的方法:

    摘要 linux下使用crontab定时备份MYSQL数据库的方法: 只需按照下面3步做,一切都在你的掌控之下: 第一步:在服务器上配置备份目录代码: ------------------------ ...

  6. centos7-每天定时备份 mysql数据库

    centos7-每天定时备份 mysql数据库 第一步:编写数据库备份脚本database_mysql_shell.sh #!/bin/bash DATE=`date +%Y%m%d%H%M` #ev ...

  7. 【mysql】备份篇1:使用系统计划任务+mysqldump 定时备份mysql数据库 不用输入密码自动导出sql文件

    项目部署在服务期上之后,有了新的需求,需要每月定时备份mysql数据库的所有数据! 查找了网上的多篇文章之后,自己又对bat文件中的mysqldump语句进行改进,可以实现了不用输入密码就能自动定时备 ...

  8. 如何定时备份Mysql数据库

    1.创建备份数据库存储目录 cd data/db mkdir backup #创建存储目录 2.添加备份脚本 vim backupdb.sh #创建脚本文件 脚本内容如下: #!/bin/sh db_ ...

  9. 利用crontab每天定时备份MySQL数据库

    当数据库服务器建立并正式投入生产使用后,我们不得不担忧一个问题:当数据库遭到破坏后,怎样安然恢复到最后一次正常的状态,使得数据的损失达到最小. 我这里以本博客的wordpress数据为例,来讨论并实现 ...

随机推荐

  1. css样式读取

    在做页面改写时,发现外部引入的样式表中一部分的样式起作用,另一部分的样式没有用.无论怎么修改都没有用.最后搜索了下答案,发现是css样式文件与需引入的文件编码不一致.导致样式读取不到或者读取到一半.

  2. Ubuntu, 更新Sourses.list

    1.备份原文件 sudo cp /etc/apt/sources.list /etc/apt/sources_list.bak 2.加载文件 vim:vim sourses.list ubuntu d ...

  3. Python学习第三天(持续学习了很多的str类型相关方法)

    今天的主要内容是各种各样的str对应方法,就直接把自己测试的东西放在了下面:还有很多习题,这个倒是得抓紧啊. #expandtabs:以制表符\t对字符串进行断句,并根据参数数字补齐位数 test = ...

  4. springmvc 整合 redis

    引入依赖 <!--redis--> <dependency> <groupId>redis.clients</groupId> <artifact ...

  5. html div四边阴影效果

      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.or ...

  6. lnmt

    目录 1.nginx安装与配置 1.1安装nginx 1.2nginx安装后的配置 2.mysql安装与配置 2.1安装mysql 2.2mysql配置 3.部署tomcat 3.1java环境安装 ...

  7. BZOJ1034[ZJOI2008]泡泡堂

    一开始是不会的,不知道如何处理相等的情况,瞎贪心一直WA. 于是就递归处理是让相等的平局还是输掉,如下,拿到了50分. int solve(int *a,int *b,int i,int l,int ...

  8. Java开发之Redis

    简介 Redis 是完全开源免费的,遵守 BSD 协议,是一个高性能的 key - value 数据库 Redis 与 其他 key - value 缓存产品均有以下特点: Redis 支持数据持久化 ...

  9. Java下载文件时文件名中的中文变成下划线,其他正常

    将 utf-8 转换成 ISO8859-1 编码 response.addHeader("Content-Disposition", "attachment;filena ...

  10. Codeforces Global Round 3:B. Born This Way

    Born This Way原文链接:[传送门] 题目大意:潇洒哥想乘坐飞机从A地到达C地,但是没有直达的航班,在A地和B地之间有一个可以中转的航班B,潇洒哥想早点到达C地(有航班就坐),但是很不幸他得 ...