How to take Mysql Backup without using script? - mysql

Is it possible to take daily backup (Only the records per day) for particular table in DB.Once the backup is done need to delete those records from table.
Is this scenario will work without using scripting language like php,perl...?

the easiest way is to use script
1) select records you need
2) put them in some form of dump
3) run delete from table with parameters you need
other constructions (triggers with stored procedures or else), IMHO, will shoot you in leg eventually

mysqldump -u root -p db_name > db_backup.sql
using above command we can back up database and if you want to take the back up of a seleted table you can use : mysqldump -c -u -p db_name table_name > table_backup.sql
And to drop the db use drop database db-name and to drop a specific table use drop table table-name

Related

Backup restore mysql from query of single table

I have a database with three tables .
I want to create a backup file of some records in the second table :
mysqldump --opt --user=${USER} --password=${PASS} --databases ${DATABASE} --where="id = $1" mydb Table2 > FILE.sql
the problem is in the restore by using this code .
mysql --user=${USER} --password=${PASS} ${DATABASE} < FILE.sql
It deletes the entire database and inserts I only selected records from the previous code .
I wish only the selected records restore without deleting the rest .
You are using the [--databases][1] flag which is used when dumping several databases and should not be used when you want to dump only a single table.
Dump several databases. Normally, mysqldump treats the first name
argument on the command line as a database name and following names as
table names. With this option, it treats all name arguments as
database names. CREATE DATABASE and USE statements are included in the
output before each new database.
Avoid using that argument and for good measure use --no-create-info or simply -t instead to make sure create table / drop table commands are not in the dump file
You should try using the --skip-add-drop-table option when using mysqldump.
http://dev.mysql.com/doc/refman/5.5/en/mysqldump.html
This would prevent a drop table entry being added to the sql export.

Rails - Archive tables into another database

We have some tables that have huge number of records and which are not used often(e.g. user_activities) and we want to have ability to archive(I mean move) records from target table into archive table in separate database.
My question is: are there known solutions for that?
Additional explanation:
I'd like to have some kind of a rake task that would trigger archiving process. The process would go through tables marked as 'archived' (or whatever) and move outdated records to archive table in separate database.
Example: user_activities has 30 000 records. I mark the table as archived and set cutoff by id - last 2000 records. I expect the following results:
user_activities contains latest 2000 records only
28 000 outdated records have been moved to archived_user_activities table in my_super_cool_named_database
PS we use mysql2 adapter (if it helps)
Thank you!
There is the dump command and restore command I have shown below that work with the entire database.
copy the database:
mysqldump -u [uname] -p[pass] [dbname] > [backupfile.sql]
Use this method to rebuild a database from scratch:
$ mysql -u [username] -p [password] [database_to_restore] < [backupfile]
Use this method to import into an existing database (i.e. to restore a database that already exists):
$ mysqlimport [options] database textfile1
To restore your previously created custback.sql dump back to your 'Customers' MySQL database, you'd use:
$ mysqlimport -u sadmin -p pass21 Customers custback.sql
Although if you only want a specific part of the db you can do something like this...
CREATE TABLE db2.table LIKE db1.table;
INSERT INTO db2.table SELECT * FROM db1.table;

mysqlimport using dump

