一、linux備份
使用python腳本,要求有python3和mysqldump
#! /usr/bin/python36 # -*- coding: utf-8 -*- import os import re import datetime import subprocess ## 需要備份數(shù)據(jù)庫(kù)的登錄信息 mysql_host = { 'test': '10.10.3.207' } db_user = 'root' db_passwd = 'PASSWD' db_port = '3306' ## 備份存放的目錄地址,沒有回自動(dòng)生成 back_dir = '/backups/mysql' # 備份文件保存時(shí)間,單位:天,超過就刪除 backup_keep_time = 30 # 此列表中的數(shù)據(jù)庫(kù)將不會(huì)備份,下面是默認(rèn)的基礎(chǔ)數(shù)據(jù)庫(kù)。 ignore_database = ['Database','information_schema','mysql','sys','performance_schema'] today = datetime.datetime.today().strftime('%Y%m%d%H%M') for env in mysql_host: cmd = '/usr/local/mysql/bin/mysql -h {} -u{} -p{} -P{} -e "show databases"'.format(mysql_host.get(env),db_user,db_passwd,db_port) result = subprocess.check_output(cmd,shell=True,universal_newlines=True).split(' ') databases = [i for i in result if i and i not in ignore_database] env_backup_dir = os.path.join(back_dir,env,today) os.system('mkdir -p {}'.format(env_backup_dir)) for database in databases: back_path = os.path.join(env_backup_dir,'%s_%s.sql.gz' %(database,today)) command = '/usr/local/mysql/bin/mysqldump -h {} -u{} -p{} -P{} {} --force |gzip > {}'.format( mysql_host.get(env),db_user,db_passwd,db_port,database,back_path ) os.system(command) tmp = os.path.join(back_dir,env) for dirname in os.listdir(tmp): time1 = datetime.datetime.strptime(dirname,'%Y%m%d%H%M') time_dif = datetime.datetime.today()-time1 times = time_dif.days if times >= backup_keep_time: for file in os.listdir(os.path.join(tmp,dirname)): os.remove(os.path.join(tmp,dirname,file)) os.rmdir(os.path.join(tmp,dirname))
執(zhí)行命令
python3 vim /usr/bin/mysql_bakup.py
會(huì)在/home/mysql下自動(dòng)生成一個(gè)mysql_bak的文件夾,文件夾是以當(dāng)時(shí)時(shí)間的文件夾,文件下就是數(shù)據(jù)庫(kù)的備份
[root@localhost ~]# ls /backups/mysql/test/ 202211151637 [root@localhost ~]# ls /backups/mysql/test/202211151637/ nacos_202211151637.sql.gz
二、windows服務(wù)器備份mysql腳本
@echo off echo 設(shè)置MySql數(shù)據(jù)庫(kù)的連接信息 set host=192.168.100.101 set port=3306 set user=root set pass=ECIDI@hc99 echo 設(shè)置要備份MySql數(shù)據(jù)庫(kù)名稱 set dbname1=bns_pay set dbname2=bns_qtnys set dbname3=fawkes set dbname4=fawkes_nacos set dbname5=fawkes_patrol set dbname6=qt_applet echo 獲取當(dāng)天的日期格式,例如:20200902231300 set hour=%time:~0,2% if "%time:~0,1%"==" " set hour=0%time:~1,1% set backup_date=%Date:~0,4%%Date:~5,2%%Date:~8,2%%hour%%Time:~3,2%%Time:~6,2% echo 設(shè)置備份文件的路徑 set backupfile1=D:mysql_bak\%dbname1%-%backup_date%.sql set backupfile2=D:mysql_bak\%dbname2%-%backup_date%.sql set backupfile3=D:mysql_bak\%dbname3%-%backup_date%.sql set backupfile4=D:mysql_bak\%dbname4%-%backup_date%.sql set backupfile5=D:mysql_bak\%dbname5%-%backup_date%.sql set backupfile6=D:mysql_bak\%dbname6%-%backup_date%.sql echo 使用mysqldump對(duì)指定的MySql進(jìn)行備份 echo 注意路徑中有空格的要加上雙引號(hào) "D:mysql-8.0.30-winx64inmysqldump" -h%host% -P%port% -u%user% -p%pass% -c --add-drop-table %dbname1% > %backupfile1% "D:mysql-8.0.30-winx64inmysqldump" -h%host% -P%port% -u%user% -p%pass% -c --add-drop-table %dbname2% > %backupfile2% "D:mysql-8.0.30-winx64inmysqldump" -h%host% -P%port% -u%user% -p%pass% -c --add-drop-table %dbname3% > %backupfile3% "D:mysql-8.0.30-winx64inmysqldump" -h%host% -P%port% -u%user% -p%pass% -c --add-drop-table %dbname4% > %backupfile4% "D:mysql-8.0.30-winx64inmysqldump" -h%host% -P%port% -u%user% -p%pass% -c --add-drop-table %dbname5% > %backupfile5% "D:mysql-8.0.30-winx64inmysqldump" -h%host% -P%port% -u%user% -p%pass% -c --add-drop-table %dbname6% > %backupfile6% echo 刪除過期文件,這里是超過30天就刪除 forfiles /p D:mysql_bak /s /m *.sql /d -30 /c "cmd /c del @file /f"
鏈接:https://www.cnblogs.com/wangyuanguang/p/16893146.html
-
服務(wù)器
+關(guān)注
關(guān)注
12文章
9206瀏覽量
85563 -
WINDOWS
+關(guān)注
關(guān)注
4文章
3552瀏覽量
88824 -
MySQL
+關(guān)注
關(guān)注
1文章
816瀏覽量
26613
原文標(biāo)題:二、windows服務(wù)器備份mysql腳本
文章出處:【微信號(hào):magedu-Linux,微信公眾號(hào):馬哥Linux運(yùn)維】歡迎添加關(guān)注!文章轉(zhuǎn)載請(qǐng)注明出處。
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
評(píng)論