定时备份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" >&2
    exit 2
fi

username="root"
dbName=""
mysql_host="localhost"
mysql_port="3306"
password="123456"
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" >&2
        exit 2
    fi
;;
*) echo "$now error option there is only permmit -h host,-P port -u user,-p password ,-f fileUrl,-n dbName" >&2
exit 2
;;
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 0 ];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" >&2
exit 2
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 3306 -u root -p 123456 -n bz  -f /bak/test 2>>/root/mysql_bak/error.log  1>> /root/mysql_bak/success.log

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

[[email protected] mysql_bak]# crontab -e

  然后加上如下任务

30 19 * * *  /root/mysql_bak/mysqlbak.sh -h 192.168.0.1 -P 3306 -u root -p 123456 -n bz  -f /bak/test 2>>/root/mysql_bak/error.log  1>> /root/mysql_bak/success.log

  查看备份文件

[[email protected] mysql_bak]# ll /bak/test/
total 22368
-rw-r--r-- 1 root root  823238 Feb 24 19:04 back_2020-02-24_19:04:48.sql
-rw-r--r-- 1 root root  823238 Feb 24 19:06 back_2020-02-24_19:06:29.sql
-rw-r--r-- 1 root root  823238 Feb 24 19:06 back_2020-02-24_19:06:50.sql
-rw-r--r-- 1 root root  134459 Feb 24 19:07 back_2020-02-24_19:07:29.sql
-rw-r--r-- 1 root root 2833376 Feb 24 19:08 back_2020-02-24_19:08:12.sql
-rw-r--r-- 1 root root 2833376 Feb 24 19:10 back_2020-02-24_19:10:06.sql
-rw-r--r-- 1 root root 2833376 Feb 24 19:12 back_2020-02-24_19:12:01.sql
-rw-r--r-- 1 root root 2833376 Feb 24 19:13 back_2020-02-24_19:13:19.sql
-rw-r--r-- 1 root root 2833376 Feb 24 19:13 back_2020-02-24_19:13:38.sql
-rw-r--r-- 1 root root 2833376 Feb 24 19:14 back_2020-02-24_19:14:20.sql
-rw-r--r-- 1 root root  823242 Feb 24 19:14 back_2020-02-24_19:14:42.sql
-rw-r--r-- 1 root root  823242 Feb 24 19:32 back_2020-02-24_19:32:29.sql
-rw-r--r-- 1 root root  823242 Feb 24 19:36 back_2020-02-24_19:36:01.sql
-rw-r--r-- 1 root root       0 Feb 24 19:45 back_2020-02-24_19:45:03.sql
-rw-r--r-- 1 root root  823238 Feb 24 19:46 back_2020-02-24_19:46:30.sql
-rw-r--r-- 1 root root       0 Feb 24 19:46 back_2020-02-24_19:46:41.sql

  查看日志

[[email protected] mysql_bak]# cat success.log
=============================2020-02-24_19:36:01=================================
begin to backup mysql at : 2020-02-24_19:36:01
begin to backup database : bz
end to backup mysql at : 2020-02-24_19:36:01
=============================2020-02-24_19:36:01=================================
=============================2020-02-24_19:45:03=================================
begin to backup mysql at : 2020-02-24_19:45:03
begin to backup database : bz
end to backup mysql at : 2020-02-24_19:45:03
=============================2020-02-24_19:45:03=================================
=============================2020-02-24_19:46:30=================================
begin to backup mysql at : 2020-02-24_19:46:30
begin to backup database : bz
end to backup mysql at : 2020-02-24_19:46:30
=============================2020-02-24_19:46:30=================================
=============================2020-02-24_19:46:41=================================
begin to backup mysql at : 2020-02-24_19:46:41
begin to backup database : bz
end to backup mysql at : 2020-02-24_19:46:41
=============================2020-02-24_19:46:41=================================

 

[[email protected] 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: 2003: 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: 1045: Access denied for user ‘root‘@‘localhost‘ (using password: YES) when trying to connect

原文地址:https://www.cnblogs.com/hetutu-5238/p/12358478.html

时间: 2024-10-22 04:46:29

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

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

Linux下定时备份MySQL数据库的Shell脚本 对任何一个已经上线的网站站点来说,数据备份都是必须的.无论版本更新还是服务器迁移,备份数据的重要性不言而喻.人工备份数据的方式不单耗费大量时间和精力,还灰常不专业的说.于是,有了下面这段脚本的出现.参考了网上的很多教程,外加自己的测试,以下脚本经测试可用.#!/bin/bash#Shell Command For Backup MySQL Database Everyday Automatically By Crontab#Author :

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

centos7-每天定时备份 mysql数据库 第一步:编写数据库备份脚本database_mysql_shell.sh #!/bin/bash DATE=`date +%Y%m%d%H%M` #every minute DATABASE=springboot-admin #database name DB_USERNAME=root #database username DB_PASSWORD="mysql" #database password BACKUP_PATH=/backu

商城系统在linux系统中定时备份MySQL数据库并删除七天前的备份文件

数据库是商城软件中重要的组成部分,定时任务能周期性地自动执行中的程序必要的业务逻辑,以DSMall开源商城系统每日店铺结算的业务逻辑为例:!/bin/bash?Name:bakmysql.sh?This?is?a?ShellScript?For?Auto?DB?Backup?and?Delete?old?Backup备份地址backupdir=/home/mysqlbackup备份文件后缀时间time=`?date?+%Y%m%d%H%M%S?<br/>需要备份的数据库名称<br/>

linux设置定时备份mysql数据库

最近写自己的项目,买了阿里云服务器,可以在云上根据自己想到的需求随意使用技术,感觉很爽.备份mysql流程如下: 环境:CentOS Linux release 7.2.1511 (Core) mysql5.6.34 创建存放备份文件的文件夹mkmir /backup/mysql 创建备份脚本vi backup.shmysqldump -u root -p888 familytree | gzip > /backup/mysql/familytree_$(date +%Y%m%d_%H%M%S)

Linux下自动还原MySQL数据库的Shell脚本

创建shell脚本topjui_source.exp,内容如下: #!/usr/bin/expect spawn echo "###### running... ######" spawn echo $argv0 spawn echo $argv1 set timeout 30 spawn mysql -uroot -p123456 set timeout 30 expect { "hi" { send "You said hi\n"} &quo

linux定时备份MySQL数据库并删除七天前的备份文件

#!/bin/bash # Name:bakmysql.sh # This is a ShellScript For Auto DB Backup and Delete old Backup #备份地址 backupdir=/home/mysqlbackup #备份文件后缀时间 time=_` date +%Y_%m_%d_%H_%M_%S ` #需要备份的数据库名称 db_name=test #mysql 用户名 db_user=root #mysql 密码 db_pass=123456 my

Linux实现定时备份MySQL数据库并删除30天前的备份文件

1. MySQL5.6以上版本 2. 修改 /etc/my.cnf 文件 # vim /etc/my.cnf [client] host=localhost user=你的数据库用户 password='你的数据库密码' 3. 编写数据库脚本 mysql-backup.sh # vim mysql-backup.sh #!/bin/bash backupDir=数据库备份目录 backupTime=`date +%Y%m%d%H%M%S` mysqldump 你的数据库 | gzip > $ba

使用Navicat定时备份mysql数据库和创建报表并邮件自动发送

数据库备份在现代计算机高速发展的今日变得日益重要,程序员往往因为不重视而忽略备份数据,导致数据丢失,造成非常严重的后果.定时备份无疑是解决备份的最好的途径,本文主要使用Navicat来自动备份数据库和创建相关的计划任务. (一)使用Navicat自动备份数据库: 1,打开navicat客户端,连上mysql后,双击左边你想要备份的数据库.点击"计划",再点击"新建批处理作业"具体如下图. 2,双击上面的可用任务,它就会到下面的列表里去,也代表你选择了这个任务 3,点

linux上定时备份mysql数据库

定时备份数据库 /usr/sbin/backupmysql timestamp=`date +"%Y-%m-%d-%H-%M-%S"` mysqldump -uroot -p'123456 databasename>/db$timestamp.sql chmod +x backupmysql 安装crontab: [[email protected] ~]# yum install vixie-cron [[email protected] ~]# yum install cro