I need to restore a dumped database, but without discarding existing rows in tables.
To dump I use:
mysqldump -u root --password --databases mydatabase > C:\mydatabase.sql
To restore I do not use the mysql command, since it will discard all existing rows, but instead mysqlimport should do the trick, obviously. But how? Running:
mysqlimport -u root -p mydatabase c:\mydatabase.sql
says "table mydatabase.mydatabase does not exist". Why does it look for tables? How to restore dump with entire database without discarding existing rows in existing tables? I could dump single tables if mysqlimport wants it.
What to do?
If you are concerned with stomping over existing rows, you need to mysqldump it as follows:
MYSQLDUMP_OPTIONS="--no-create-info --skip-extended-insert"
mysqldump -uroot --ppassword ${MYSQLDUMP_OPTIONS} --databases mydatabase > C:\mydatabase.sql
This will do the following:
remove CREATE TABLE statements and use only INSERTs.
It will INSERT exactly one row at a time. This helps mitigate rows with duplicate keys
With the mysqldump performed in this manner, now you can import like this
mysql -uroot -p --force -Dtargetdb < c:\mydatabase.sql
Give it a Try !!!
WARNING : Dumping with --skip-extended-insert will make the mysqldump really big, but at least you can control each duplicate done one by one. This will also increase the length of time the reload of the mysqldump is done.
I would edit the mydatabase.sql file in a text editor, dropping the lines that reference dropping tables or deleting rows, then manually import the file normally using the mysql command as normal.
mysql -u username -p databasename < mydatabase.sql
The mysqlimport command is designed for dumps created with the mysql command SELECT INTO OUTFILE rather than direct database dumps.
This sounds like it is much more complicated than you are describing.
If you do a backup the way you describe, it has all the records in your database. Then you say that you do not want to delete existing rows from your database and load from the backup? Why? The reason why the backup file (the output from mysqldump) has the drop and create table commands is to ensure that you don't wind up with two copies of your data.
The right answer is to load the mysqldump output file using the mysql client. If you don't want to do that, you'll have to explain why to get a better answer.

Copying an existing table data from read only database into read-write database MySQL

I have two databases, for one I have only read access, and for the other i have read-write access. Let's call them A and B respectively.
There is a table in A that I need to copy(with its data and structure) into B.
To copy its structure I have used 'LIKE' keyword, but it did not work out. So, i could not copy the data also.
Do you guys have any further suggestions?
Thank you in advance.
To copy a table from one database to another, you'd do:
DROP TABLE IF EXISTS backup_db.table1;
CREATE TABLE backup_db.table1 SELECT * FROM live_db.table1;
Or you could use mysqldump
mysqldump -u root -p --opt dbname table1 > ~/export.sql
And to put it into the other database, you'd do:
mysql -u root -p dbname2 < ~/export.sql
Edit: Also, please note, if you had a master and slave setup, this would happen automatically. A slave is read-only while a master is read/write. Data from the master would automatically be copied to the slave, but you'd need two servers. Just for your information.
Take a look at mysqldump. It will allow you to backup the table data/structure to file from server A and restore it on server B.
mysqldump [options] db_name [tbl_name ...]

Mysql restore to restore structure and no data from a given backup (schema.sql)

Hi I use mysql administrator and have restored backup files (backup.sql). I would like to use restore the structure without data and it is not giving me an option to do so. I understand phpadmin provides this. I can not use this however. Any one can tell me an easy way?
Dump database structure only:
cat backup.sql | grep -v ^INSERT | mysql -u $USER -p
This will execute everything in the backup.sql file except the INSERT lines that would have populated the tables. After running this you should have your full table structure along with any stored procedures / views / etc. that were in the original databse, but your tables will all be empty.
You can change the ENGINE to BLACKHOLE in the dump using sed
cat backup.sql | sed 's/ENGINE=(MYISAM|INNODB)/ENGINE=BLACKHOLE/g' > backup2.sql
This engine will just "swallow" the INSERT statements and the tables will remain empty. Of course you must change the ENGINE again using:
ALTER TABLE `mytable` ENGINE=MYISAM;
IIRC the backup.sql files (if created by mysqldump) are just SQL commands in a text file. Just copy-paste all the "create ..." statements from the beginning of the file, but not the "insert" statements in to another file and "mysql < newfile" you should have the empty database without any data in it.
there is no way to tell the mysql client to skip the INSERT commands. the least-hassle way to do this is run the script as-is and let it load the data, then just TRUNCATE all of the tables.
you can write a script to do the following:
1 : import the dump into a new database.
2 : truncate all the tables with a loop.
3 : export the db again.
4 : now u just have the structure
You can backup you MYSQL database structure with
mysqldump -u username –p -d database_name > backup.sql
(You should not supply password at command line as it leads to security risks.MYSQL will ask for password by default.)