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

后端 未结 11 862
北海茫月
北海茫月 2020-11-28 19:35

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

Is there a simple command line program that I can use to backup certain databases in SQL Serv

相关标签:
11条回答
  • 2020-11-28 19:56

    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.

    0 讨论(0)
  • 2020-11-28 19:57

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

    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]

    Where

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

    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

    [server] your servername or server ip

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

    [password] the required login password

    0 讨论(0)
  • 2020-11-28 19:57

    You could use a VB Script I wrote exactly for this purpose: https://github.com/ezrarieben/mssql-backup-vbs/

    Schedule a task in the "Task Scheduler" to execute the script as you like and it'll backup the entire DB to a BAK file and save it wherever you specify.

    0 讨论(0)
  • 2020-11-28 19:58

    To backup a single database from the command line, use osql or 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.

    0 讨论(0)
  • 2020-11-28 20:01

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

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

    0 讨论(0)
提交回复
热议问题