MySQL数据库备份&还原-shell脚本

手动备份使用mysqldump:

  1. 备份一个数据库
  • mysqldump -hhostname -uusername -pmypwd databasename > /path to backup/bakname.sql

    备份并压缩

  • mysqldump -hhostname -uusername -pmypwd databasename | gzip > /path to backup/bakname.sql.gz

  1. 备份多个数据库
  • mysqldump -hhostname -uusername -pmypwd databases databasename1 databasename2 databasename3 > /path to
    backup/bakname.sql
  1. 备份数据库一些表
  • mysqldump -hhostname -uusername -pmypwd databasename table1 table2 table3 > /path to backup/bakname.sql
  1. 仅备份数据库结构
  • mysqldump -no-data -databases databasename1 databasename2 databasename3 > /path to backup/bakname.sql
  1. 备份所有数据库
  • mysqldump -all-databases > /path to backup/bakname.sql

数据库的还原

1. 还原无压缩数据库
mysql -hhostname -uuser -pmypwd databasename < /path to backup/bakname.sql

2. 还原压缩数据库
gunzip < /path to backup/bakname.sql.gz | mysql -hhostname -uusername -pmypwd databasename

脚本定时备份shell

1,创建一个 shell脚本文件

vim mysqlBackup.sh

2,

#!/bin/bash

# 以下配置信息请自己修改
mysql_user="USER" #MySQL备份用户
mysql_password="PASSWORD" #MySQL备份用户的密码
mysql_host="localhost"
mysql_port="3306"
mysql_charset="utf8" #MySQL编码
backup_db_arr=("db1" "db2") #要备份的数据库名称,多个用空格分开隔开 如("db1" "db2" "db3")
backup_location=/tmp/mysql  #备份数据存放位置,末尾请不要带"/",此项可以保持默认,程序会自动创建文件夹
expire_backup_delete="ON" #是否开启过期备份删除 ON为开启 OFF为关闭
expire_days=3 #过期时间天数 默认为三天,此项只有在expire_backup_delete开启时有效

# 本行开始以下不需要修改
backup_time=`date +%Y%m%d%H%M`  #定义备份详细时间
backup_Ymd=`date +%Y-%m-%d` #定义备份目录中的年月日时间
backup_3ago=`date -d '3 days ago' +%Y-%m-%d` #3天之前的日期
backup_dir=$backup_location/$backup_Ymd  #备份文件夹全路径
welcome_msg="Welcome to use MySQL backup tools!" #欢迎语

# 判断MYSQL是否启动,mysql没有启动则备份退出
mysql_ps=`ps -ef |grep mysql |wc -l`
mysql_listen=`netstat -an |grep LISTEN |grep $mysql_port|wc -l`
if [ [$mysql_ps == 0] -o [$mysql_listen == 0] ]; then
        echo "ERROR:MySQL is not running! backup stop!"
        exit
else
        echo $welcome_msg
fi

# 连接到mysql数据库,无法连接则备份退出
mysql -h$mysql_host -P$mysql_port -u$mysql_user -p$mysql_password <<end
use mysql;
select host,user from user where user='root' and host='localhost';
exit
end

flag=`echo $?`
if [ $flag != "0" ]; then
        echo "ERROR:Can't connect mysql server! backup stop!"
        exit
else
        echo "MySQL connect ok! Please wait......"
        # 判断有没有定义备份的数据库,如果定义则开始备份,否则退出备份
        if [ "$backup_db_arr" != "" ];then
                #dbnames=$(cut -d ',' -f1-5 $backup_database)
                #echo "arr is (${backup_db_arr[@]})"
                for dbname in ${backup_db_arr[@]}
                do
                        echo "database $dbname backup start..."
                        `mkdir -p $backup_dir`
                        `mysqldump -h$mysql_host -P$mysql_port -u$mysql_user -p$mysql_password $dbname --default-character-set=$mysql_charset | gzip > $backup_dir/$dbname-$backup_time.sql.gz`
                        flag=`echo $?`
                        if [ $flag == "0" ];then
                                echo "database $dbname success backup to $backup_dir/$dbname-$backup_time.sql.gz"
                        else
                                echo "database $dbname backup fail!"
                        fi
                        
                done
        else
                echo "ERROR:No database to backup! backup stop"
                exit
        fi
        # 如果开启了删除过期备份,则进行删除操作
        if [ "$expire_backup_delete" == "ON" -a  "$backup_location" != "" ];then
                 #`find $backup_location/ -type d -o -type f -ctime +$expire_days -exec rm -rf {} \;`
                 `find $backup_location/ -type d -mtime +$expire_days | xargs rm -rf`
                 echo "Expired backup data delete complete!"
        fi
        echo "All database backup success! Thank you!"
        exit
fi
注意:如果这个sh文件是在win下编辑的,需要用编辑器转换为unix格式,否则sh会执行不成功,如图:

MySQL数据库备份&还原-shell脚本_第1张图片
3,给脚本赋予执行权限

chmod +x ./mysqlBackup.sh

在当前目录下执行:
./mysqlBackup.sh然后敲回车即可!,执行完成会在你指定的backup_location目录下生成一个当前日期的文件夹,在日期文件夹下就是我们打包之后的 sql文件

4,定时执行脚本

执行crontab -e命令

crontab -e

输入以下内容,设置每天凌晨3:00定时自动备份

00 03 * * * /mysqlBackup.sh

5,MySQL恢复
mysql -u username -p databse < backup.sql

写在最后:
加wx,获得更多关于Mysql以及 mysql优化高级视频
MySQL数据库备份&还原-shell脚本_第2张图片

你可能感兴趣的:(mysql)