Link to home
Start Free TrialLog in
Avatar of Sam80
Sam80

asked on

How to use delphi to backup sqlserver database?

I use delphi for developing application and  sqlserver for database server.
and now I want to add one more  function that can backup the database automatically without operations in SQLserver console

any solutions or  codes samples?

Regards,
Sam
Avatar of Delphi_76
Delphi_76

Backing up an entire database:

{u can write this in a query component-sql property, call it by "Query1.ExecSql" not "Query1.open"}

BACKUP DATABASE {database_name | @database_name_var}
TO <backup_device> [,...n]
[WITH
    [BLOCKSIZE = {blocksize | @blocksize_variable}]
    [[,] DESCRIPTION = {text | @text_variable}]
    [[,] DIFFERENTIAL]
    [[,] EXPIREDATE = {date | @date_var}
        | RETAINDAYS = {days | @days_var}]
    [[,] FORMAT | NOFORMAT]
    [[,] {INIT | NOINIT}]
    [[,] MEDIADESCRIPTION = {text | @text_variable}]
    [[,] MEDIANAME = {media_name | @media_name_variable}]
    [[,] [NAME = {backup_set_name | @backup_set_name_var}]
    [[,] {NOSKIP | SKIP}]
    [[,] {NOUNLOAD | UNLOAD}]
    [[,] [RESTART]
    [[,] STATS [= percentage]]
]

Backing up specific files or filegroups:

BACKUP DATABASE {database_name | @database_name_var}
    <file_or_filegroup> [,...n]
TO <backup_device> [,...n]
[WITH
    [BLOCKSIZE = {blocksize | @blocksize_variable}]
    [[,] DESCRIPTION = {text | @text_variable}]
    [[,] EXPIREDATE = {date | @date_var}
        | RETAINDAYS = {days | @days_var}]
    [[,] FORMAT | NOFORMAT]
    [[,] {INIT | NOINIT}]
    [[,] MEDIADESCRIPTION = {text | @text_variable}]
    [[,] MEDIANAME = {media_name | @media_name_variable}]
    [[,] [NAME = {backup_set_name | @backup_set_name_var}]
    [[,] {NOSKIP | SKIP}]
    [[,] {NOUNLOAD | UNLOAD}]
    [[,] [RESTART]
    [[,] STATS [= percentage]]
]

Backing up a transaction log:

BACKUP LOG {database_name | @database_name_var}
{
    [WITH
        { NO_LOG | TRUNCATE_ONLY }]
}
|
{
    TO <backup_device> [,...n]
    [WITH
        [BLOCKSIZE = {blocksize | @blocksize_variable}]
        [[,] DESCRIPTION = {text | @text_variable}]
        [[,] EXPIREDATE = {date | @date_var}
            | RETAINDAYS = {days | @days_var}]
        [[,] FORMAT | NOFORMAT]
        [[,] {INIT | NOINIT}]
        [[,] MEDIADESCRIPTION = {text | @text_variable}]
        [[,] MEDIANAME = {media_name | @media_name_variable}]
        [[,] [NAME = {backup_set_name | @backup_set_name_var}]
        [[,] NO_TRUNCATE]
        [[,] {NOSKIP | SKIP}]
        [[,] {NOUNLOAD | UNLOAD}]
        [[,] [RESTART]
        [[,] STATS [= percentage]]
    ]
}


<backup_device> ::=
    {
        {backup_device_name | @backup_device_name_var}
        |
        {DISK | TAPE | PIPE} =
            {'temp_backup_device' | @temp_backup_device_var}
    }


<file_or_filegroup> ::=
    {
        FILE = {logical_file_name | @logical_file_name_var}
        |
        FILEGROUP = {logical_filegroup_name | @logical_filegroup_name_var}
    }

Avatar of Sam80

ASKER

yes ,well , thanks .
I want to back up the entire database, what is the back up database output format ? a file ?

I want to save those backup file in a specifed dir ,

and also , I 'd like to know how to revert  to the database that backuped before,according to your method.


Regards,
Sam
ASKER CERTIFIED SOLUTION
Avatar of Peter_
Peter_

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Dear Sam,
i believe peter's method is very much effective and more powerful.. unless u cant install the SQL tools on the client PC.. other than that..it's just perfect!..

cheers,

Delphi_76
Avatar of Sam80

ASKER

Thanks Delphi_76, Peter. thank you for all your helps.
I see I forgot to explain FAppName which is used when doing the connection.
This is simply a string that is to hold a description/name of your application, this can also be empty.
It is included if the server administrator choose view connections.

If you want to be able to access the interface components (GUI) of Enterprise Manager,
then you need to use the SQLNamespace (like in my sample). SQLNamespace is then navigated to gain access
to the proper DMO object.

But if you want access to "under the hood" functions only (like making a backup
without showing any GUI), you may choose to create and access the SQLDMO objects directly instead.

There are plenty of documentation and samples on the internet featuring both cases. The language though is mostly C++ or Visual Basic.