Avatar of Camillia
Camillia
Flag for United States of America asked on

Will I lose recovery of database if I run this command?

I issued this command and i want to know if we will lose point-in-time recovery if we do this OR can we recover data from the log file even if i run this:

DBCC SHRINKFILE(<TransactionLogName>, 1)

BACKUP LOG <DatabaseName> WITH TRUNCATE_ONLY

DBCC SHRINKFILE(<TransactionLogName>, 1)
Microsoft SQL Server 2005

Avatar of undefined
Last Comment
jogos

8/22/2022 - Mon
brad2575

No you should not, shrink only removes the extra white space from the file.

If you did a truncate it would remove all the data from the file.
Camillia

ASKER
I did this as well:
BACKUP LOG <DatabaseName> WITH TRUNCATE_ONLY

so, it didnt truncate the data?
jogos

Truncate only removes space at the end of the file .... if there is something still in use there nothing will be removed.

Check also the file-definitions of that logfile, if it has it's initial size it won't shrink either.

Why do you want to shrink?
What's size, it's initial size and the size of the datafile (.mdf and .ndf's if the exist) and the highest and size of the transaction logs.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Camillia

ASKER
>>Check also the file-definitions of that logfile
how do I do this?
Camillia

ASKER
>>What's size, it's initial size and the size of the datafile (.mdf and .ndf's if the exist) and the highest and size of the transaction logs.

LDF file.
Initial size was like 36 GB
I ran those commands and it went to 1024 KB. It's growing again but it's at 56MB something.

So, I didnt lose anything by running those 3 commands? only removed the space? I'm not a DBA and my manager is asking if we lost log data.
ASKER CERTIFIED SOLUTION
Scott Pletcher

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
jogos

And if you find the transaction log is growing to big... you can always reduce the time between the transaction log backups (if not recovery model Simple).

And shrinking your log to 1Mb means that after a not so great number of transactions your log file again has to grow. No a good idea... take a fair size.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Camillia

ASKER
so, the short answer is i ran the command and no log data is there anymore, correct?

What should i had done to backup the log first?
Scott Pletcher

>> so, the short answer is i ran the command and no log data is there anymore, correct? <<

Correct.  [That is my view at least, other posters seem to disagree.]
 

Instead, to keep the log contents:

BACKUP LOG <DatabaseName>
TO DISK = 'd:\path\to\backup\file\DatabaseName_YYYYMMDD_HHMM.TRN'


The default extension for a tran log backup is ".TRN".  Just for consistency with everyone else in the world :-), I suggest you use it too, although of course technically it can be anything you want.
Camillia

ASKER
ah, but i used "truncate_only". I should've backed it up, then truncated and shrinked? correct?
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Scott Pletcher

If you didn't want to lose any log activity, yes.

If the db is in FULL or BULK-LOGGED, SQL does not free log used space until either:
1) you backup the log  OR
2) you truncate the log.
SQL will not re-use/overwrite used log space until one of those two things is done.

Once you do either of those, SQL marks the used log space as unused and it can be re-used/overwritten by SQL.

So, once you issued the truncate command, all the used space was marked as unused.  Even if you take a log backup now, you won't get all that old activity, because it has already been marked as unused, and SQL only backs up used space during a log backup.
jogos

<<ah, but i used "truncate_only". I should've backed it up, then truncated and shrinked? correct?>>
Repeat "And if you find the transaction log is growing to big... you can always reduce the time between the transaction log backups (if not recovery model Simple).

And shrinking your log to 1Mb means that after a not so great number of transactions your log file again has to grow. No a good idea... take a fair size.
"

Check your recovery model of your db, if it is FULL then  you must always take transaction log backups. So you have ability to have a point in time restore and your logs relaese the used space (internal) so it does not have to grow on disk. http://msdn.microsoft.com/en-us/library/ms189275.aspx

Why do you want to truncate and shrink?
Scott Pletcher

jogos:

Do you retract your statement:
>> Truncate only removes space at the end of the file .... if there is something still in use there nothing will be removed. <<

Or is the affect of a log truncate still in dispute?
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Camillia

ASKER
>>Why do you want to truncate and shrink?

We didnt have enough disk space so I ran those commands.
jogos

<<We didnt have enough disk space so I ran those commands.>>
Your database actions need space in the logfile so trowing that space away is only temporary. And having to expand log file when short on space will cause one day or another the error 'unable to allocate space ...' and your transaction will be aborted and everybody will be stuck.. And expanding log file when disk nearly full is definitly generating disk fragmentation.

Do read and respond on suggestions
- what is your recovery model of your databases
- are you taking transaction log backups on databases with recovery model FULL
- what is size of database and size of your log file
- make a plan on how to get more diskspace (logfiles on other disk?)