Home Database Mysql Tutorial SqlServer自动备份策略设置

SqlServer自动备份策略设置

Jun 07, 2016 pm 03:21 PM
sqlserver enterprise backup Strategy Manager automatic set up

企业管理器中的 Tools , Database Maintenance Planner ,可以设置数据库的定期自动备份计划。并通过启动 Sql server Agent 来自动运行备份计划。具体步骤如下 : 1 、打开企业管理器,在控制台根目录中依次点开 Microsoft SQL Server--SQL Server 组 -- 双

企业管理器中的ToolsDatabase Maintenance Planner,可以设置数据库的定期自动备份计划。并通过启动Sql server Agent来自动运行备份计划。具体步骤如下:

1、打开企业管理器,在控制台根目录中依次点开Microsoft SQL Server-->SQL Server-->双击打开你的服务器

2、然后点上面菜单中的工具-->选择数据库维护计划器

3、下一步选择要进行自动备份的数据-->下一步更新数据优化信息,这里一般不用做选择-->下一步检查数据完整性,也一般不选择

4、下一步指定数据库维护计划,默认的是1周备份一次,点击更改选择每天备份后点确定

5、下一步指定备份的磁盘目录,选择指定目录,如您可以在D盘新建一个目录如:d:/databak,然后在这里选择使用此目录,如果您的数据库比较多最好选择为每个数据库建立子目录,然后选择删除早于多少天前的备份,一般设定47天,这看您的具体备份要求,备份文件扩展名默认的是BAK

6、下一步指定事务日志备份计划,看您的需要做选择-->下一步要生成的报表,一般不做选择-->下一步维护计划历史记录,最好用默认的选项-->下一步完成

7、完成后系统很可能会提示Sql Server Agent服务未启动,先点确定完成计划设定,然后找到桌面最右边状态栏中的SQL绿色图标,双击点开,在服务中选择Sql Server Agent,然后点击运行箭头,选上下方的当启动OS时自动启动服务

8、可以设置启动启动sql server Agent:运行Services.msc,设置sqlserverAgent为自动启动。

 

修改计划:

打开企业管理器,在控制台根目录中依次点开Microsoft SQL Server-->SQL Server-->双击打开你的服务器-->管理-->数据库维护计划

 

Sql Server自身就提供了完善的备份机制,我们只要灵活运用,就能达到令人满意的效果。

先了解一下:为何要做备份?

数据备份是容灾的基础,是指为防止系统出现操作失误或系统故障导致数据丢失,而将全部或部分数据集合从应用主机的硬盘或阵列复制到其它的存储介质的过程。传统的数据备份主要是采用内置或外置的磁带机进行冷备份。但是这种方式只能防止操作失误等人为故障,而且其恢复时间也很长。随着技术的不断发展,数据的海量增加,不少的企业开始采用网络备份。网络备份一般通过专业的数据存储管理软件结合相应的硬件和存储设备来实现。

方法一:SqlServer自动作业备份

1、打开SQL Server Management Studio

2、启动SQL Server代理

3、点击作业->新建作业

4常规中输入作业的名称

5、新建步骤,类型选T-SQL,在下面的命令中输入下面语句

DECLARE @strPath NVARCHAR(200)

set @strPath = convert(NVARCHAR(19),getdate(),120)

set @strPath = REPLACE(@strPath, ':' , '.')

set @strPath = 'D:/bak/' + 'databasename'+@strPath + '.bak'

BACKUP DATABASE [databasename] TO DISK = @strPath WITH NOINIT , NOUNLOAD , NOSKIP , STATS = 10, NOFORMAT

D:/bak/改为自己的备份路径,databasename修改为想备份的数据库的名称)

6、添加计划,设置频率,时间等。确定,完成。

方法二:SqlServer自动作业备份

SQL2005的维护计划里面自带了备份数据库任务,但不会自动按日期命名,不方便,下面的方法是以存储过程来解决的。

要用SQL2005的维护计划功能首先要确认SQL Server Agent服务是在启动状态,然后打开SQL Server Management Studio,展开对象资源管理器里的管理,右击维护计划选择新建维护计划,为维护计划命名,如:TestDB_Backup,选择默认的子计划 “Subplan_1 ”并点击上方的子计划设置按钮来设置子计划执行方式和时间:

