Automatic and timed backup of SQL Server databases

Source: Internet
Author: User
Tags configuration settings rewind password protection

The following is an automatic backup method that I have confirmed is available.
1. Choose Enterprise Manager> Manage> SQL server proxy.
2. Create a new job and take the job name as needed. For example, for data backup, select sa as the owner. Of course, you can select another user, provided that the user has the permission to execute the job;
3. Click the step tab to go to the step panel. Create a step. You can enter the step name as needed, such as step 1. The default type and database do not need to be modified. Write the following statement in the command:
Backup database [database name] to disk = n' F: \ databack \ firenews BACKUP 'with noinit, NOUNLOAD, NAME = n' firenews BACKUP', NOSKIP, STATS = 10, NOFORMAT
Note: The Database Name, DISK = (here you need to enter the path and the Name of your database backup) can be entered at will.
4. Click the scheduling tab to go to the scheduling panel. Create a scheduling task and enter the name as needed. Click "show" and select "Schedule" for the task to be executed. such as every day, every 2 days, every week, every month, etc. set it as needed. (Note: There is a startup option next to the scheduling name, which must be checked)
5. Do not forget one thing after you confirm it. Right-click the job you just created and start the job. If there is no problem with your job, a message indicating that the job is successfully executed will be displayed, corresponding backup files appear on your disk;
6. Another important problem is that your SQL server agent server has been started.
If we need to generate a new backup based on the daily date so that we can differentiate the backup file, we need to modify the SQL statement just now.
Reference instance:

1. Automatic Backup and fixed backup name. Backup will be appended each timeCopy codeThe Code is as follows: backup database FireNews to disk = n'f: \ databack \ firenews backup 'with noinit, nounload, name = n'firenews backup', noskip, stats = 10, noformat
Backup database fireweb to disk = n' F: \ backup \ data \ fireweb backup 'with noinit, nounload, name = n' fireweb backup', noskip, stats = 10, noformat

