SQL Transaction Log Full when running ShrinkFile SQL Server - sql-server

I am having an issue with DB Log Full and going through all the things trying to figure out why this is happening. I have of course implemented the backup here.
I run the backup commands for the shrink of the database log, the odd thing is that sometimes the script runs and at other times I get an error.
USE [MYDB];
BACKUP DATABASE [MYDB] TO DISK = 'L:\Backup\MYDB_DB_2023_01_05__T08_10_21.bak' ;
BACKUP LOG [MYDB] TO DISK = 'L:\Backup\MYDB_log_2023_01_05__T08_10_21.bak' WITH INIT;
ALTER DATABASE [MYDB]
SET RECOVERY SIMPLE;
DBCC SHRINKFILE(MY_Log, 3000);
ALTER DATABASE [MYDB]
SET RECOVERY FULL;
I have no idea why I am seeing the error message below since I am running this to backup / truncate the Log.
My DB Log size is set to 3GB & Autogrow with Limit 7GB, SQL Express version is 2017
Msg 3013, Level 16, State 1, Line 4
BACKUP DATABASE is terminating abnormally. [I don't know why this occurs]
Processed 894794 pages for database 'MYDB', file 'MY_Log' on file 1.
BACKUP LOG successfully processed 894794 pages in 97.732 seconds (71.528 MB/sec).
(1 row affected)
DBCC execution completed. If DBCC printed error messages, contact your system administrator.
(1 row affected)
Msg 9002, Level 17, State 2, Line 4
The transaction log for database 'MYDB' is full due to 'LOG_BACKUP'.
/ Yes I know the Log is full that is why I am running this script! //
EDIT: Comment Requested Info.

Related

How to recover a dropped table in sql server after a full backup taken

I have to recover a dropped table from a SQL Server database which runs on SQL Server 2016 Standard edition. Database is in full recovery mode.
After the dropped the table, I took a full database backup and then transaction log backup twice. Now can I recover my dropped table with or without using expensive third party tools?
I tried this link and getting error in the last command.
Result of the STOPBEFOREMARK query:
Processed 2104752 pages for database 'databasecopy', file 'databasefilename' on file 1.
Processed 6 pages for database 'datebasecopy', file 'database_log' on file 1.
RESTORE DATABASE successfully processed 2104758 pages in 123.259 seconds (133.405 MB/sec).
Msg 4335, Level 16, State 2, Line 15
The specified STOPAT time is too early. All or part of the database is already rolled forward beyond that point.
Msg 3013, Level 16, State 1, Line 15
RESTORE LOG is terminating abnormally.
RESTORE DATABASE successfully processed 0 pages in 0.544 seconds (0.000 MB/sec).
When I click the Restore option of the original database and click on Timeline option I get the below screen
Table dropped timing from fun_dblog:
Ok, to avoid getting into a long chat about this here is what I suggest:
Restore the full backup you took 4 months ago, NOT the one you took today:
RESTORE DATABASE [databasecopy] FROM DISK = N'OLD_BACKUP.bak' WITH MOVE N'database' TO N'C:\SQLskills\database2.mdf', MOVE N'database_log' TO N'C:\SQLskills\database2_log.ldf', REPLACE, NORECOVERY; GO
Then run the RESTORE up to that LSN:
RESTORE LOG [databasecopy] FROM DISK = N'D:\SQLskills\database_Log2.bak' WITH STOPBEFOREMARK = 'lsn:3420000002597000001', NORECOVERY;
GO
RESTORE DATABASE [databasecopy] WITH RECOVERY; GO
It will not work if you use your current Full Back up because everything in the log at that point has already been committed and you are trying to go back in time. Restore only goes forward in time. That is the reason for your error.

SQL Database in Asynchronous AO relication - Log file not shrining after backup

I have a database whose log file size is 527GB, showing almost 100% use. The DB is in AO Asynchronous replication with another SQL server. DB is in Full backup mode, and Log backup is happening every hour. I tried to shrink log file, it didn't work, gave me following message.
Msg 1468, Level 16, State 2, Line 2
The operation cannot be performed on database "MYDB" because it is involved in a database mirroring session or an availability group. Some operations are not allowed on a database that is participating in a database mirroring session or in an availability group.
Msg 5069, Level 16, State 1, Line 2
ALTER DATABASE statement failed.
The log for database 'MYDB' cannot be shrunk until all secondaries have moved past the point where the log was added..
(1 row(s) affected)
DBCC execution completed. If DBCC printed error messages, contact your system administrator.
I think its not doing it because of replication all logs are not transferred and its been all the time. How do solve the issue without taking Database out of AO replication group? I can move it to Synchronous mode, but I do not want it to move out of AO.
To check out the cause of your problem have a look at results of the query below (log_truncation_holdup_reason field):
select * from sys.dm_db_log_stats(db_id('YourDatabaseName'))
UPD: For versions lower than 2016 SP2 you can get the same using a query:
SELECT log_reuse_wait_desc
FROM sys.databases
WHERE name = 'YourDatabaseName'
If it is AVAILABILITY_REPLICA then check active transactions to understand which one could cause it.
dbcc opentran
Depending on results you will be able to decide what you should do.

I am trying to backup a database and am getting MSG 3013

I have tried to backup in Microsoft SSMS with the GUI backup task, and it fails after a few seconds, so then I tried running this command:
BACKUP DATABASE databasename TO DISK = 'd:\databasename_full.Bak' WITH FORMAT, MEDIANAME = 'd_datbasenamebackup', NAME = 'Full Backup of databasename';
And get a very generic error of the following
Msg 3013, Level 16, State 1, Line 1 BACKUP DATABASE is terminating abnormally.
I am wondering if anyone has come across this error before. Everything I have read is saying there is a media fault, which I know isn't the case.
There is a Microsoft support page for this Error Message 3013.
It is apparently caused when a filemark in your backup device could not be read. Resolution steps below:
To allow SQL Server to perform new backups to the backup device, you
must manually delete or erase the device by using the following
command:
BACKUP DATABASE mydatabase TO DISK='C:\MyDatabase.bak' with FORMAT

How to shrink a database in MS SQL Server if the drive is full?

I am having issue with SQL Server where I Can't execute any queried because the Hard Drive that hosts the databases is full.
I tried to shrink the database using the "Shrink" function in MS SMS but that did not work because the disk space is full.
How can I truncate the logs without crashing the database but also shrink the size?
I tried the following also
USE myDatabaseName;
GO
-- Truncate the log by changing the database recovery model to SIMPLE.
ALTER DATABASE myDatabaseName
SET RECOVERY SIMPLE;
GO
-- Shrink the truncated log file to 1 MB.
DBCC SHRINKFILE (myDatabaseName_Log, 1);
GO
-- Reset the database recovery model.
ALTER DATABASE myDatabaseName
SET RECOVERY FULL;
GO
but got the following error
Msg 3023, Level 16, State 3, Line 2
Backup and file manipulation operations (such as ALTER DATABASE ADD FILE) on a database must be serialized. Reissue the statement after the current backup or file manipulation operation is completed.
Msg 5069, Level 16, State 1, Line 2
ALTER DATABASE statement failed.
Thanks
SQL Server Pre 2012
BACKUP LOG LogName
WITH TRUNCATE_ONLY;
SQL Server 2012 onwards
The WITH_TRUNCATEONLY has been removed so you should switch the recovery model to Simple and revert to perform the same action.
ALTER DATABASE DatabaseName
SET RECOVERY SIMPLE;
After which, don't forget to re-instate your original recovery model!
Shrinking the log
Just because you've truncated the log this does not mean that the file size on disk has changed.
To reduce the file size of the log you need to issue the following command
DBCC SHRINKFILE (DatabaseLogLogicalName, 1);
Short answer:
BACKUP LOG WITH TRUNCATE_ONLY
Long answer: Backup your DB and read this first:
Managing Transaction Logs

SQL Server 2008 db shrink error

I have SQL Server 2008 R2 Standard edition with 3 tb data. My disc is full. I want to shrink the .mdf file. I've deleted many rows from my db. And I launch
DBCC SHRINKDATABASE(my_db, TRUNCATEONLY)
I got error
Msg 0, Level 11, State 0, Line 0
A severe error occurred on the current command. The results, if any, should be discarded.
I tried to do it in single user mode result is same. We have increased disk size to 4.5 tb and retried shrinking, result is same.
dbcc checkdb command shows no error on db.
Does anyone know what is problem?
Please help
Have you tried specifying both the file name and the target size in MB, e.g.
DBCC SHRINKFILE('filename', 20);
Or the TRUNCATEONLY option, which doesn't try any of the nasty stuff like reorganizing all of your physical data:
DBCC SHRINKFILE('filename', TRUNCATEONLY);
Also you could try making sure the database is in single user mode first:
ALTER DATABASE db SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
...and turn off any backup / log backup or other maintenance jobs that may be conflicting with your shrink operation.

Resources