user3163275 user3163275 - 7 months ago 40
PHP Question

Mongodb php backup collections

I have a problem, my code does not work exactly as I want. I need to create a backup and restore. And that is my problem; My new collection is not identical.
Wrong data form:



My backup

$db = $m->$dbname;
try{
$backup1 = $db->people->find();
$text1=json_encode(iterator_to_array($backup1));
$people = 'people.txt';
if ($text1 ){
file_put_contents($people, $text1, LOCK_EX);
echo 'complete';
}
else echo 'error';


My restore

$db = $m->$dbname;
try{
$collection = $db->people1;
$file = file_get_contents('people.txt', FILE_USE_INCLUDE_PATH);
if ($file){
$file = json_decode($file);
foreach ($file as $id => $item) {
$collection->insert($item);
}
echo 'Restore complete';
}
else echo 'Error';

Answer

Use mongodump and mongorestoreBack Up and Restore with MongoDB Tools