-- 1. Informacje o instancji SQL Server
SELECT
    @@SERVERNAME                AS ServerName,
    @@VERSION                   AS SQLVersion,
    SERVERPROPERTY('Edition')   AS Edition,
    SERVERPROPERTY('ProductVersion') AS ProductVersion,
    SERVERPROPERTY('ProductLevel')   AS ProductLevel,
    SERVERPROPERTY('EngineEdition')  AS EngineEdition,
    SERVERPROPERTY('IsClustered')    AS IsClustered,
    SERVERPROPERTY('MachineName')    AS MachineName,
    SERVERPROPERTY('InstanceName')   AS InstanceName,
    SERVERPROPERTY('Collation')      AS ServerCollation,
    SERVERPROPERTY('IsIntegratedSecurityOnly') AS WindowsAuthOnly;

-- 2. Informacje o maszynie
SELECT
    cpu_count,
    scheduler_count,
    hyperthread_ratio,
    physical_memory_kb/1024 AS PhysicalMemoryMB,
    sqlserver_start_time
FROM sys.dm_os_sys_info;

-- 3. Dyski
SELECT DISTINCT
       vs.volume_mount_point,
       vs.logical_volume_name,
       vs.total_bytes / 1024 / 1024 / 1024 AS TotalGB,
       vs.available_bytes / 1024 / 1024 / 1024 AS FreeGB
FROM sys.master_files mf
CROSS APPLY sys.dm_os_volume_stats
             (mf.database_id,mf.file_id) vs
ORDER BY vs.volume_mount_point;

-- 4. Wszystkie bazy danych
SELECT
    name,
    database_id,
    compatibility_level,
    recovery_model_desc,
    state_desc,
    create_date
FROM sys.databases
ORDER BY name;

-- 5. Rozmiary baz
SELECT
    DB_NAME(database_id) AS DatabaseName,
    SUM(size)*8/1024 AS SizeMB
FROM sys.master_files
GROUP BY database_id
ORDER BY SizeMB DESC;

-- 6. Pliki baz danych
SELECT
    DB_NAME(database_id) AS DatabaseName,
    name,
    type_desc,
    physical_name,
    size*8/1024 AS SizeMB
FROM sys.master_files
ORDER BY DatabaseName;

-- 7. SQL Logins
SELECT
    name,
    type_desc,
    default_database_name,
    create_date,
    modify_date
FROM sys.server_principals
WHERE type IN ('S','U','G')
ORDER BY name;


-- To jest jeden z najważniejszych elementów migracji.

-- 8. Linked Servers
EXEC sp_linkedservers;


-- Migracje często wybuchają właśnie przez brak linked serverów.
-- 9. SQL Agent Jobs
SELECT
    name,
    enabled,
    date_created
FROM msdb.dbo.sysjobs
ORDER BY name;

-- 10. Harmonogramy Jobów
SELECT
    j.name AS JobName,
    s.name AS ScheduleName,
    s.enabled
FROM msdb.dbo.sysjobs j
JOIN msdb.dbo.sysjobschedules js
    ON j.job_id = js.job_id
JOIN msdb.dbo.sysschedules s
    ON js.schedule_id = s.schedule_id;

-- 11. Database Mail
SELECT *
FROM msdb.dbo.sysmail_profile;

-- 12. Credentials
SELECT *
FROM sys.credentials;

-- 13. SQL Server Configuration
EXEC sp_configure;

-- 14. Service Accounts
SELECT
    servicename,
    startup_type_desc,
    status_desc,
    service_account
FROM sys.dm_server_services;


-- Usługi i konta serwisowe są regularnie wymieniane jako element wymagający migracji.
-- 15. Endpointy
SELECT *
FROM sys.endpoints;


-- Ważne dla Mirroring/AG.
-- 16. Linked Login Mappings
EXEC master.sys.sp_helplinkedsrvlogin;

-- 17. Certyfikaty
SELECT *
FROM sys.certificates;

-- 18. SQL Audit
SELECT *
FROM sys.server_audits;

SELECT *
FROM sys.server_audit_specifications;

-- 19. AlwaysOn
SELECT *
FROM sys.availability_groups;

SELECT *
FROM sys.availability_replicas;

SELECT *
FROM sys.dm_hadr_availability_replica_states;
/**
20. Co trzeba przenieść na nowy serwer

Minimalna lista migracyjna:
Bazy danych
SQL Logins (najlepiej sp_help_revlogin)
SQL Agent Jobs
Linked Servers
Linked Server Logins
Credentials
Database Mail
SQL Audits
Service Accounts
SSIS Packages
Maintenance Plans
Operators
Alerts
Endpoints
Certificates
TDE Keys
AlwaysOn Configuration
Backup Jobs
ODBC DSN (na Windows)
Scheduled Tasks
Konfiguracja antywirusa dla SQL
Konfiguracja backupów
Monitoring (Splunk/Dynatrace/SCOM/Zabbix)
*/

