Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Mysql backup script

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Read several scripts on the Internet, slightly modified according to the online, a little more accurate, that is, the lack of logging function, have not decided how to write, follow-up add

#! / bin/bash

# increase the number of backups

Backup_Max_Files=20

# Database backup directory

WORKING_DIR=$ (dirname "$0")

Export WORKING_DIR=$ (cd-P "$WORKING_DIR" / > / dev/null; pwd)

# Database IP address, account number, password

DB_Host= "XXXXXXX"

DB_User= "XXXXXX"

DB_Password= "XXXXXXX"

# databases to be backed up

Databases= (

"mysql"

"zabbix"

)

# =

Echo "# Bash Shell MySql Databases Backup Tools!"

# define variable = current date

Date_Time=$ (date +% F)

# define backup file prefix

BackDataFile=zhang "${Date_Time}"

Cd $WORKING_DIR

Mkdir $BackData_File

# count the number of databases that need to be backed up

Count=0

While ["x$ {Databases [count]}"! = "x"]

Do

Count=$ ((count + 1))

Echo "Databases=$count"

Done

Echo "[+] ${count} The database is about to be backed up"

For Databases in ${Databases [@]}

Do

Echo "[+] MySql-backing up: ${Databases}"

Echo-n "MySql Backup Start:"

Echo $(date + "% Y-%m-%d% H:%M:%S")

# use the Mysqdump tool to make a logical backup (full) and send a successful failure reminder using an email script

If $(mysqldump-h$ {DB_Host}-u$ {DB_User}-p$ {DB_Password}-- single-transaction-- flush-logs mysql > ${BackData_File} / ${Databases} .sql)

Then

Echo "Success Oklahs: Mysql database backup success!"

Python / root/jiaoben/mail.py "y@163.com"${Databases} Database backup succeeded"${Databases} > Database backup succeeded"

Else

Echo "Error notations: Failure of MySQL database backup!"

# python / root/jiaoben/mail.py "y@163.com"${Databases} Database backup failed"${Databases} > Database backup failed Please check"

Fi

Echo-n "Backup success:"

Echo $(date + "% Y-%m-%d% H:%M:%S")

Done

Echo

Echo "[+] Packaging and compressing folders with Tar...."

# package and compress the generated database files using the tar command

Cd $WORKING_DIR

Tar-cv ${BackData_File} | bzip2 > ${BackData_File} .tar.bz2 & & rm-rf ${BackData_File}

# Delete data files backed up n days ago

Echo "[+] Delete over time database backup files...."

Find $WORKINGDIR-name "zhang*.tar.bz2"-mtime + ${Backup_Max_Files}-exec rm-rf {}\

# find $WORKINGDIR-name "zhang*.tar.bz2"-mtime + ${Backup_Max_Files}-exec ls {}\

Replacement: email alarm script (online version, tested and available without problem)

#! / usr/bin/python

Coding=utf-8

# Test method: python 5.py XXXXX@163.com test test

# the ability to send an email indicates a success

Import smtplib

Import sys

From email.mime.text import MIMEText

Class send_mail (): mail_host= "smtp.163.com"

Mail_user= "XXXXXX@163.com" br/ > mail_host= "smtp.163.com"

Mail_user= "XXXXXX@163.com"

Mail_pass= "XXXXXx"

Mail_postfix= "163.com"

Def init (self,sub,content): self.me= "Zabbix-Alter" + "" br/ > self.me= "Zabbix-Alter" + ""

Self.msg ['From'] = self.me

Self.msg ['Subject'] = sub

Def sendMessage (self,user): self.msg ['To'] = ";" .join (user) try: s = smtplib.SMTP_SSL (host=self.mail_host,port=465) s.login (self.mail_user,self.mail_pass) s.sendmail (self.me, user, self.msg.as_string ())

# send email

S.close ()

Except Exception, e:

Print str (e)

If name = 'main':

Mailto_list= []

Mailto_list.append (sys.argv [1])

S = send_mail (sys.argv [2], sys.argv [3])

S.sendMessage (mailto_list)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report