Skip to main content
Our Tech Ideas

Migrating SQL Server Using Backup and Restore: A Step-by-Step Guide

Migrating SQL Server Using Backup and Restore: A Step-by-Step Guide

Migrating an SQL Server from one server to another can be a daunting task. Whether you are upgrading to a newer version, moving to a different environment, or consolidating servers, it’s crucial to ensure that all necessary components are transferred correctly to avoid any disruptions. In this blog post, we will walk you through the comprehensive steps and considerations for a successful SQL Server migration.

Understanding What Needs to Be Migrated

When migrating an SQL Server, you need to ensure that the following key elements are moved:

  1. Databases:
    • User Databases: All the databases created by users.
    • System Databases: Master, model, msdb, and tempdb (note: these might need reconfiguration on the new server).
  2. Logins and Users:
    • SQL Server logins, ensuring their SIDs match to avoid orphaned users.
    • Server roles and user mappings.
  3. Jobs:
    • SQL Server Agent jobs, including their schedules and histories.
  4. Linked Servers:
    • Configurations for any linked servers.
  5. Configurations:
    • Server-wide configurations such as sp_configure settings and server roles.
    • Database-specific configurations and options.
  6. SQL Server Agent:
    • Alerts, operators, and other related settings.
  7. Replication Settings (if applicable):
    • Configuration of publisher, distributor, and subscriber settings.
  8. Full-Text Catalogs:
    • Full-text indexes and catalogs.
  9. Security:
    • Server-level permissions and endpoint configurations (Service Broker, Mirroring, etc.).
  10. Database Files:
    • Data files (.mdf) and log files (.ldf).
    • Filestream data, if used.
  11. Integration Services:
    • SSIS packages.
  12. Analysis Services and Reporting Services:
    • SSAS databases and configurations.
    • SSRS reports and configurations.
  13. Service Broker Objects (if used):
    • Configuration and objects related to Service Broker.
  14. Backup and Maintenance Plans:
    • Maintenance plans and related configurations.
  15. Custom Stored Procedures, Functions, and Triggers:
    • User-defined stored procedures, functions, and triggers in system databases.
  16. SQL Server Configuration Manager settings:
    • Network protocols and aliases.
  17. Extended Events and Profiler Templates:
    • Extended Events sessions and Profiler templates.
  18. Database Encryption Keys (if TDE is used):
    • Database encryption keys and certificates.
  19. Resource Governor Configuration (if used):
    • Resource pools and workload groups.
  20. Performance Data:
    • Performance data collection settings and data, if used.

Steps to Migrate SQL Server

1. Backup and Restore

Backup Databases

First, take full backups of all user databases and system databases on the source server.

BACKUP DATABASE [YourDatabase]
TO DISK = 'C:\Backups\YourDatabase.bak'
WITH FORMAT, INIT, SKIP, NOREWIND, NOUNLOAD, STATS = 10
SQL

Restore Databases

Restore the backups on the target server.

RESTORE DATABASE [YourDatabase]
FROM DISK = 'C:\Backups\YourDatabase.bak'
WITH MOVE 'YourDatabase_Data' TO 'D:\Data\YourDatabase.mdf',
     MOVE 'YourDatabase_Log' TO 'D:\Logs\YourDatabase.ldf',
     RECOVERY, STATS = 10
SQL

2. Script Out and Recreate

Logins and Users

Generate scripts for logins from the source server.

-- Script to transfer logins
EXEC sp_help_revlogin
SQL

Execute the generated script on the target server.

Jobs

Script out SQL Server Agent jobs and recreate them on the target server.

-- Example script for a job
USE msdb;
GO

EXEC sp_add_job
    @job_name = N'Your Job Name';
-- Add job steps, schedules, etc.
SQL

3. Data Copying

Use tools like the SQL Server Import and Export Wizard, Data Migration Assistant (DMA), or custom SSIS packages to copy data from the source server to the target server.

4. Testing

Before finalizing the migration, thoroughly test the new server:

  • Verify that all databases are accessible.
  • Ensure that logins and users can connect and have the correct permissions.
  • Test SQL Server Agent jobs to ensure they run as scheduled.
  • Check linked servers, full-text catalogs, and other configurations.
  • Run application tests to confirm that everything works as expected.

5. Cutover

Plan and execute the cutover to the new server:

  • Notify all relevant stakeholders of the planned downtime.
  • Perform a final backup of the source server.
  • Stop any applications or services using the source server.
  • Restore any final backups on the target server.
  • Redirect applications and services to the target server.

Additional Considerations

Performance Tuning

Post-migration, monitor the performance of the new server. Tweak configurations as needed to optimize performance.

Documentation

Document the entire migration process. Include details about configurations, changes made, and any issues encountered and resolved.

Rollback Plan

Always have a rollback plan in place in case something goes wrong during the migration. This plan should include steps to revert to the source server without data loss.

Conclusion

Migrating an SQL Server involves meticulous planning and execution. By following the steps outlined above and ensuring all necessary components are transferred, you can achieve a smooth transition with minimal disruption to your services. Remember, thorough testing and a well-documented rollback plan are key to a successful migration. Happy migrating!