Beyond Full Backups: Exploring Differential & Transaction Log Backups

2024-07-27

Backing Up SQL Server Databases with Command Line Scripts

We can achieve this using the sqlcmd utility and the BACKUP DATABASE Transact-SQL (T-SQL) statement. Here's an example script with explanations:

sqlcmd -S SERVERNAME -U USERNAME -P PASSWORD -Q "BACKUP DATABASE DATABASE_NAME TO DISK = 'BACKUP_LOCATION\DATABASE_NAME_BACKUP.bak'"

Explanation:

  • sqlcmd: This is the command-line tool used to execute T-SQL statements on SQL Server.
  • -S SERVERNAME: This specifies the name of the SQL Server instance you want to connect to.
  • -U USERNAME: This specifies the username with access to the database.
  • -P PASSWORD: This specifies the password for the provided username.
  • -Q: This tells sqlcmd to execute the following quoted string as a T-SQL statement.
  • "BACKUP DATABASE DATABASE_NAME TO DISK = 'BACKUP_LOCATION\DATABASE_NAME_BACKUP.bak'": This is the T-SQL statement that performs the backup.

Breakdown of the T-SQL statement:

  • BACKUP DATABASE: This keyword initiates the backup operation.
  • DATABASE_NAME: This is the name of the database you want to back up.
  • TO DISK: This specifies that the backup will be stored as a file.
  • 'BACKUP_LOCATION\DATABASE_NAME_BACKUP.bak': This defines the path and filename for the backup file. Replace "BACKUP_LOCATION" with the actual folder where you want to store the backup, and modify "DATABASE_NAME_BACKUP.bak" to your desired filename extension (typically ".bak").

Examples:

  • To backup the "master" database to C:\Backups\master_backup.bak:
sqlcmd -S SERVERNAME -U USERNAME -P PASSWORD -Q "BACKUP DATABASE master TO DISK = 'C:\Backups\master_backup.bak'"
  • To backup the "AdventureWorks2019" database with a timestamp to D:\DailyBackups:
sqlcmd -S SERVERNAME -U USERNAME -P PASSWORD -Q "BACKUP DATABASE AdventureWorks2019 TO DISK = 'D:\DailyBackups\AdventureWorks2019_%date:~-10,2%%date:~-7,2%%date:~-4,4%.bak'"

This example uses environment variables to automatically append the current date to the backup filename, ensuring unique backups each day.

Related Issues and Solutions:

  • Permissions: Ensure the user you specify in the script has appropriate permissions to access the database and create files in the backup location.
  • Scheduling: While this script allows manual backup execution, consider scheduling regular backups using Windows Task Scheduler or SQL Server Agent for automated backups.
  • Backup Types: This example demonstrates a full database backup. You can explore other backup options like differential or transaction log backups for specific scenarios using the WITH clause in the BACKUP DATABASE statement.

Remember to replace placeholders like SERVERNAME, USERNAME, PASSWORD, DATABASE_NAME, and BACKUP_LOCATION with your actual values before running the script.


sql-server command-line scripting



Locking vs Optimistic Concurrency Control: Strategies for Concurrent Edits in SQL Server

Collision: If two users try to update the same record simultaneously, their changes might conflict.Solutions:Additional Techniques:...


Reordering Columns in SQL Server: Understanding the Limitations and Alternatives

Workarounds exist: There are ways to achieve a similar outcome, but they involve more steps:Workarounds exist: There are ways to achieve a similar outcome...


Unit Testing Persistence in SQL Server: Mocking vs. Database Testing Libraries

TDD (Test-Driven Development) is a software development approach where you write the test cases first, then write the minimum amount of code needed to make those tests pass...


Taming the Hash: Effective Techniques for Converting HashBytes to Human-Readable Format in SQL Server

In SQL Server, the HashBytes function generates a fixed-length hash value (a unique string) from a given input string.This hash value is often used for data integrity checks (verifying data hasn't been tampered with) or password storage (storing passwords securely without the original value)...


Alternative Methods for Splitting Delimited Strings in SQL

Understanding the Problem:A delimited string is a string where individual items are separated by a specific character (delimiter). For example...



sql server command line scripting

Keeping Watch: Effective Methods for Tracking Updates in SQL Server Tables

This built-in feature tracks changes to specific tables. It records information about each modified row, including the type of change (insert


Bridging the Gap: Transferring Data Between SQL Server and MySQL

SSIS is a powerful tool for Extract, Transform, and Load (ETL) operations. It allows you to create a workflow to extract data from one source


Taming the Tide of Change: Version Control Strategies for Your SQL Server Database

Version control systems (VCS) like Subversion (SVN) are essential for managing changes to code. They track modifications


Can't Upgrade SQL Server 6.5 Directly? Here's How to Migrate Your Data

Outdated Technology: SQL Server 6.5 was released in 1998. Since then, there have been significant advancements in database technology and security


Replacing Records in SQL Server 2005: Alternative Approaches to MySQL REPLACE INTO

SQL Server 2005 doesn't have a direct equivalent to REPLACE INTO. You need to achieve similar behavior using a two-step process: