Overwrite a Backup - sqlsitemapprovider

I need to set up a job to create backup everyday. I also need to overwrite an existing backup.
Can somebody please help me with it.
Thanks,

Have a look at mysqldump.
mysqldump db_name tbl_name > backupfile.sql
will dump the a db / table and overwrite backupfile.sql if it exists.
Use rsync or scp to copy it to another host if needed.

Related

Clone database content into an existing database

I have two AzureSQL databases, let's call them "A" and "B". During our deployment process I want to copy schema + data from A to B. How to do that with as little as possible code? Is there a built-in task or PowerShell command to do that?
I've found https://learn.microsoft.com/en-us/azure/sql-database/sql-database-copy but this command creates a new database as the target. I don't want that, the target already exists.
Your best option is to drop the database B.
DROP DATABASE [yourdatabaseB];
Then create a copy of A as B.
CREATE DATABASE yourdatabaseB
AS COPY OF yourdatabaseA ( SERVICE_OBJECTIVE = 'P2' )
Although you don't want that is the quickest way to do it.
Script CREATE statements for every table
Disable all foreign-keys
Populate all the tables using SSIS wizard
Re-enable foreign keys
DETAILS ...
https://richardbriansmith.wordpress.com/2018/10/10/copy-tables-to-new-database/
I would try to:
Restore database A to C
Drop database B
Rename C to B
Can't you automate the monitoring setup? You can probably copy, wait for the copy to complete and then rename the databases. Renaming should be small enough downtime?

What is the use of PURGE in DROP statement of Hive?

Hi Please describe the difference between both in hive with example.
DROP TABLE [IF EXISTS] table_name [PURGE];
If you don't use purge the table goes to a Trash directory, from there the table can be recovered after drop it. But if you do use purge table won't go to Trash directory, so it can't be recovered.
Regards !!

Mysqldump tables from different databases?

I want to backup two tables: table1 and table2.
table1 is from database database1.
table2 is from database database2.
Is there a way to dump them with a single mysqldump call?
I know I can do:
mysqldump -S unixSocket --skip-comments --default-character-set=utf8 --databases database1 --tables table1 > /tmp/file.sql
But how to dump two tables from different databases?
Use mysqldump twice but second time with redirect to file as append >> /tmp/file.sql.
There are three general ways to invoke mysqldump:
shell> mysqldump [options] db_name [tbl_name ...]
shell> mysqldump [options] --databases db_name ...
shell> mysqldump [options] --all-databases
Only the first one lets you select the database and table name, but doesn't allow multiple databases. If you use the second or third option you'll dump the selected databases (second) or all databases (third).
So, you can do it but you'll need to dump to entire databases.
As MichaƂ Powaga stated in the comments, you might also do it twice.
first time with "> /tmp/file.sql"
second time with ">> /tmp/file.sql to append"
The syntax is:
mysqldump --databases db_name1 [db_name2 ...] > my_databases.sql
Check for reference: http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html
Hope it helps
This might be a workaround but you could ignore the other tables you DON'T want to backup.
Such as in your case:
mysqldump --databases database1 database2 --ignore-table=database1.table2 --ignore-table=database2.table1
You need to define each table you DON'T want to dump with each --ignore-table statement.
Good luck!
For linux/bash, oneliner:
(mysqldump dbname1 --tables table1; mysqldump dbname2 --tables table2) | gzip > dump.sql.gz

Given a SQL Express database base file (.MDF) how can I wipe/clear/reset the schema and/or data?

Whats options do I have to clear the schema and data from a MDF file? What options to delete all the data?
To reset a databases schema, it seems I need to copy a file from a backup of the database when it was empty. I was wondering if there was a simpler or more efficient way.
To clear all data, it seems I'd need to write a script. The script would disable constraints, then drop all rows from each table before turning back on constraints. This is straightforward but does require I discover/track what tables exist in the database. Maybe its not sufficient or there is an easier approach?
I'm not sure what the point is of 'clearing the schema' - surely a new database already has a 'clear' schema.. BUT, you can create a new database in code via the following T-SQL:
USE Master
CREATE DATABASE NewDb (NAME=NewDbFile, FILENAME= '<filepath>')
If you need a file (an MDF) you can then detach the database too with sp_detach_db and you can then move it as required from the location specified above:
EXEC sp_detach_db NewDb
To clear the data you can use sp_msforeachtable with a truncation command - it is a non-logged operation, and does not check constraints, nor foreign keys - however, it cannot be rolled back!
EXEC sp_msforeachtable 'TRUNCATE TABLE ?'

Clean everything from database tables

I have some database with a lot tables , and i want to clean the data from tables how i can do it ?
like doing for loop on every table in the database DELETE FROM table_name;
Thanks.
Another alternative
use mysqldump to dump table schema only
use add-drop table
once you have the dump file, execute it
all the table will be drop and re-create
If you want to reset your auto increment,
then make changes to the dump file to reset it
if you are using linux, this can be done by a bash script, like
for table in $(mysql -N <<<"show tables from your_db")
do
mysql -N <<< "truncated $table"
done
One way is to create a script which will generate all the sql statements for you. Here is a version for linux:
echo "select concat('truncate ', table_name, ';') as '' from \
information_schema.tables where table_schema='YOURDATABASE';"\
|mysql -u USER -p > /tmp/truncate-all-tables.sql
The simplest way to do this is simply to issue a DELETE or TRUNCATE command on a per-table basis either from a .sql batch file or via a scripting language. (Whilst it's possible to interrogate the table schema information and use this, this may not be suitable depending on whether or not you have the required access privs and whether the tables in question follow a SELECTable naming convention.)
Incidentally, whilst you can use DELETE, this won't reclaim the used disk space and any auto_increment fields will remember the previous ID. (If you want to get the disk space back and reset the IDs to 1, use "TRUNCATE <table name>;".)