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 ?
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')
->fields('node')
->condition('type', 'your_content_type')
->condition('created', REQUEST_TIME - (86400 * 30), '<')
->range(0, 50);
$result = $query->execute();
while($record = $result->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')
->fields($node)
->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->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: