mysql 备份库的shell_定时备份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=$(whichmysqldump)

mysqlUrl=$(whichmysql)if [ -n $mysqlDumpurl] | [ -n mysqlUrl ]; then

echo "cant't find mysql application" >&2exit2

fiusername="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" inh) mysql_host=$OPTARG ;;

n) dbName=$OPTARG ;;

P) mysql_port=$OPTARG ;;

u) username=$OPTARG ;;

p) password=$OPTARG ;;

f)

fileUrl=$OPTARGif [ -d $fileUrl ]; then

if [[ $fileUrl == */ ]]; thenback_url="$fileUrlback_$now.sql"

elseback_url="$fileUrl/back_$now.sql"

fi

else

echo "$fileUrl is not a directory" >&2exit2

fi;;*)echo "$now error option there is only permmit -h host,-P port -u user,-p password ,-f fileUrl,-n dbName" >&2exit2;;esac

doneresult=""

if [[ -n $dbName ]]; thenresult=$dbNameelseresult=$($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_urldone

else

echo "$now mysql connection error" >&2exit2

fiend=$(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点半执行的话

[root@db-mysql 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

查看备份文件

[root@db-mysql mysql_bak]# ll /bak/test/total22368

-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

查看日志

[root@db-mysql mysql_bak]# catsuccess.log=============================2020-02-24_19:36:01=================================begin to backup mysql at :2020-02-24_19:36:01begin 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:03begin 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:30begin 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:41begin to backup database : bz

end to backup mysql at :2020-02-24_19:46:41

=============================2020-02-24_19:46:41=================================

[root@db-mysql mysql_bak]# caterror.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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 好的,以下是一个备份MySQL数据库Shell脚本示例: ```bash #!/bin/bash # MySQL数据库备份脚本 # 设置备份文件名和目录 BACKUP_DIR="/var/backups/mysql" DATE=$(date +%Y-%m-%d_%H-%M-%S) DB_NAME="mydatabase" FILENAME="$BACKUP_DIR/$DB_NAME-$DATE.sql" # 设置MySQL登录信息 MYSQL_USER="root" MYSQL_PASSWORD="password" # 执行备份 mysqldump -u $MYSQL_USER -p$MYSQL_PASSWORD $DB_NAME > $FILENAME # 压缩备份文件 gzip $FILENAME # 删除7天前的备份文件 find $BACKUP_DIR -type f -name "*.gz" -mtime +7 -exec rm {} \; ``` 上述脚本将会备份名为 "mydatabase" 的MySQL数据库,并将备份文件保存在 "/var/backups/mysql" 目录下。脚本将创建一个新的备份文件名,格式为 "mydatabase-YYYY-MM-DD_HH-MM-SS.sql",其中 "YYYY-MM-DD_HH-MM-SS" 是当前日期和时间。备份完成后,脚本使用 gzip 压缩备份文件,并删除7天前的备份文件。 请注意,为了安全起见,建议将MYSQL_USER和MYSQL_PASSWORD的值设置为有效的MySQL用户名和密码,而不是将其硬编码在脚本中。 ### 回答2: shell脚本备份MySQL数据库是一种自动化的数据库备份方式,它通过编写shell脚本来实现备份任务的执行。下面是一个简单的用于备份MySQL数据库shell脚本示例: #!/bin/bash # 设置备份目录 BACKUP_DIR="/opt/backup/db" # 设置MySQL登录信息 DB_USER="root" DB_PASSWORD="yourpassword" # 设置备份文件名 BACKUP_FILE="db_backup_$(date +%Y%m%d%H%M%S).sql" # 创建备份目录 mkdir -p $BACKUP_DIR # 备份MySQL数据库到指定目录 mysqldump -u $DB_USER -p$DB_PASSWORD --all-databases > $BACKUP_DIR/$BACKUP_FILE # 判断备份是否成功 if [ $? -eq 0 ]; then echo "MySQL数据库备份成功,备份文件名为:$BACKUP_FILE" else echo "MySQL数据库备份失败" fi 在上述脚本中,首先定义了备份目录、MySQL登录信息和备份文件名的变量。然后,通过mkdir命令创建备份目录。接下来,使用mysqldump命令将MySQL数据库备份到指定目录,并将备份文件命名为当前日期和时间。最后,通过判断mysqldump命令执行的返回值来确定备份是否成功,并输出相应的提示信息。 使用脚本进行MySQL数据库备份只需在终端中执行bash命令加上脚本路径即可,如:bash /path/to/backup_mysql.sh。执行完毕后,备份文件将保存在指定的备份目录中。 这种shell脚本备份MySQL数据库的方式可以自动化执行备份任务,提高工作效率,同时也方便了备份文件的管理和查找。 ### 回答3: shell脚本备份MySQL数据库是通过编写一个脚本文件来实现自动备份和恢复MySQL数据库的操作。下面是一个简单的示例: #!/bin/bash # Shell脚本备份MySQL数据库 # 定义备份存储目录和时间戳 backup_dir="/path/to/backup" timestamp=$(date +%Y%m%d%H%M%S) # 定义MySQL数据库的连接参数 db_user="root" db_password="password" db_host="localhost" db_name="database_name" # 创建备份目录 if [ ! -d "$backup_dir" ]; then mkdir -p $backup_dir fi # 使用mysqldump命令备份数据库 mysqldump -u$db_user -p$db_password -h$db_host $db_name > $backup_dir/${db_name}_${timestamp}.sql # 检查备份是否成功 if [ $? -eq 0 ]; then echo "数据库备份成功!" else echo "数据库备份失败!" fi # 清理过期备份(保留最新的N个备份) backup_count=$(ls -l $backup_dir | grep -c "^-" 2>/dev/null) if [ $backup_count -gt 10 ]; then oldest_backup=$(ls -tr $backup_dir | head -1) rm -f $backup_dir/$oldest_backup fi # 恢复MySQL数据库(如果需要) # mysql -u$db_user -p$db_password -h$db_host -e "DROP DATABASE $db_name;" # mysql -u$db_user -p$db_password -h$db_host -e "CREATE DATABASE $db_name;" # mysql -u$db_user -p$db_password -h$db_host $db_name < $backup_dir/latest.sql 这个脚本的功能是备份指定的MySQL数据库,将备份文件保存到指定的目录中,并可根据需要恢复数据库脚本使用mysqldump命令来导出数据库,并使用mysql命令来恢复数据库备份文件保留最新的10个,超过数量的将自动删除最旧的备份文件。可以根据实际需求修改备份目录、数据库连接参数和保留备份数量等设置。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值