Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MSSQL DataBase Backup without a specific table

I need to take a schedule backup without a specific table in sql. Because if I take a backup with that table it will take long time. I need to exclude one table from backup. Is it possible? Without that table all tables and data should be there in the database.

like image 767
Jude Nanayakkara Avatar asked Nov 19 '15 17:11

Jude Nanayakkara


People also ask

What is the command to take a partial backup?

productname = 'database'; Taking a partial database backup SQL Database is straightforward. We require to add READ_WRITE_FILEGROUPS argument in the backup database command. This option instructs SQL Server to initiate a partial filegroup backup for read-write filegroups.

What are the main 3 types of backups in SQL?

A backup of data in a complete database (a database backup), a partial database (a partial backup), or a set of data files or filegroups (a file backup).

Can you restore just one table from SQL backup?

You can't restore a single table directly from your backup to a database. You could restore your complete backup to new database and then copy your table from there to the desired database.


2 Answers

You can setup a separate file group for this one table, apart from the PRIMARY file group. This will give you the ability to create a backup that omits your large table. Below is an example that steps out the process.

1) Create a new file group for your database.

USE [master]
GO
ALTER DATABASE [EXAMPLEDB] ADD FILEGROUP [EXAMPLEFG1]
GO

2) Create an identical table with a slightly different name on the new file group.

CREATE TABLE [dbo].[example]
(
    [e] [int] NOT NULL
)
ON [EXAMPLEFG1]

GO

3) Insert records from original table into new table.

4) Delete the old table and correct the name of the new table to match the name of the old table.

5) Backup PRIMARY which now excludes the table that is now on file group "EXAMPLEFG1".

BACKUP DATABASE EXAMPLE
   FILEGROUP = 'PRIMARY',
   TO DISK = '<Your Directory>'
GO

If you decide to do a backup of EXAMPLEFG1 simply change the FILEGROUP value to "EXAMPLEFG1" in the above query.

Check out this Microsoft site for more info on filegroup backups.

Hope this helps!

like image 86
Mike Zalansky Avatar answered Oct 12 '22 20:10

Mike Zalansky


Mike's answer is almost correct, except that you don't need to create another table to move the data.

According to this answer, which I tested myself, after creating a filegroup, you just need to move the clustered index to the other filegroup. If you don't have a clustered index, you can create one just to help you in this process. If you also have nonclustered indexes, move them too. After you finish, all data will have been moved between filegroups automatically.

So, if you want to exclude this big table from your backup routine, do the following:

  1. Create a new file group.

    USE [master]  
    ALTER DATABASE [MyDatabase] ADD FILEGROUP [FG_MYBIGTABLE]
    
  2. Add a new file to this file group.

    ALTER DATABASE [MyDatabase]
    ADD FILE
    (
        name = MyDatabase_MyBigTable,
        filename = 'C:\DB_Files\MyDatabase_MyBigTable.ndf',
        size = 1024MB,
        maxsize = unlimited,
        filegrowth = 100MB
    ) 
    TO FILEGROUP FG_MYBIGTABLE
    
  3. Move the clustered index by creating another with DROP_EXISTING the old one.

    CREATE UNIQUE CLUSTERED INDEX CIX_MyBigTable
    ON MyDatabase.dbo.MyBigTable (ID)
    WITH DROP_EXISTING, ONLINE
    ON FG_MYBIGTABLE
    
  4. Move the other indexes.

    CREATE NONCLUSTERED INDEX [IX_OtherTable] 
    ON MyDatabase.dbo.MyBigTable (OtherTable_ID)
    WITH DROP_EXISTING, ONLINE
    ON FG_MYBIGTABLE
    
  5. Backup the database referencing the PRIMARY file group.

    BACKUP DATABASE MyDatabase
    FILEGROUP = 'PRIMARY',
    TO DISK = 'C:\Backups\MyBackup.bak'
    
like image 40
Zanon Avatar answered Oct 12 '22 20:10

Zanon