2. automatic backup, named after time, complete backupCopy codeThe Code is as follows: declare @ filename nvarchar (100) set @ filename = 'f: \ databack \ data '+ convert (char (8), getdate (), 112) + '. bak 'print @ filename backup database [firenews] to disk = @ filename with noinit, NOUNLOAD, NAME = n' firenews BACKUP ', NOSKIP, STATS = 10, NOFORMAT
Declare @ filename nvarchar (100) set @ filename = 'f: \ backup \ data \ fireweb \ fireweb' + convert (char (8), getdate (), (112) + '. bak 'print @ filename backup database [fireweb] to disk = @ filename with noinit, NOUNLOAD, NAME = n' fireweb BACKUP ', NOSKIP, STATS = 10, NOFORMAT

Note:
1. the folder stored in the backup database must already exist;
2. N must be capitalized;
3. The value of NAME can be written at will;
4. FireNews. fireweb indicates the database name;
5. When creating a job, the system prompts: The Stored Procedure master could not be found. dbo. xp_regread: The table name master database does not have this stored procedure (it may have been deleted during Security Processing). You must register it yourself. The method is: Find the database --
Master -- extended stored procedure, right-click "create extended stored procedure" (Name: xp_regread, path: xpstar. dll)
The following describes how to back up a complete database:
Back up the entire database:Copy codeThe Code is as follows: backup database {database_name | @ database_name_var}
TO <backup_device> [,... n]
[
[BLOCKSIZE = {blocksize | @ blocksize_variable}]
[[,] DESCRIPTION = {'text' | @ text_variable}]
[[,] DIFFERENTIAL]
[[,] EXPIREDATE = {date | @ date_var}
| RETAINDAYS = {days | @ days_var}]
[[,] PASSWORD = {password | @ password_variable}]
[[,] FORMAT | NOFORMAT]
[[,] {INIT | NOINIT}]
[[,] Mediadeiterator = {'text' | @ text_variable}]
[[,] MEDIANAME = {media_name | @ media_name_variable}]
[[,] MEDIAPASSWORD = {mediapassword | @ mediapassword_variable}]
[[,] NAME = {backup_set_name | @ backup_set_name_var}]
[[,] {NOSKIP | SKIP}]
[[,] {NOREWIND | REWIND}]
[[,] {NOUNLOAD | UNLOAD}]
[[,] RESTART]
[[,] STATS [= percentage]
]

Parameters
DATABASE
Specifies a complete database backup. If a list of files and file groups is specified, only the specified files and file groups are backed up.

This indicates that Microsoft SQL Server backs up sufficient transaction logs to generate a consistent database that will be used to restore the database during full or differential database backup. Only full database backup can be used on the master database.

{Database_name | @ database_name_var}
A database is specified to back up transaction logs, some databases, or complete databases from the database. If it is provided as a variable (@ database_name_var), you can specify this name as a variable of the String constant (@ database_name_var = database name) or string data type (excluding ntext or text data.
<Backup_device>
Specifies the logical or physical backup device used for backup. It can be one or more of the following forms:
{Logical_backup_device_name} | {@ logical_backup_device_name_var}
Is the logical name of the backup device created by sp_addumpdevice. The database is backed up to this device and its name must comply with the identifier rules. If it is provided as a variable (@ logical_backup_device_name_var), you can specify the name of the backup device as a String constant (@ logical_backup_device_name_var = logical backup device name) or a variable of the string data type (except ntext or text data type.
{DISK | TAPE} =
'Physical _ backup_device_name '| @ physical_backup_device_name_var
Allows you to create backups on a specified disk or tape device. You do not need to have a specified physical device before executing the BACKUP statement. If a physical device exists and the INIT option is not specified in the BACKUP statement, the BACKUP is appended to the device.
If you specify to disk or to tape, enter the complete path and file name. For example, DISK = 'C: \ Program Files \ Microsoft SQL Server \ MSSQL \ BACKUP \ Mybackup. dat 'or TAPE =' \. \ tape0 '.

This indicates that if you enter a relative path name for the backup to the disk, the backup files will be stored in the default backup directory. This directory is set during installation and stored in the BackupDirectory registry key value under the KEY_LOCAL_MACHINE \ Software \ Microsoft \ MSSQLServer directory.
If you are using a network server with a unified naming rule (UNC) Name or a re-directed drive letter, specify the disk device type.
When multiple files are specified, the logical file name (or variable) and physical file name (or variable) can be mixed ). However, all devices must be of the same type (disk, tape, or pipe ).
Windows 98 does not support backup to a disk.

Yes indicates that you can specify placeholders for multiple backup devices. The maximum number of backup devices is 64.
BLOCKSIZE = {blocksize | @ blocksize_variable}
Specify the size of a physical block by number of nodes. In Windows NT, the default block size is set to the default block size of the device. Generally, this parameter is not required when SQL Server selects a block size suitable for the device. On Windows 2000-based computers, the default value is 65,536 (64 KB, the maximum size supported by SQL Server ).
For disks, BACKUP automatically determines the appropriate block size of the disk device.

Description if you want to store the result backup set to the CD-ROM and then recover from the CD-ROM, set BLOCKSIZE to 2048.

The default BLOCKSIZE of a tape is 65,536 (64 KB ). The block size is explicitly declared to replace the block size selected by SQL Server.
DESCRIPTION = {'text' | @ text_variable}
Specifies the free text that describes the backup set. The string can contain a maximum of 255 characters.
DIFFERENTIAL
The specified database backup or file backup should be consistent with the database or file part changed after the last full backup. Differential backup generally occupies less space than full backup. You do not need to back up all the individual logs backed up during the last full backup. For more information, see differential database backup and file differential backup.

This indicates that SQL Server backs up sufficient transaction logs during full or differential database backup to generate a consistent database when restoring the database.

EXPIREDATE = {date | @ date_var}
Specifies the date on which the backup set expires and is allowed to be overwritten. If this date is provided as a variable (@ date_var), you can specify this date as a String constant (@ date_var = date) and a string data type variable (excluding ntext or text data), smalldatetime, or datetime variable, and the date must comply with the configured system datetime format.
RETAINDAYS = {days | @ days_var}
Specifies how many days after which the backup media set can be rewritten. If specified with the variable (@ days_var), the variable must be an integer.

Important if EXPIREDATE or RETAINDAYS is not specified, the validity period depends on the media retention configuration settings of sp_configure. These options only prevent SQL Server from overwriting files. You can use other methods to erase tapes, but the operating system can delete disk files. For more information about expired verification, see the SKIP and FORMAT in this topic.

PASSWORD = {password | @ password_variable}
Set a password for the backup set. PASSWORD is a string. If a password is defined for the backup set, you must provide the password to restore the backup set.

The key backup set password prevents unauthorized access to the backup set content through SQL Server 2000, but does not prevent the backup set from being overwritten.

For more information about using passwords, see "Permissions.
FORMAT
Specify that the Media Header should be written to all volumes used for this backup operation. Any existing media header is overwritten. The FORMAT option invalidates the entire media content and ignores any existing content.

Exercise caution when using FORMAT. Formatting a backup device or media will make the entire media set unavailable. For example, if you initialize a single tape in an existing striped backup set, the entire backup set becomes unavailable.

By specifying the FORMAT, the backup operation implies SKIP and INIT.
NOFORMAT
The specified Media Header should not be written to all the volumes used for the backup operation, and the backup device should not be overwritten unless INIT is specified.
INIT
Specify that all backup sets should be overwritten, but the media header should be retained. If INIT is specified, all existing backup set data on that device will be overwritten.
Backup media is not overwritten in the following situations:
None of the Backup Settings on the media have expired. For more information, see the EXPIREDATE and RETAINDAYS options.

If the BACKUP set name is provided in the BACKUP statement, the BACKUP set name does not match the name on the BACKUP media. For more information, see the NAME clause.
Use the SKIP option to replace these checks. For more information about the interaction between SKIP, NOSKIP, INIT, and NOINIT, see the annotations section.

If the backup media has password protection, SQL Server will not write the media, unless the media password is provided. The SKIP option does not replace this check. Password-protected media can be overwritten only after formatting. For more information, see FORMAT options.

NOINIT
Indicates that the backup set will be appended to the specified disk or tape device to retain the existing backup set. NOINIT is the default setting.
The FILE option of the RESTORE command is used to select an appropriate backup set during restoration. For more information, see RESTORE.
If you define a media password for a media set, you must provide a password.
Mediadeiterator = {text | @ text_variable}
Specifies the free-format text description of a media set, which can contain a maximum of 255 characters.
Mediadeiterator = {text | @ text_variable}
Specifies the media name for the entire backup media set, which can contain a maximum of 128 characters. If MEDIANAME is specified, it must match the previously specified media name, which already exists in the backup volume. If MEDIANAME is not specified or the SKIP option is specified, the media name is not verified.
MEDIAPASSWORD = {mediapassword | @ mediapassword_variable}
Set a password for the media set. MEDIAPASSWORD is a string.
If a password is defined for a media set, the password must be provided when a backup set is created for the media set. In addition, you must provide a media password when performing any restoration operations on the media set. Password-protected media can be overwritten only after formatting. For more information, see FORMAT options.
For more information about using passwords, see "Permissions.
NAME = {backup_set_name | @ backup_set_var}
Specifies the name of the Backup set. The name can contain up to 128 characters. If no NAME is specified, it is null.
NORECOVERY
Used only with backup log. Back up the tail of the log and make the database in the restoring State. NORECOVERY is useful when a fault is transferred to a secondary database or when the log tail is saved before the RESTORE operation.
STANDBY = undo_file_name
Used only with backup log. Back up the tail of the log and make the database in read-only or standby mode. The undo file name specifies the storage that can accommodate rollback changes. If you subsequently apply the restore log operation, you must undo these rollback changes.
If the specified file name does not exist, SQL Server creates the file. If the file already exists, SQL Server overwrites it. For more information, see use a backup server.
NOREWIND
Specify that SQL Server keeps the tape open after the backup operation is complete. NOREWIND means NOUNLOAD. SQL Server retains the ownership of the tape drive until the BACKUP or RESTORE command uses REWIND.
If you accidentally turn the tape on, the fastest way to release the tape is to use the following RESTORE command:
Restore labelonly from tape = <name> with rewind: You can query the sysopentapes table in the master database to find the list of opened tapes.
REWIND
Specify that SQL Server releases tape and rewind. If neither NOREWIND nor REWIND is specified, the default value is REWIND.
NOSKIP
Indicates that the BACKUP statement checks the expiration date before it can override all BACKUP sets on the media.
SKIP
Disable BACKUP set expiration and name check. These checks are generally executed by the BACKUP statement to prevent the BACKUP set from being overwritten. For more information, see the Notes section.
NOUNLOAD
Specifies that the tape is not automatically detached from the tape drive after the backup. Always set to NOUNLOAD until the specified UNLOAD is specified. This option is only used for tape devices.
UNLOAD
Specifies that the tape is automatically replayed and detached after the backup is complete. When a new user starts a session, it is set to UNLOAD by default. This setting is maintained until the User specifies NOUNLOAD. This option is only used for tape devices.
RESTART
Specify that SQL Server restarts an interrupted backup operation. Because the RESTART option restarts the backup operation when the backup operation is interrupted, it saves time. To RESTART a specific BACKUP operation, repeat the entire BACKUP statement and add the RESTART option. You do not have to use the RESTART option, but it can save time.

Important this option is only used for backing up tape media and backing up across multiple tape volumes. There will never be a restart operation on the first volume of backup.

STATS [= percentage]
A message is displayed whenever another percentage ends. It is used to measure the progress. If percentage is omitted, SQL Server displays a message every 10 percentage points after completion.
<File_or_filegroup>
Specifies the logical name of the file or file group contained in the database backup. You can specify multiple files or file groups.
FILE = {logical_file_name | @ logical_file_name_var}
Name one or more files contained in the database backup.
FILEGROUP = {logical_filegroup_name | @ logical_filegroup_name_var}
Name one or more file groups in the database backup.

It indicates that when the database size and performance requirements make it impractical to back up a complete database, a file is backed up. To back up transaction logs separately, use backup log.

It is important that you use backup log to provide separate BACKUP of transaction logs before you can use file and file group BACKUP to restore the database. For more information about file backup, see using file backup for backup.

If the recovery model is SIMPLE, file and File Group Backup are not allowed.

Is a placeholder, indicating that multiple files and file groups can be specified. There is no limit on the maximum number of files or file groups.
LOG
Specifies that only transaction logs are backed up. This LOG is backed up from the last successfully executed LOG to the end of the current LOG. Once the log is backed up, it may cut off the space that is no longer needed by the replication or activity transaction.

If the backup log does not seem to have truncated most of the logs, there may be an old open transaction in the log. You can use dbcc sqlperf (LOGSPACE) to observe the log space. For more information, see transaction log backup.

NO_LOG | TRUNCATE_ONLY
If you do not need to back up and copy logs, you can delete the inactive logs and truncate the logs. This option releases space. Because log backup is not saved, you do not need to specify a backup device. NO_LOG and TRUNCATE_ONLY are synonymous.
Changes recorded in the log cannot be recovered after logs are backed up using NO_LOG or TRUNCATE_ONLY. To recover the DATABASE, execute backup database immediately.
NO_TRUNCATE
Logs can be backed up when the database is damaged.
Note
You can append a database or log backup to any disk or tape device so that the database and its transaction logs can be stored in a physical location.
When the database is in use, SQL Server uses an online backup process to back up the database. The following list includes operations that cannot be performed during database or transaction log backup:
FILE Management is allowed during backup operations, such as the alter database statement with the add file or remove file option and the INSERT, UPDATE, or DELETE statement.

Shrink a database or file. This includes automatic contraction.
If the backup is started while these operations are in progress, the backup will be terminated. If you attempt to perform these operations while backing up, the Operation will fail.
As long as the operating system supports database sorting rules, backup operations can be performed between different platforms, even if these platforms use different processor types.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.