最近项目需要定时备份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. 2019牛客多校第七场E Find the median 离散化+线段树维护区间段

    Find the median 题意 刚开始集合为空,有n次操作,每次操作往集合里面插入[L[i],R[i]]的值,问每次操作后中位数是多少 分析 由于n比较大,并且数可以达到1e9,我们无法通过权值 ...

  2. Oracle 12.2.0.1 Installation Fails With "PRVG-0449"

    Mac 电脑虚拟机 Parallels 中进行Oracle 12.2.0.1 数据库软件安装时,预环境检查过程中,提示堆栈大小限制[失败],即使修复问题依然如故. Oracle 12.2.0.1 In ...

  3. flask入门(一)

    flask是一个轻量级的框架,据说跟django跟比是真的轻. 首先要先配置一个虚拟环境,flask项目需要在那个虚拟环境里运行,这里需要用的venv库实在python3里的标准库,不过有的linux ...

  4. Pyarm的Pyqt的配置

    相关连接: Python PyQt 安装python3.4 x64到c盘根目录. 安装PyQt5-5.5.1-gpl-Py3.4-Qt5.5.1-x64.exe 安装pycharm-professio ...

  5. MongoDB - String转换为Int,并更新到数据库中

    方法1 使用$convert, MongoDB版本 >= 4,速度快. 使用pymongo示范,原生mongo语句并没有尝试. # 假设{'age': '47'}, 转换后为{'age': 47 ...

  6. 【C语言】用C语言输出一个吃豆人

    大圆盘减去扇形和小圆盘: #include <math.h> #include <stdio.h> int main() { double x, y; ; y >= -; ...

  7. ThinkPHP中的时间自动填充 无法获取时间

    protected $_auto = array(       array('addTime','time','1','function'),    ); addTime在数据库里的的类型必须为int ...

  8. c++中const变量定义与头文件包含的有关问题

    在使用C++进行程序开发的时候,有个常识我们很熟悉,就是把类的定义写在.h文件中,把类的具体实现写在.cpp文件中.这毫无疑问是对的.但我们很少去思考为什么要这样做,本文结合自己的学习体会,对头文件及 ...

  9. c语言用raw socket进行抓包

    https://www.cnblogs.com/MrYuan/p/5215923.html https://blog.csdn.net/qq_41787205/article/details/8669 ...

  10. bugku 你必须让他停下

    首先打开链接会发现一个不断刷新的网页 然后使用抓包工具burpsuit抓网页 然后右键点击跳转到repeater 然后点击go一直点击 注意黄色区域的变化然后在点击过程中会发现flag 然后拿到答案