Home Database Mysql Tutorial 自动备份数据库并发送邮件报告数据库

自动备份数据库并发送邮件报告数据库

Jun 07, 2016 pm 04:17 PM
send backup Report database automatic mail

最近在一个做企业的一个内部系统,数据库采用的是SQL2000,为了保证数据的安全性,需要每天下班之后做数据备份,并且通过邮件的方式通知管理员备份情况。备份数据库很简单,用SQL代理建立一个作业,每天定时备份数据库即可,通过SQL2000来发邮件的话,在网上

   最近在一个做企业的一个内部系统,数据库采用的是SQL2000,,为了保证数据的安全性,需要每天下班之后做数据备份,并且通过邮件的方式通知管理员备份情况。备份数据库很简单,用SQL代理建立一个作业,每天定时备份数据库即可,通过SQL2000来发邮件的话,在网上找了些资料,发现有多种方式可以采用。

  一、通过SQL Mail

  SQL Mail 提供了一种从 Microsoft SQL Server 发送和阅读电子邮件的简单方法。原理是通过调用服务器上面的 MAPI 子系统来进行邮件发送,所以服务器上面需要安装诸如 Microsoft Outlook(不能是Outlook ExPRess) 之类的 MAPI 客户端,而且在发送邮件的时候,Outlook必须处于打开的状态。具体的设置方法可以通过网上查询。

  二、使用CDONTS

  通过调用本机的SMTP服务来发送邮件,所以服务器上必须安装IIS和SMTP。相应的存储过程为

  CREATE PROCEDURE [dbo].[sp_send_cdontsmail]

  @From varchar(100),

  @To varchar(100),

  @Subject varchar(100),

  @Body varchar(4000),

  @CC varchar(100) = null,

  @BCC varchar(100) = null

  AS

  Declare @MailID int

  Declare @hr int

  EXEC @hr = sp_OACreate 'CDONTS.NewMail', @MailID OUT

  EXEC @hr = sp_OASetProperty @MailID, 'From',@From

  EXEC @hr = sp_OASetProperty @MailID, 'Body', @Body

  EXEC @hr = sp_OASetProperty @MailID, 'BCC',@BCC

  EXEC @hr = sp_OASetProperty @MailID, 'CC', @CC

  EXEC @hr = sp_OASetProperty @MailID, 'Subject', @Subject

  EXEC @hr = sp_OASetProperty @MailID, 'To', @To

  EXEC @hr = sp_OAMethod @MailID, 'Send', NULL

  EXEC @hr = sp_OADestroy @MailID

  调用方法:

  exec sp_send_cdontsmail 'someone@shouji138.com','someone2@hks8.com','测试邮件标题','这里是邮件内容,推荐一个好的小说站,好看书吧,'

  三、使用CDOSYS

  微软已经在 Windows 2000、Windows xp 以及 Windows 2003 中淘汰了 CDONTS,所以使用CDOSYS是目前最好的解决方案。使用CDOSYS可以使用远程的SMTP服务器来发送邮件,我们通过测试163网易的免费邮箱,可以正常发送邮件,相应的存储过程如下:

  CREATE PROCEDURE sys_sendmail @To varchar(100) , @Bcc varchar(500), @Subject varchar(400)=" ",

  @Body varchar(4000) =" "

  AS

  Declare @smtpserver varchar(50) --SMTP服务器地址

  Declare @smtpusername varchar(50) --SMTP服务器用户名

  Declare @smtpuserpassWord varchar(50) --SMTP服务器密码

  set @smtpserver = 'smtp.163.com'

  set @smtpusername = 'yourname@163.com' --这里设置成你的163邮箱用户名

  set @smtpuserpassword = 'password' --这里设置成你的163邮箱密码

  Declare @object int

  Declare @hr int

  EXEC @hr = sp_OACreate 'CDO.Message', @object OUT

  EXEC @hr = sp_OASetProperty @object, 'Configuration.fields("").Value','2'

  EXEC @hr = sp_OASetProperty @object, 'Configuration.fields("").Value', @smtpserver

  --下面三条语句是smtp验证,如果服务器需要验证,则必须要这三句,你需要修改用户名和密码

  EXEC @hr = sp_OASetProperty @object, 'Configuration.fields("").Value','1'

  EXEC @hr = sp_OASetProperty @object, 'Configuration.fields("").Value',@smtpusername

  EXEC @hr = sp_OASetProperty @object, 'Configuration.fields("").Value',@smtpuserpassword

  EXEC @hr = sp_OAMethod @object, 'Configuration.Fields.Update', null

  EXEC @hr = sp_OASetProperty @object, 'To', @To

  EXEC @hr = sp_OASetProperty @object, 'Bcc', @Bcc

  EXEC @hr = sp_OASetProperty @object, 'From', @smtpusername

  EXEC @hr = sp_OASetProperty @object, 'Subject', @Subject

  EXEC @hr = sp_OASetProperty @object, 'TextBody', @Body

  EXEC @hr = sp_OAMethod @object, 'Send', NULL

  --判断出错

  IF @hr 0

  BEGIN

  EXEC sp_OAGetErrorInfo @object

  print 'failed'

  return @object

  END

  PRINT 'success'

  EXEC @hr = sp_OADestroy @object

  GO

  调用存储过程发送邮件:exec sys_sendmail 'someone@shouji138.com','someone2@hks8.com','测试邮件标题','这里是邮件内容,手机主题,'

  从以上三种方法的优缺点比较来看,我们当然采取第三种方法,不需要在服务器上装别的组件和程序。我们可以在SQL代理中建立一个作业,调度设为每天下午6点,执行的数据库备份语句和发送邮件的SQL如下:

  declare @dbname varchar(50)

  set @dbname = 'dbtest' --设置数据库名

  declare @filename nvarchar(100)

  declare @time datetime

  set @time = getdate()

  set @filename= 'D:数据库自动备份'+@dbname+substring(replace(replace(replace(CONVERT(varchar, @time, 120 ),'-',''),' ',''),':',''),1,14 )+'.bak'

  --print @filename

  BACKUP DATABASE dbtest TO DISK = @filename WITH NOINIT, NOUNLOAD, NAME = N'BIS_data_backup', NOSKip , STATS = 10, NOFORMAT

  --下面获取备份之后文件的大小

  declare @size int

  declare @sizeM decimal (5, 2)

  select top 1 @size=backup_size

  from msdb.dbo.backupset

  where database_name = @dbname

  order by backup_start_date desc

  set @sizeM = CAST(@size as float)/1024/1024

  --print @sizeM

  --邮件内容

  declare @content varchar(2000)

  set @content='数据库自动备份成功。

  数据库名:'+@dbname+'

  备份文件名:'+@filename+'

  备份文件大小:'+convert(varchar,@sizeM)+'M

  备份时间:'+CONVERT(varchar, @time, 120 )+'

  这是一封系统自动发出的邮件,用来每天报告数据库自动备份情况,请不要直接回复。'

  --print @content

  --发送邮件

  EXECUTE dbtest.dbo.sys_sendmail 'dba@hks8.com','dba@shouji138.com',

  '数据库自动备份日报',@content

  go

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use MySQL backup and restore in PHP? How to use MySQL backup and restore in PHP? Jun 03, 2024 pm 12:19 PM

