定时备份mysql数据库的shell脚本
最近项目需要定时备份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脚本的更多相关文章
- Linux下定时备份MySQL数据库的Shell脚本
Linux下定时备份MySQL数据库的Shell脚本 对任何一个已经上线的网站站点来说,数据备份都是必须的.无论版本更新还是服务器迁移,备份数据的重要性不言而喻.人工备份数据的方式不单耗费大量时间 ...
- Linux下备份MySQL数据库的Shell脚本
数据库每天都想备份,手动备份太麻烦而又容易忘记,所以写了一个自动备份MySQL数据库的脚本,加入定时计划中,每天自运运行. 创建Shell脚本代码如下,命名为mysql_dump.sh #!/bin/ ...
- 每天自动备份MySQL数据库的shell脚本
经常备份数据库是一个好习惯,虽然数据库损坏或数据丢失的概率很低,但一旦发生这种事情,后悔是没用的.一般网站或应用的后台都有备份数据库的功能按钮,但需要去手工执行.我们需要一种安全的,每天自动备份的方法 ...
- Linux shell实现每天定时备份mysql数据库
每天定时备份mysql数据库任务,删除指定天数前的数据,保留指定天的数据: 需求: 1,每天4点备份mysql数据: 2,为节省空间,删除超过3个月的所有备份数据: 3,删除超过7天的备份数据,保留3 ...
- linux下使用crontab定时备份MYSQL数据库的方法:
摘要 linux下使用crontab定时备份MYSQL数据库的方法: 只需按照下面3步做,一切都在你的掌控之下: 第一步:在服务器上配置备份目录代码: ------------------------ ...
- centos7-每天定时备份 mysql数据库
centos7-每天定时备份 mysql数据库 第一步:编写数据库备份脚本database_mysql_shell.sh #!/bin/bash DATE=`date +%Y%m%d%H%M` #ev ...
- 【mysql】备份篇1:使用系统计划任务+mysqldump 定时备份mysql数据库 不用输入密码自动导出sql文件
项目部署在服务期上之后,有了新的需求,需要每月定时备份mysql数据库的所有数据! 查找了网上的多篇文章之后,自己又对bat文件中的mysqldump语句进行改进,可以实现了不用输入密码就能自动定时备 ...
- 如何定时备份Mysql数据库
1.创建备份数据库存储目录 cd data/db mkdir backup #创建存储目录 2.添加备份脚本 vim backupdb.sh #创建脚本文件 脚本内容如下: #!/bin/sh db_ ...
- 利用crontab每天定时备份MySQL数据库
当数据库服务器建立并正式投入生产使用后,我们不得不担忧一个问题:当数据库遭到破坏后,怎样安然恢复到最后一次正常的状态,使得数据的损失达到最小. 我这里以本博客的wordpress数据为例,来讨论并实现 ...
随机推荐
- [P3806] 【模板】点分治 - 点分治
辣鸡蒟蒻怎么今天才来敲这个模板题 好像还敲了很久的样子 (大雾) #include <bits/stdc++.h> using namespace std; #define int lon ...
- Oracle VM VirtualBox - ping不通虚拟机
问题描述 用Oracle VM VirtualBox创建虚拟机后,本机电脑ping不通虚拟机 解决方案 https://www.cnblogs.com/ranrongzhen/p/6958485.ht ...
- java.awt.Font
显示效果 Font mf = new Font(String 字体,int 风格,int 字号);字体:TimesRoman, Courier, Arial等风格:三个常量 lFont.PLAIN, ...
- (转)Boyer-Moore算法
转自:Boyer-Moore算法 一.简述 在当前用于查找子字符串的算法中,BM(Boyer-Moore)算法是当前有效且应用比较广的一中算法,各种文本编辑器的“查找”功能(Ctrl+F),大多采用B ...
- python之路之线程,进程,协程2
一.线程 1.创建线程 2.主线程是否等待子线程 t.setDaemon(Ture/False):默认是false,等待子线程完成,ture,表示不等待子线程结束 3.主线程等待,子线程执行 join ...
- 数位DP 不要62
数位DP的问法是从某个数到某个数的区间里,求出满足题目要求的个数: 如本题所说的不要62和4,就是求出这个区间内,满足这一条件的数: 比如问 6 199的这个区间内满足条件的数,那么就求出1到199满 ...
- Java + Selenium 无头浏览器模式
我们说的无头模式,只是在爬虫执行的时候,不再弹出浏览器的界面,只是使用浏览器的内核进行爬取,下面是示例代码: //设置本地chromedriver地址 System.setProperty(" ...
- 12day echo {1..100} << 三剑客命令
04. 直接编辑文件命令方法 a 如何直接在文件中添加单行内容 echo "oldboy" >>/oldboy/oldboy.txt 补充: echo命令用法说明: 0 ...
- 为什么html表单用post提交后,提交页面是空白
为什么html表单用post提交后,提交页面是空白? 因为post提交就应该用doPost()方法处理数据
- 【vue】 router.beforeEach
import store from '@/store' const Vue = require('vue') const Router = require('vue-router') Vue.use( ...