Rails - Archive tables into another database - mysql

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;

Related

Migration of MYSQL database without losing records

I'm migrating a MYSQL DB from one host to another so I run the following command to backup the DB from the old hosting:
mysqldump -u **** -p **** | gzip > /home/***/***.sql.gz
And then use the following command to import the DB to the new host:
zcat /home/***/***.sql.gz | mysql -u *** -p ***
After successfully importing the DB, I point the domain to the new DNS.
The problem is that the website is active so new records are very likely to get inserted after the last backup. So, I may need to run the command once again after full DNS propagation.
So, my question, does the mysql command insert the new rows and update the existing ones or does it actually totally drop the tables and start over with the backup? If that happens, the records that have been inserted after DNS propagation might get lost!
Thanks
If you look at the output of mysqldump (before you gzip it) you will see that it contains a sequence of
DROP TABLE x;
CREATE TABLE x (...);
INSERT INTO x (...) VALUES (...);
So, no, it does not do an insert / replace, it drops and recreates the tables.

How to export mysql database through command line, but ignore some specific table

I have a large MySQL database its almost 434 tables. I would like the export the database but ignore or skip a couple of tables. This process should be done through command line, because i have more than 6 GB+ data in database. What is the proper syntax to export all tables, but ignore some specific of them?
mysqldump -u root -p database directory table1 table2 table2 > /var/www/mydb_tables.sql
This query is working fine, but its difficulty to mention all the 434+ table name. I want the query that skip only specific table and export remaining all table through command line.
You can use --ignore-table to skip certain tables.
mysqldump -u username -p database --ignore-table=database.table1 --ignore-table=database.table2 > /var/www/mydb_tables.sql

MySQL: Create consistent dump of audit tables

I have set up a system whose purpose it is to generate incremental dumps of our production data to our data warehouse. "Incremental" in this sense means that we can "synchronize" the production database with our data warehouse every minute or so without having to generate a full dump. Instead, we are just dumping and inserting the new/changed data.
On our replication save, I have set up a system where every relevant table of our production database has one insert TRIGGER and one update TRIGGER. These copy every row which is inserted or updated into an "audit table" in a different schema. This audit schema contains tables with the same structure as the production tables, but no indexes, and by using those TRIGGERs the audit tables will only contain the new or updated rows since the last export.
At the moment I'm using the mysql command line client to do the following for each of these audit tables:
LOCK TABLES table WRITE
SELECT * FROM table
DELETE FROM table
UNLOCK TABLES
I then pipe the output to some other scripts for further processing.
Now this works perfectly fine, however it creates the problem that while the state of every individual table will be consistent, the entire set of tables won't be. For example, if I have a clicks table and an impressions table and there is a 1 minute delay between dumping the former and the latter, the entire dump will be in a state which is inconsistent, obviously.
Now my question is: How do I do the following:
Lock ALL tables
Generate dumps for ALL tables
Delete all data from ALL tables
Unlock tables
I cannot use the mysql command line client because I cannot keep the lock across different sessions, and each table requires a new command. Also, I checked mysqldump which allows dumping multiple tables at a time, but I didn't find a way to delete all data from the tables before releasing the locks.
Any ideas?
To perform the first two points the command could be this one :
mysqldump --lock-all-tables -u root -p DATABASENAME > nameofdumpfile.sql
Since it is not possible to perform step 3 and 4 without releasing the lock, at least with mysqldump utility, why don't copy all the tables into another database (backup db) and then export the dump file from it ?
CREATE DATABASE backupdb;
USE originaldb;
FLUSH TABLES WITH READ LOCK;
keep this prompt(Prompt 1) open and then clone the database from another command prompt(Prompt 2) :
mysqldump --lock-all-tables -u root -p originaldb | mysql -u backup -p password backupdb;
Drop the original database from the Prompt 1 :
USE backupdb;
DROP DATABASE originaldb;
Then restore the empty database back with its original name (note the -d flag ) :
mysqldump --lock-all-tables -u root -p backupdb | mysql -u backup -p password originaldb;
This could be an example of a workaround that you can apply to achieve what you need.

How to take Mysql Backup without using script?

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

Restoring selective tables from an entire database dump?

I have a mysql dump created with mysqldump that holds all the tables in my database and all their data. However I only want to restore two tables. (lets call them kittens and kittens_votes)
How would I restore those two tables without restoring the entire database?
Well, you have three main options.
You can manually find the SQL statements in the file relating to the backed up tables and copy them manually. This has the advantage of being simple, but for large backups it's impractical.
Restore the database to a temporary database. Basically, create a new db, restore it to that db, and then copy the data from there to the old one. This will work well only if you're doing single database backups (If there's no CREATE DATABASE command(s) in the backup file).
Restore the database to a new database server, and copy from there. This works well if you take full server backups as opposed to single database backups.
Which one you choose will depend upon the exact situation (including how much data you have)...
You can parse out CREATE TABLE kittens|kitten_votes AND INSERT INTO ... using regexp, for example, and only execute these statements. As far as I know, there's no other way to "partially restore" from dump.
Open the .sql file and copy the insert statements for the tables you want.
create a new user with access to only those 2 tables. Now restore the DB with -f (force) option that will ignore the failed statements and execute only those statements it has permission to.
What you want is a "Single Table Restore"
http://hashmysql.org/wiki/Single_table_restore
A few options are outlined above ... However the one which worked for me was:
Create a new DB
$ mysql -u root -p CREATE DATABASE temp_db
Insert the .sql file ( the one with the desired table ) into the new DB
$ mysql -u root -p temp_db < ~/full/path/to/your_database_file.sql
dump the desired table
$ mysqldump -u root -p temp_db awesome_single_table > ~/awesome_single_table.sql
import desired table
$ mysql -u root -p original_database < ~/awesome_single_table.sql
Then delete the temp_db and you're all golden!