什么是用于备份SQL server数据库的简单命令行程序或脚本?

[英]What is a simple command line program or script to backup SQL server databases?


I've been too lax with performing DB backups on our internal servers.

我在内部服务器上执行DB备份时太松懈了。

Is there a simple command line program that I can use to backup certain databases in SQL Server 2005? Or is there a simple VBScript?

是否有一个简单的命令行程序可以用来备份SQL Server 2005中的某些数据库?还是有一个简单的VBScript?

9 个解决方案

#1


98  

To backup a single database from the command line, use osql or sqlcmd.

要从命令行备份单个数据库,请使用osql或sqlcmd。

"C:\Program Files\Microsoft SQL Server\90\Tools\Binn\osql.exe" 
    -E -Q "BACKUP DATABASE mydatabase TO DISK='C:\tmp\db.bak' WITH FORMAT"

You'll also want to read the documentation on BACKUP and RESTORE and general procedures.

您还需要阅读关于备份、恢复和一般过程的文档。

#2


9  

Schedule the following to backup all Databases:

为备份所有数据库安排以下工作:

Use Master

Declare @ToExecute VarChar(8000)

Select @ToExecute = Coalesce(@ToExecute + 'Backup Database ' + [Name] + ' To Disk =     ''D:\Backups\Databases\' + [Name]   + '.bak'' With Format;' + char(13),'')
From
Master..Sysdatabases
Where
[Name] Not In ('tempdb')
and databasepropertyex ([Name],'Status') = 'online'

Execute(@ToExecute)

There are also more details on my blog: how to Automate SQL Server Express Backups.

我的博客上还有更多细节:如何自动执行SQL Server Express备份。

#3


9  

I use ExpressMaint.

我使用ExpressMaint。

To backup all user databases I do for example:

为了备份所有用户数据库,我这样做:

C:>ExpressMaint.exe -S (local)\sqlexpress -D ALL_USER -T DB -BU HOURS -BV 1 -B c:\backupdir\ -DS

C:> ExpressMaint。exe -S(本地)\sqlexpress -D ALL_USER -T DB -BU HOURS - bv1 -B c:\backupdir\ -DS

#4


5  

I found this on a Microsoft Support page http://support.microsoft.com/kb/2019698.

我在微软的支持页面http://support.microsoft.com/kb/2019698上发现了这一点。

It works great! And since it came from Microsoft, I feel like it's pretty legit.

它太棒了!因为它来自微软,我觉得它是合法的。

Basically there are two steps.

基本上有两个步骤。

  1. Create a stored procedure in your master db. See msft link or if it's broken try here: http://pastebin.com/svRLkqnq
  2. 在主db中创建一个存储过程。请参阅msft链接或如果它坏了,请尝试:http://pastebin.com/svRLkqnq
  3. Schedule the backup from your task scheduler. You might want to put into a .bat or .cmd file first and then schedule that file.

    从任务调度程序中调度备份。您可能想先将其放入.bat或.cmd文件中,然后安排该文件。

    sqlcmd -S YOUR_SERVER_NAME\SQLEXPRESS -E -Q "EXEC sp_BackupDatabases @backupLocation='C:\SQL_Backup\', @backupType='F'"  1>c:\SQL_Backup\backup.log            
    

Obviously replace YOUR_SERVER_NAME with your computer name or optionally try .\SQLEXPRESS and make sure the backup folder exists. In this case it's trying to put it into c:\SQL_Backup

显然,用您的计算机名替换YOUR_SERVER_NAME或可选地尝试。\SQLEXPRESS并确保存在备份文件夹。在本例中,它试图将其放入c:\SQL_Backup中

#5


4  

I'm using tsql on a Linux/UNIX infrastructure to access MSSQL databases. Here's a simple shell script to dump a table to a file:

我正在Linux/UNIX基础结构上使用tsql访问MSSQL数据库。下面是一个简单的shell脚本,用于将表转储到文件中:

#!/usr/bin/ksh
#
#.....
(
tsql -S {database} -U {user} -P {password} <<EOF
select * from {table}
go
quit
EOF
) >{output_file.dump}

#6


4  

You can use the backup application by ApexSQL. Although it’s a GUI application, it has all its features supported in CLI. It is possible to either perform one-time backup operations, or to create a job that would back up specified databases on the regular basis. You can check the switch rules and exampled in the articles:

您可以使用ApexSQL备份应用程序。尽管它是一个GUI应用程序,但它在CLI中支持所有特性。可以执行一次备份操作,也可以创建一个作业,在常规的基础上备份指定的数据库。你可以查看开关规则,并在文章中举例:

#7


2  

Eventual if you don't have a trusted connection as the –E switch declares

如果您没有-E开关所声明的可信连接,就会出现这种情况

Use following command line

使用以下命令行

"[program dir]\[sql server version]\Tools\Binn\osql.exe" -Q "BACKUP DATABASE mydatabase TO DISK='C:\tmp\db.bak'" -S [server] –U [login id] -P [password]

“程序dir \[sql server版本]\ \ Binn \ osql工具。exe“-Q”将mydatabase数据库备份到磁盘='C:\tmp\db。-U[登录id] -P[密码]

Where

在哪里

[program dir] is the directory where the osql.exe exists

是osql的目录。exe存在

On 32bit OS c:\Program Files\Microsoft SQL Server\
On 64bit OS c:\Program Files (x86)\Microsoft SQL Server\

[sql server version] your sql server version 110 or 100 or 90 or 80 begin with the largest number

[sql server版本]您的sql server版本110或100或90或80以最大的数字开头

[server] your servername or server ip

服务器名称或服务器ip。

[login id] your ms-sql server user login name

[登录id]您的ms-sql服务器用户登录名

[password] the required login password

[密码]所需的登录密码

#8


2  

Below is the simple script to make database backup.

下面是进行数据库备份的简单脚本。

DECLARE @name VARCHAR(50) -- database name  
DECLARE @path VARCHAR(256) -- path for backup files  
DECLARE @fileName VARCHAR(256) -- filename for backup  
DECLARE @fileDate VARCHAR(20) -- used for file name


-- specify database backup directory
SET @path = 'C:\Backup\'  


-- specify filename format
SELECT @fileDate = CONVERT(VARCHAR(20),GETDATE(),112) 


DECLARE db_cursor CURSOR FOR  
SELECT name 
FROM master.dbo.sysdatabases 
WHERE name NOT IN ('master','model','msdb','tempdb')  -- exclude these databases


OPEN db_cursor   
FETCH NEXT FROM db_cursor INTO @name   


WHILE @@FETCH_STATUS = 0   
BEGIN   
       SET @fileName = @path + @name + '_' + @fileDate + '.BAK'  
       BACKUP DATABASE @name TO DISK = @fileName  


       FETCH NEXT FROM db_cursor INTO @name   
END   


CLOSE db_cursor   
DEALLOCATE db_cursor

#9


-10  

If you can find the DB files... "cp DBFiles backup/"

如果你能找到DB文件…“cp DBFiles备份/”

Almost for sure not advisable in most cases, but it's simple as all getup.

在大多数情况下,这几乎是不可取的,但也很简单。

智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2008/09/23/6f392b07fdd3499ace93141f69c54d55.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告