Backing up and restoring a MySQL database in PHP can be achieved by following these steps: Back up the database: Use the mysqldump command to dump the database into a SQL file. Restore database: Use the mysql command to restore the database from SQL files.

How does Go language implement the addition, deletion, modification and query operations of the database? How does Go language implement the addition, deletion, modification and query operations of the database? Mar 27, 2024 pm 09:39 PM

Go language is an efficient, concise and easy-to-learn programming language. It is favored by developers because of its advantages in concurrent programming and network programming. In actual development, database operations are an indispensable part. This article will introduce how to use Go language to implement database addition, deletion, modification and query operations. In Go language, we usually use third-party libraries to operate databases, such as commonly used sql packages, gorm, etc. Here we take the sql package as an example to introduce how to implement the addition, deletion, modification and query operations of the database. Assume we are using a MySQL database.

How does Hibernate implement polymorphic mapping? How does Hibernate implement polymorphic mapping? Apr 17, 2024 pm 12:09 PM

Hibernate polymorphic mapping can map inherited classes to the database and provides the following mapping types: joined-subclass: Create a separate table for the subclass, including all columns of the parent class. table-per-class: Create a separate table for subclasses, containing only subclass-specific columns. union-subclass: similar to joined-subclass, but the parent class table unions all subclass columns.

iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos Jul 18, 2024 am 05:48 AM

Apple's latest releases of iOS18, iPadOS18 and macOS Sequoia systems have added an important feature to the Photos application, designed to help users easily recover photos and videos lost or damaged due to various reasons. The new feature introduces an album called "Recovered" in the Tools section of the Photos app that will automatically appear when a user has pictures or videos on their device that are not part of their photo library. The emergence of the "Recovered" album provides a solution for photos and videos lost due to database corruption, the camera application not saving to the photo library correctly, or a third-party application managing the photo library. Users only need a few simple steps

An in-depth analysis of how HTML reads the database An in-depth analysis of how HTML reads the database Apr 09, 2024 pm 12:36 PM

HTML cannot read the database directly, but it can be achieved through JavaScript and AJAX. The steps include establishing a database connection, sending a query, processing the response, and updating the page. This article provides a practical example of using JavaScript, AJAX and PHP to read data from a MySQL database, showing how to dynamically display query results in an HTML page. This example uses XMLHttpRequest to establish a database connection, send a query and process the response, thereby filling data into page elements and realizing the function of HTML reading the database.

Detailed tutorial on establishing a database connection using MySQLi in PHP Detailed tutorial on establishing a database connection using MySQLi in PHP Jun 04, 2024 pm 01:42 PM

How to use MySQLi to establish a database connection in PHP: Include MySQLi extension (require_once) Create connection function (functionconnect_to_db) Call connection function ($conn=connect_to_db()) Execute query ($result=$conn->query()) Close connection ( $conn->close())

How to handle database connection errors in PHP How to handle database connection errors in PHP Jun 05, 2024 pm 02:16 PM

To handle database connection errors in PHP, you can use the following steps: Use mysqli_connect_errno() to obtain the error code. Use mysqli_connect_error() to get the error message. By capturing and logging these error messages, database connection issues can be easily identified and resolved, ensuring the smooth running of your application.

Apple's iOS 18 mail app gets AI wings: enhanced search, smart writing/reply, classification and summarization, etc. Apple's iOS 18 mail app gets AI wings: enhanced search, smart writing/reply, classification and summarization, etc. Jun 11, 2024 pm 09:34 PM

According to news on June 7, technology media AppleInsider published a blog post yesterday, revealing that Apple is developing Project BlackPearl internally, which mainly enhances the email application in iOS18 and macOS15 systems, allowing AI to help users compose and reply to emails. The relevant content is summarized as follows: Enhanced Search Project BlackPearl first enhances email search results, which will display more information from address books, locations and locally stored documents. Smart Replies In addition, the new "Smart Replies" function in the email application will provide appropriate reply content based on the locally running large language model (LLM) Ajax and combined with the context of the email. compose mail

See all articles