Archive old content data in different database How can i make archive my old content and save in different db with my cron job.

I want to store in different DB because i want to increase my site performance.

In drupal 7 what's the best way to do it ?
Here's what you can do:


Add your archive database to your settings.php below your default database.

$databases['my_other_db']['default'] = array(
  // Your secondary database's credentials here.
  // You will be able to explicitly connect to this database from your modules.
);

In your cron script, load all nodes you want to archive. In this example I retrieve all nodes older than a month and no more than 50 at a time.

$nodes_to_archive = array();

$query = db_select('node', 'n')
  -&gt;fields('node')
  -&gt;condition('type', 'your_content_type')
  -&gt;condition('created', REQUEST_TIME - (86400 * 30), '&lt;')
  -&gt;range(0, 50);
$result = $query-&gt;execute();

while($record = $result-&gt;fetchAssoc()) {
  $nodes_to_archive[] = $record;
}

In the same script, switch from database and add the nodes to the archive (make sure the second database has the same structure as the existing one)

db_set_active('my_other_db');

foreach ($nodes_to_archive as $node) {
  db_insert('node')
    -&gt;fields($node)
    -&gt;execute();
}

Still in the same script, switch back to the default database and remove the archived nodes.

db_set_active();

$nids = array();
foreach ($nodes_to_archive as $node) {
  $nids[] = $node-&gt;nid;
}

node_delete_multiple($nids);



I haven't tested any of this so you might need to improve it here and there. If anything's unclear, please ask.

Here is a list of helpfull resources:


How to connect to multiple databases within Drupal
Function db_select
Function db_insert
Function node_delete_multiple