How to copy databases automatically from one server to another in efficient way (not backup and restore) - sql-server

every once in a while I need to move a database from one server (QA) to another (production )
until now I used to perform backup on one server copy to the other server and then restore it...
is there a way to do it more efficiently?
maybe an automated job?
mirror? replication?
I'm searching for one-two clicks solution..
tnx.
I already made an automated script that does all of the following:
1)backup.
2)zip.
3)copy.
4)unzip.
5)restore.

Could you possible create an SSIS package (using the Transfer Database Task+ Transfer SQL Server Objects) that you can execute when you need it? You could create a 'template' package and use it for further use with just changing the connections.
See this blog post for some more details:
https://www.mssqltips.com/sqlservertip/2064/transfer-database-task-and-transfer-sql-server-objects-task-in-ssis/
From the link above:
Transfer Database Task: The Transfer Database Task is used to move a database to another SQL Server instance or create a copy on the same
instance (with different database name). This task works in two modes
[...]:
Offline : In this mode, the source database is detached from the source server after putting it in single user mode, copies of the mdf,
ndf and ldf files are moved to specified network location [...]
Online : In this mode, the task uses SMO to transfer the database objects to the destination server. In this mode, the database is
online during the copy and move operation, but it will take longer as
it has to copy each object from the database individually [...].
Transfer SQL Server Objects Task The Transfer SQL Sever Objects task is used to transfer one or more SQL Server objects to a different
database, either on the same or another SQL Server instance. This
allows you to select different types of objects you want to transfer.
You can select tables, views, stored procedures, user defined
functions etc. [...]
Not sure what it would take to make this package transferable among the different databases you may need to copy, but maybe it's worth exploring.
Good luck! :)

Related

Use Remote Database If Object Is Not Found

I am in search of a better solution, I'd like to make a database where there are some local tables/sprocs/views that should be used as the default. As a fall back use a remote database.
The setup I have right now is to copy the schema and/or data of the tables I don't want to change in the remote database. Then create a view for all the views and tables I want read-only access to. The sprocs are just a copy from the remote database as well. This cuts down on time copying all the data as well.
What I am wondering is if there is a way at a lower level to tell SQL Server to fall back on the remote database if an object is not found, then fail if the object is still not found?

Cleared SQL Server tables still retain some data

I made a custom application that is running from several years and is full of company data.
Now I need to replicate the application for another customer, so I set up a new server then i cloned the databases and empty all the tables.
Then I made a database and file shrink.
On the SQL Server side, the databases looks empty but if I run a grep search on the database files .mdf and .log I still can find recurrence of the previous company name also in system databases.
How do I really clean a SQL Server database?
Don't use backup/restore to clone a database for distribution to different clients. These commands copy data at the physical page/extent level, which may contain artifacts of deleted data, dropped objects, etc.
The best practice for this need is to create a new database with schema and system data from scratch using T-SQL scripts (ideally source controlled). If you don't already have these scripts, T-SQL scripts for schema/data can be generated from an existing database using the SMO API via .NET code or PowerShell. Here's the first answer I found with a search that uses the Microsoft.SqlServer.Management.SMO.Scripter class. Note you can include scripts data too (insert statements) by specifying the ScriptData scripting option for desired tables.

Persist Some Data Between Database Backup and Restore

I have a database in SQL Server 2008 (not R2)
A third party has the job of replacing the database regularly by restoring the data in the live environment from a .bak file made in the development environment. This leads to the destruction of any user generated data in that database. I am restricted in the live environment and cannot have two databases there.
One solution I am thinking about is to write a stored procedure that could save somehow the user generated data to some kind of local file and then once the development .bak is restored a second stored procedure could write this data back from the local file.
I'm familiar with using generate scripts that will generate a .sql file so maybe something similar to this, but it needs to be generated from a sql query that contains only the user generated data (these are specific rows of certain tables that are joined together - not the best design but it's what I have to work with).
Is it possible to generate a SQL script from a SQL query? Or is there some other kind of local file storage I could use. Something like a CSV file would be ok but I'm not aware of an easy way to automate restoring this. It will need to be restored with some very specific sql queries.

SQL Server database remote transfer - best method

I have two databases, one on a remote server the other local. (SQL Server 2008)
The database on my local server has the entire structure setup but no data. I would like to copy the data from the remote server to my server and I am wondering the best method in which to do this.
The main issue I am experiencing is the user that I have to the remote database has limited permissions. I cannot read the stored procedures, user defined functions so when I use Import/Export wizard I do not get the schema etc. So a regular dump/restore is not working for me as it restores the tables without the Primary Keys/Foreign Keys and the stored procedures.
I'd like to do this,
INSERT INTO localtable SELECT * FROM remotedb.table
I was having issues because of the IDENTITY fields and I had to explicitly name all of the columns. Also I am not sure if SQL Server Management Studio allows you to use two different databases, remote and local, so I was looking for any advice.
I have also tried applications like SQL FTP and Backup and it fails because it runs out of memory (I have 16GB of memory on the machine and the DB is like 4GB). I also can use the SQL Server import/export wizard but then I don't get the schema information. I also tried SQL Compare from Red Gate and it runs into issues with the permissions. Unfortunately I do not have the time to request and gain access to a new user so I was hoping someone had a creative idea.
You can definitely use SQL Server Backups for this. It will not run out of memory. If it does please tell us the message (because likely you are misinterpreting it). This is the fastest possible and the most complete solution.
You can tell the export wizard to also script the schema. It is hidden under "advanced" somewhere (terrible UI). But the script will be extremely big and I know of no way to execute it.
You can drop all schema objects except PKs in the target database. Then you can use remote queries to copy all the data over. You will not get any problems with foreign keys and identity columns if you drop the beforehand. After you are done you can recreate all those objects. It is probably best if you use a transaction for all of this because that way you get consistent source data from a point-in-time.

SQL Server - Schema only replication

Is there a way to replicate only schema (and all schema objects) without data between two SQL server instances?
For copying, rather than replicating, the simplest way would be to "Create scripts" for the database and run them on target server. This will create a new blank database on the new server.
Replicaton in SQL server implies that as you make changes to one schema they are automatically replicated on the other server. This can be done to some extent with SQL server replication, you just prevent he data being transferred but setting a criteria. I don't see how this would be of much use though.
I found this researching something else so I don't know if it is still an issue for you or not but there is an object in SSIS called Transfer SQL Server Objects. I haven't used it before but it has an option to copy data or not and you can select copy all objects or just specific types of objects, permissions etc.

Resources