How do I find the FamilyGUID of an exsting database When executing a RESTORE command, I understand that SQL Server ensures that an existing database is not being overwritten by a different database.  This check can be bypassed using REPLACE.

I understand that SQL Server uses the backup file's FamilyGUID to determine whether the database being restored is the same as the database being overwritten.  Is this correct? 

I understand that the FamilyGUID of the backup file can be determined using 

RESTORE headeronly FROM DISK = N'Q:\MyBackup.bak'


But how can I find the FamilyGUID of the database that is being overwritten?
You can use the catalog view - sys.database_recovery_status

SELECT DB_NAME(database_id) as DatabaseName, database_guid, family_guid
FROM master.sys.database_recovery_status


You can make your restore process more sophisticated by comparing the  family_guid of the backup file and the database to be restored.

Just dump the info of RESTORE headeronly into a temp table and use above query to compare. If they are different then there is a problem.