Database backup and restoration, log cleanup code, and database backup logs

Source: Internet
Author: User

Database backup and restoration, log cleanup code, and database backup logs

-- The database is occupied and cannot be restored. Solution: -- 1. Set the database to work in single-user mode. -- Setting method: Right-click the database to be restored, right-click the menu and choose "properties"> "option"> "status"> "Restrict Access"> "Single ". This is the menu command of SQLSERVER2005. For other versions, find them by yourself. -- Database offline use masterGOALTER database dbset offline with rollback immediate -- database offline goalter database dbset online -- connection -- kill all processes using the database use masterGOdeclare @ dbname varchar (50) set @ dbname = 'db' declare @ SQL varchar (50) declare cs_result cursor local for select 'Kill '+ cast (spid as varchar (50) from sys. sysprocesses where db_name (dbid) = @ dbname open cs_resultfetch next from cs_result into @ sqlwhile @ fetch_status = 0 begin execute (@ SQL) fetch next from cs_result into @ sqlendclose cs_resultdeallocate cs_result -------------- data log cleanup ---------------------- Change Mode alter database db set recovery simplego -- shrink dbcc shrinkfile () -- 30 units are M, log File Size GO -- change the mode to FULL and then change it back. If it is simple, it is unnecessary to alter database db set recovery full.


 


The log of database 'new2552' is full. Back up the transaction log of the database to release some log space. What do you mean?

1. Clear logs
Dump transaction database name WITH NO_LOG
2. truncate transaction logs:
Backup log database name WITH NO_LOG
3. Compress database files (if not compressed, the database files will not be reduced
Enterprise Manager -- Right-click the database you want to compress -- all tasks -- contract database -- contract file
-- Select log file -- select to shrink to XXM in the contraction mode. Here, a minimum number of MB allowed to be shrunk is displayed. Enter this number directly and click OK.
-- Select data file -- select to shrink to XXM in the contraction mode. Here, a minimum number of MB allowed to be shrunk is displayed. Enter this number directly and click OK.
You can also use SQL statements to complete
-- Shrink Database
Dbcc shrinkdatabase (customer profile)
-- Contract the specified data file. 1 is the file number. You can use this statement to query: select * from sysfiles
Dbcc shrinkfile (1)
4. To minimize log files (for SQL 7.0, this step can only be performed in the query analyzer)
A. Separate the database:
Enterprise Manager -- server -- database -- Right-click -- detach Database
B. Delete LOG files in my computer
C. Additional database:
Enterprise Manager -- server -- database -- Right-click -- attach Database
This method generates a new LOG with a size of more than 500 K.
Or use the code:
The following example separates pubs and attaches a file in pubs to the current server.
A. Separation
E x e c sp_detach_db @ dbname = 'pubs'
B. Delete log files
C. Attach
E x e c sp_attach_single_file_db @ dbname = 'pubs ',
@ Physname = 'C: \ Program Files \ Microsoft SQL Server \ MSSQL \ Data \ pubs. mdf'
5. In order to automatically contract in the future, make the following settings:
Enterprise Manager -- server -- Right-click Database -- Property -- option -- select "auto contract"
-- SQL statement setting method:
E x e c sp_dboption 'database name', 'autowrite', 'true'
6. If you want to prevent the log from increasing too much in the future
Enterprise Manager -- server -- Right-click Database -- properties -- transaction log
-- Limit file growth to xM (x is the maximum data file size you allow)
-- SQL statement settings:
Alter database name modify file (name = logical file name, maxsize = 20)
Note:
Follow these steps. Do not follow these steps.
Otherwise, your database may be damaged.
Generally, steps 4 and 6 are not recommended.
Step 3 is not safe, and may damage the database or lose data
If the log reaches the upper limit in step 1, the subsequent database processing will fail and the log can be restored after being cleared.

In addition, we provide a simpler method, which I have tried and tried repeatedly. We recommend that you use it.
Simpler Method:
1. Right-built Database Properties window-fault recovery model-set to simple
2. Right-built database all tasks-shrink Database
3. Right-built Database Properties window-fault recovery model-set as large-capacity log records... remaining full text>

Sql2005 how to restore the database and clear junk log files

Alter procedure [dbo]. [backupdbname]
AS
BEGIN
-- Set nocount on added to prevent extra result sets from
-- Interfering with SELECT statements.
Set nocount on;
Declare @ SQL as nvarchar (4000)
Set @ SQL = 'del D: \ dbname. Bak'
Exec master .. xp_mongoshell @ SQL, no_output
Backup database dbname to disk = 'd: \ dbname. bak'
Set @ SQL = 'schtasks/run/TN "senddbname "'
-- Exec master .. xp_mongoshell @ SQL, no_output
END
The above is the backup storage process
Restore database [dbname] from disk = n'd: \ dbname. bak 'with file = 1, NOUNLOAD, STATS = 10
The preceding are restored SQL statements.
Backup log dbname WITH NO_LOG
Dump transaction dbname WITH NO_LOG
Dbcc shrinkfile (dbname_Log, 500)
The preceding statements are used to shrink the database.

If the database is in use and the file is large, shrinking is not necessarily useful.
You can back up the logs and the log files will be cleared automatically.
 

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.