zoukankan      html  css  js  c++  java
  • mysql的备份与恢复(windows、Linux并拷贝至备机)

    mysql定时备份

    Windows版本

    @echo off & setlocal ENABLEEXTENSIONS
    :: ---------- 配置项 ----------
    :: 备份放置的路径,加 
    set BACKUP_PATH=D:Backup
    :: 要备份的数据库名称,多个用空格分隔
    set DATABASES=bds-oneaccept sakila world
    :: MySQL 用户名
    set USERNAME=root
    :: MySQL 密码
    set PASSWORD=123456
    :: MySQL Bin 目录,加 
    :: 如果可以直接使用 mysqldump(安装时添加 MySQL Bin 目录到了环境变量),此处留空即可
    set MYSQL=
    :: WinRAR 自带命令行工具的可执行文件路径,长文件名注意用 Dos 长文件名书写方式
    set WINRAR=C:Progra~1WinRARRar.exe
    :: ---------- 以下请勿修改 ----------
    set YEAR=%date:~0,4%
    set MONTH=%date:~5,2%
    set DAY=%date:~8,2%
    :: 如果在 dos 下输入 time 返回的不是 24 小时制(没有 0 填充),请自行修改此处
    set HOUR=%time:~0,2%
    set MINUTE=%time:~3,2%
    set SECOND=%time:~6,2%
    set DIR=%BACKUP_PATH%%YEAR%%MONTH%%DAY%
    set ADDON=%YEAR%%MONTH%%DAY%%HOUR%%MINUTE%%SECOND%
    :: create dir
    if not exist %DIR% (
    mkdir %DIR% 2>nul
    )
    if not exist %DIR% (
    echo Backup path: %DIR% not exists, create dir failed.
    goto exit
    )
    cd /d %DIR%
    :: backup
    echo Start dump databases...
    for %%D in (%DATABASES%) do (
    echo Dumping database %%D ...
    %MYSQL%mysqldump -u%USERNAME% -p%PASSWORD% %%D > %%D.%ADDON%.sql 2>nul
    :: winrar
    if exist %WINRAR% (
    %WINRAR% a -k -r -s -m1 -ep1 %%D.%ADDON%.rar %%D.%ADDON%.sql 2>nul
    del /F /S /Q %%D.%ADDON%.sql 2>nul
    )
    )
    echo Done
    :exit
    

    Linux版本

    #!/bin/bash
    
    date=$(date +%Y%m%d)
    #本级备份目录 backup_file_dir=/home/mysqlbak/data/${date}
    #数据库账号 account=root
    #数据库密码 password=123456
    #备份日志路径 backlog=${backup_file_dir}/backup_${date}.log #备机IP remoteIP=192.168.2.1 #备机备份路径(根据物理存储大小设置) remote_dir=/home/mysqlbak/data/${date} #在本端服务器建备份目录 if [ ! -d ${backup_file_dir} ];then mkdir -p ${backup_file_dir} fi #在本端创建备份日志 if [ ! -f ${backlog} ];then touch ${backlog} fi #在对端服务器建备份目录 ssh ${remoteIP} mkdir -p ${remote_dir} #查询所有数据库 DATABASES=$(mysql -u${account} -p${password} -e "show databases" | grep -Ev "Database|sys|information_schema|performance_schema|mysql") #循环数据库进行备份 for db in $DATABASES do echo ----------${backup_file_dir}/${db}_${date}.sql.gz BEGIN---------- >> ${backlog} mysqldump -u${account} -p${password} --default-character-set=utf8 -q --lock-all-tables --flush-logs -E -R --triggers -B ${db} | gzip > ${backup_file_dir}/${db}_${date}.sql.gz #此处需做双机互信才可互相拷贝 scp ${backup_file_dir}/${db}_$date.sql.gz ${remoteIP}:${remote_dir} echo ----------${backup_file_dir}/${db}_${date}.sql.gz COMPLETE---------- >> ${backlog} done echo "所有数据库已备份完毕!" >> ${backlog}

    Mysql恢复

    window

    待补充

    Linux

    mysql -uroot -p123456 bds-oneaccept < bds-oneaccept_20200622.sql

    已亲测,Windows与Linux备份文件不可互用,Linux备份文件需在Linux上恢复,windows同理。另,用命令(mysqldump)备份的文件需用命令(mysql)进行恢复,不可用(sqlyong、Navicat)等工具恢复。

  • 相关阅读:
    VOA 2009/11/02 DEVELOPMENT REPORT In Kenya, a Better Life Through Mobile Money
    2009.11.26教育报道在美留学生数量创历史新高
    Java中如何实现Tree的数据结构算法
    The Python Tutorial
    VOA HEALTH REPORT Debate Over New Guidelines for Breast Cancer Screening
    VOA ECONOMICS REPORT Nearly Half of US Jobs Now Held by Women
    VOA ECONOMICS REPORT Junior Achievement Marks 90 Years of Business Education
    VOA 2009/11/07 IN THE NEWS A Second Term for Karzai; US Jobless Rate at 10.2%
    Ant入门
    Python 与系统管理
  • 原文地址:https://www.cnblogs.com/lansetuerqi/p/13131645.html
Copyright © 2011-2022 走看看