简体   繁体   中英

MDF and LDF SQL Server backups

In SQL Server Management Studio I can take database backups and save them as .bak files, which can also be restored in SQL Server Management Studio.

Is there any difference between doing this and setting up a script which backs up the .MDF and .LDF files - and if there was ever an issue, I could just re-attach the .MDF and .LDF files?

Thanks

It depends on your restore needs and backup space. It's certainly possible to to just reattach MDF and LDF files, but there are some limitations:

  • You have to detach the database first, make the backup (both files at the same time) and then reattach. This means downtime.
  • You cannot make incremental backups.
  • You cannot make differential backups.
  • You cannot do point-in-time restoration.
  • You basically have to make a full backup each time you copy the MDF and LDF files, which can really eat up space (thus, it can be better to do incremental or differential backups).
  • SQL Server has built-in mechanisms that can run without invoking external scripts to do regular backups. MDF and LDF backups require external scripts that have permission to access the data directory, the backup location and the server to attach/detach the database.

Basically, I'd say that unless you have a really good reason to not use the built-in backup functionality, I'd avoid doing manual backups of the MDF and LDF files.

Database backups are much more powerful than backing up the files.

First of all, if you backup the files while the database is in use, because it is changing constantly you may get something that works, or more likely you will get a corrupted file. A proper database backup coordinates ongoing changes with the backup process so that the backup file is perfectly consistent. A file backup may give you a file that has half of the changes in a transaction and not the other half, or,worse, half the changes in a particular page and not the other half.

Secondly, proper database backups let you recover the database to ANY point in time beginning at the oldest full backup, not just the point in time that the backup was made. (You will need the chain of all log backups made since the full backup to do this).

EDIT: note that as pointed out in the comments, the built-in functions don't necessarily provide point-in-time recovery--only if you use the type of backups that provide that functionality (though there are other reasons to use that type of backup even if you don't need point-in-time recovery).

The files generated by SQL Server backup only contains data not free, unused, space. The .mdf and .ndf files will contain (sometimes very large amounts of) empty, unused, space making these files larger than the backup files. And you have to detach the database from SQL Server to copy it out.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM