最近项目需要定时备份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. (复习)父子组件传值使用v-modal双向绑定,报错Avoid mutating a prop directly解决方案

    报错:Avoid mutating a prop directly since the value will be overwritten whenever the parent component. ...

  2. JS高级---一个神奇的原型链

    一个神奇的原型链 <script> var divObj=document.getElementById("dv"); console.dir(divObj); //d ...

  3. vs2008编译错误fatal error C1902: 程序数据库管理器不匹配;请检查安装解决

    重装了本本上的Xp系统,如往常一样,升级,装杀毒软件,开发工具.一些进行的非常顺利.然而,在我打开VS2008准备耕作的时候,尽然出现了一邪恶的错误提示:vs2008编译错误fatal error C ...

  4. python之路之网络基础

    c类地址

  5. codeforce F - Three Paths on a Tree

    F. Three Paths on a Tree time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

  6. Apache Kafka(八)- Kafka Delivery Semantics for Consumers

    Kafka Delivery Semantics 在Kafka Consumer中,有3种delivery semantics,分别为:至多一次(at most once).至少一次(at least ...

  7. Hive文件与记录格式

    1. Hive文件与记录格式 Create table 有多种用法,例如STORED AS SEQUENCEFILE, ROW FORMAT DELIMITED, SERDE, INPUTFORMAT ...

  8. 安装Elasticsearch出现 node validation exception 的问题处理

    es报错如下: [2019-10-11T16:23:28,945][ERROR][o.e.b.Bootstrap ] [es-node-1] node validation exception[3] ...

  9. Django Auth组件->扩展用户

    Auth用户 1.声明用户表 djangauth/settings.py..............................AUTH_USER_MODEL = 'app01.UserInfo' ...

  10. Maven安装与学习

    一.安装 1.下载http://maven.apache.org/download.cgi 2.选择zip格式 安装完后解压到某一位置(E:\xitong\major\apache-maven-3.6 ...