出现设置界面,设置每天3点执行一次,

 

确定后保存一下,然后新建一个查询页,开始创建存储过程,代码如下:

USE [TestDB]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [BackupDatabase](@FolderPath varchar(500))
as
DECLARE @FullPath varchar(1000)
set @FullPath = @FolderPath+ ‘TestDB_’+convert(VARCHAR(4),year(getdate()))+right( ‘0′+convert(VARCHAR(2),MONTH(getdate())),2)+right(’0′+convert(VARCHAR(2),DAY(getdate())),2)+ ‘.bak’
backup database [TestDB] to disk=@FullPath WITH INIT
return

USE [TestDB] GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE PROCEDURE [BackupDatabase](@FolderPath varchar(500)) as DECLARE @FullPath varchar(1000) set @FullPath = @FolderPath+ ‘TestDB_’+convert(VARCHAR(4),year(getdate()))+right( ‘0′+convert(VARCHAR(2),MONTH(getdate())),2)+right(’0′+convert(VARCHAR(2),DAY(getdate())),2)+ ‘.bak’ backup database [TestDB] to disk=@FullPath WITH INIT return


设置完毕,切换回维护计划设置页面,在左边的工具箱中拖入一个执行T-SQL语句任务


双击拖入后的执行T-SQL语句任务,输入执行存储过程的语句来完成每天的自动备份工作,代码如下:


use [TestDB]
exec BackupDatabase ‘E:/SqlData/TestDB/’

use [TestDB] exec BackupDatabase ‘E:/SqlData/TestDB/’

确定后保存该维护计划即可

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
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)

Hot Topics

Java Tutorial
1665
14
PHP Tutorial
1270
29
C# Tutorial
1250
24
How to import mdf file into sqlserver How to import mdf file into sqlserver Apr 08, 2024 am 11:41 AM

The import steps are as follows: Copy the MDF file to SQL Server's data directory (usually C:\Program Files\Microsoft SQL Server\MSSQL\DATA). In SQL Server Management Studio (SSMS), open the database and select Attach. Click the Add button and select the MDF file. Confirm the database name and click the OK button.

How to solve the problem that the object named already exists in the sqlserver database How to solve the problem that the object named already exists in the sqlserver database Apr 05, 2024 pm 09:42 PM

For objects with the same name that already exist in the SQL Server database, the following steps need to be taken: Confirm the object type (table, view, stored procedure). IF NOT EXISTS can be used to skip creation if the object is empty. If the object has data, use a different name or modify the structure. Use DROP to delete existing objects (use caution, backup recommended). Check for schema changes to make sure there are no references to deleted or renamed objects.

How to check sqlserver port number How to check sqlserver port number Apr 05, 2024 pm 09:57 PM

To view the SQL Server port number: Open SSMS and connect to the server. Find the server name in Object Explorer, right-click it and select Properties. In the Connection tab, view the TCP Port field.

How to recover accidentally deleted database in sqlserver How to recover accidentally deleted database in sqlserver Apr 05, 2024 pm 10:39 PM

If you accidentally delete a SQL Server database, you can take the following steps to recover: stop database activity; back up log files; check database logs; recovery options: restore from backup; restore from transaction log; use DBCC CHECKDB; use third-party tools. Please back up your database regularly and enable transaction logging to prevent data loss.

Where is the sqlserver database? Where is the sqlserver database? Apr 05, 2024 pm 08:21 PM

SQL Server database files are usually stored in the following default location: Windows: C:\Program Files\Microsoft SQL Server\MSSQL\DATALinux: /var/opt/mssql/data The database file location can be customized by modifying the database file path setting.

What to do if the sqlserver service cannot be started What to do if the sqlserver service cannot be started Apr 05, 2024 pm 10:00 PM

When the SQL Server service fails to start, here are some steps to resolve: Check the error log to determine the root cause. Make sure the service account has permission to start the service. Check whether dependency services are running. Disable antivirus software. Repair SQL Server installation. If the repair does not work, reinstall SQL Server.

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 to delete sqlserver if the installation fails? How to delete sqlserver if the installation fails? Apr 05, 2024 pm 11:27 PM

If the SQL Server installation fails, you can clean it up by following these steps: Uninstall SQL Server Delete registry keys Delete files and folders Restart the computer

See all articles