The recommended way to run a backup is by scheduling Ola Halengren's maintenance scripts.
Consider your recovery options when deciding how frequently to backup Transaction Logs. More frequent Transaction Log backups reduce the potential amount of data lost in a recovery event. Backing up transaction logs every 60 seconds is viable (but do make sure you have scripted and tested recovery because the large number of backup files to process will make this cumbersome to do by hand). Also consider options like AlwaysOn Availability Groups.
If you need to take a transaction log backup to a different location (maybe you are hitting space constraints with the default backup location)...
BACKUP LOG [myDatabase]
TO DISK = N'E:\SQLBackups\myDatabase_Emergency.trn'
WITH
COMPRESSION,
CHECKSUM,
STATS = 5;
GO
To check backup history...
SELECT TOP (20)
bs.database_name,
bs.backup_start_date,
bs.backup_finish_date,
bs.type AS backup_type,
bs.backup_size / 1024.0 / 1024.0 AS backup_size_mb,
bmf.physical_device_name
FROM msdb.dbo.backupset AS bs
JOIN msdb.dbo.backupmediafamily AS bmf
ON bs.media_set_id = bmf.media_set_id
WHERE bs.database_name = N'myDatabase'
ORDER BY bs.backup_finish_date DESC;
GO
USE master;
GO
SELECT
d.name,
d.state_desc,
d.recovery_model_desc,
d.log_reuse_wait_desc,
mf.name AS logical_file_name,
mf.physical_name,
CAST(mf.size * 8.0 / 1024 AS decimal(18,2))
AS current_size_mb,
CASE
WHEN mf.max_size = -1
THEN 'Unlimited'
ELSE CAST(mf.max_size * 8.0 / 1024 AS varchar(30)) + ' MB'
END AS configured_max_size,
CASE
WHEN mf.is_percent_growth = 1
THEN CAST(mf.growth AS varchar(20)) + '%'
ELSE CAST(mf.growth * 8.0 / 1024 AS varchar(20)) + ' MB'
END AS configured_growth
FROM sys.databases AS d
JOIN sys.master_files AS mf
ON mf.database_id = d.database_id
WHERE d.name = N'myDatabase'
AND mf.type_desc = 'LOG';
GO
Pay particular attention to:
Available space on the log volume
Whether max_size has been reached
Whether autogrowth is disabled
Whether autogrowth is configured as a very small increment
Whether the log volume disappeared or became read-only
SELECT
name,
state_desc,
recovery_model_desc,
log_reuse_wait_desc
FROM sys.databases
WHERE name = N'myDatabase';
GO
USE [myDatabase];
GO
SELECT
total_log_size_in_bytes / 1024.0 / 1024.0 AS total_log_mb,
used_log_space_in_bytes / 1024.0 / 1024.0 AS used_log_mb,
used_log_space_in_percent
FROM sys.dm_db_log_space_usage;
GO
Check there is no backup executing...
SELECT
session_id,
command,
status,
percent_complete,
start_time,
estimated_completion_time
FROM sys.dm_exec_requests
WHERE command LIKE N'BACKUP%';
GO
Identify (and note) which database(s) is to blame...
use [master]
go
dbcc sqlperf(logspace);
Make a transaction log backup (to truncate the log)...
SSMS - SQL Server Agent - Job Activity Monitor - "DatabaseBackup - USER_DATABASES - LOG" - Start Job at Step...
Check for open transactions...
DBCC OPENTRAN (N'myDatabase')l
GO
Reclaim space...
use [myDatabase]
go
sp_helpfile
Work out an appropriate size to shrink the file to. This can be a case of educated guesswork. Your first aim is obviously to get the disk usage back under the alerting threshold (this site can be useful: http://www.calculator.net/percent-calculator.html). It is conceivably possible that this would be better achieved by shrinking several log files rather than just the largest log file. You may consider referring to the output from the dbcc sqlperf(logspace) command you ran earlier which will also have shown the % log space used. Refer to the output from before the backup as after a backup this % will be low (unless there is an active transaction which prevented the backup from freeing up space - which may warrant further investigation). It may be that a full transaction log destination should actually be resolved by adding storage. Certainly, if you have to reclaim space on a regular basis, you should consider increasing the disk size. If you can find no evidence to suggest otherwise, a size of 7000MB or 8000MB is often a good starting point. Be aware that making the log file too small will cause a performance hit as the log regrows back to its natural size.
use [myDatabase]
go
checkpoint;
go
dbcc shrinkfile(myDatabase_log, 8000);
go
Where 8000 is a size in MB (in this case ~8GiB)If the file has not shrunk repeat the backup and shrink steps again.
Make target size calculation more scientific...
http://dbadiaries.com/monitoring-sql-server-transaction-log-growth-using-dbcc-sqlperflogspaceTo identify the amount of free space in your transaction log...
use [myDatabase]
SELECT name,
size / 128.0 - CAST(FILEPROPERTY(name, 'SpaceUsed') AS INT) / 128.0 AS AvailableSpaceInMB
FROM sys.database_files;
ALTER DATABASE [myDatabase]
MODIFY FILE
(
NAME = N'myDatabase_log',
MAXSIZE = 51200MB,
FILEGROWTH = 1024MB
);
GO
This example changes MAXSIZE to 51200MB and FILEGROWTH to 1024MB
If the transaction log isn't truncating when you expect it to, use this script to help identify why...
Or use this...
SELECT
name,
state_desc,
recovery_model_desc,
log_reuse_wait_desc
FROM sys.databases
WHERE name IN
(
N'myDatabase',
N'otherDatabase'
);
GO
ALTER DATABASE myDatabase SET OFFLINE;
ALTER DATABASE myDatabase
MODIFY FILE (Name = myDatabase_Log, Filename = 'F:\NewDir\myDatabase.ldf')
Move the file in the filesystem
ALTER DATABASE myDatabase SET ONLINE;
RECONFIGURE
SELECT TOP (30)
bs.backup_start_date,
bs.backup_finish_date,
CASE bs.type
WHEN 'D' THEN 'Full'
WHEN 'I' THEN 'Differential'
WHEN 'L' THEN 'Transaction log'
ELSE bs.type
END AS backup_type,
CAST(bs.backup_size / 1024.0 / 1024.0 AS decimal(18,2))
AS backup_size_mb,
bmf.physical_device_name
FROM msdb.dbo.backupset AS bs
JOIN msdb.dbo.backupmediafamily AS bmf
ON bmf.media_set_id = bs.media_set_id
WHERE bs.database_name = N'myDatabase'
ORDER BY bs.backup_finish_date DESC;
GO