FreePBX restore headaches

With the backup and restore module, every time I need to restore a backup (after reinstalling trixbox, and on the same machine), I need to go to each extension and hit the submit button. As well, I need to go to each ring group, day/night control, and follow me users and hit the submit button at the bottom. Then apply changes.

If I do not do all this, calls with either go straight to Voicemail, or, calls will just be dropped.

Is there something I do wrong?

Thanks

Unfortunately none of the thousands of users in the community have dug in and provided a patch to this common problem. The code is there that is suppose to restore the astdb objects which cause the problem, I’m not sure why it is not getting executed because if you run the pieces separately it all works. I noticed there were some errors but I don’t think any of them should have been causing the problem but feel free to try these out (always make backups first) and report back if it does in fact help:

/var/www/html/admin/modules/backup/bin/restoreastdb.php

Index: restoreastdb.php
===================================================================
--- restoreastdb.php  (revision 5598)
+++ restoreastdb.php  (working copy)
@@ -4,6 +4,19 @@

 global $argv;

+// Astdb trees that should be deleted before the restore
+//
+$deltree = array(
+ 'AMPUSER',
+ 'DEVICE',
+ 'CF',
+ 'CFB',
+ 'CFU',
+ 'CW',
+ 'DND',
+ 'DAYNIGHT',
+);
+
 function getconf($filename) {
         $file = file($filename);
         foreach ($file as $line) {
@@ -27,8 +40,15 @@
  // You must supply a single filename, which will be written to /tmp
  exit;
 }
-$dump = file_get_contents("/tmp/ampbackups.$argv[1]/astdb.dump");
+$dump = file_get_contents("/tmp/ampbackups.".$argv[1]."/astdb.dump");
 $arr = explode("\n", $dump);
+
+// Before restoring, let's clear out all of the current settings for the main objects
+//
+foreach ($deltree as $family) {
+ $astman->database_deltree($family);
+}
+
 foreach ($arr as $line) {
  $result = preg_match("/\[(.+)\] \[(.+)\]/", $line, $matches);
  / Now, the bad ones we know about are the ones that start with //, anything starting with SIP or IAX,

/var/www/html/admin/modules/backup/schedule_functions.php

Index: schedule_functions.php
===================================================================
--- schedule_functions.php  (revision 5598)
+++ schedule_functions.php  (working copy)
@@ -16,6 +16,7 @@
 {
  global $type;
  global $asterisk_conf;
+ global $amp_conf;
         if (is_dir($dir)) {
          if (($file!=".") && ($file!="..") && ($file!="")){
                  echo "<li><a class=\"info\" href=\"javascript:decision('Are you sure you want to delete this File Set?','config.php?type=$type&display=$display&action=delete
dataset&dir=$dir')\">";
@@ -25,7 +26,7 @@
                 if ($dh = opendir($dir)){
                         while (($file = readdir($dh)) !== false)
                         {
-                                if (($file!=".") && ($file!="..") && ($dir==$asterisk_conf['astvarlibdir']."/backups/"))
+                                if (($file!=".") && ($file!="..") && ($dir==$amp_conf['ASTVARLIBDIR']."/backups/"))
                                         echo "<li><a href=\"config.php?type=$type&display=$display&action=restore&dir=$dir/$file\">$file</a><br></li>";
                                 else if (($file!=".") && ($file!="..") )
                                         echo "<li><a href=\"config.php?type=$type&display=$display&action=restore&dir=$dir/$file&file=$file\">$file</a><br></li>";
@@ -82,11 +83,11 @@
  if($filetype=="ALL") {
    $Message="Restored All Files in BackupSet";
    $fileholder=substr($file, 0,-7);
-   exec('/bin/rm -rf /tmp/ampbackups.$fileholder');
+   exec("/bin/rm -rf /tmp/ampbackups.$fileholder");

    // First restore voicemial (for some reason if you do it all at once these don't get restored
    //
-   exec('/bin/rm -rf '.$asterisk_conf['astspooldir'].'/voicemail');
+   exec('/bin/rm -rf '.$amp_conf['ASTSPOOLDIR'].'/voicemail');
    $tar_cmd="tar -PxvOz -f \"$dir\" /tmp/ampbackups.$fileholder/voicemail.tar.gz | tar -Pxvz";
    exec($tar_cmd);

@@ -104,57 +105,57 @@
    $tar_cmd="tar -Pxvz -f \"$dir\" /tmp/ampbackups.$fileholder/asterisk.sql /tmp/ampbackups.$fileholder/asteriskcdr.sql /tmp/ampbackups.$fileholder/astdb.dump";
    exec($tar_cmd);

-   $sql_cmd="mysql -u $amp_conf[AMPDBUSER] -p$amp_conf[AMPDBPASS] < /tmp/ampbackups.$fileholder/asterisk.sql";
+   $sql_cmd="mysql -u ".$amp_conf['AMPDBUSER']." -p".$amp_conf['AMPDBPASS']." < /tmp/ampbackups.$fileholder/asterisk.sql";
    exec($sql_cmd);
-   $sql_cmd="mysql -u $amp_conf[AMPDBUSER] -p$amp_conf[AMPDBPASS] < /tmp/ampbackups.$fileholder/asteriskcdr.sql";
+   $sql_cmd="mysql -u ".$amp_conf['AMPDBUSER']." -p".$amp_conf['AMPDBPASS']." < /tmp/ampbackups.$fileholder/asteriskcdr.sql";
    exec($sql_cmd);
-   exec($asterisk_conf['astvarlibdir']."/bin/restoreastdb.php $fileholder");
-   exec('/bin/rm -rf /tmp/ampbackups.$fileholder');
+   exec($amp_conf['AMPBIN']."/restoreastdb.php $fileholder");
+   exec("/bin/rm -rf /tmp/ampbackups.$fileholder");
  } else if($filetype=="VoiceMail") {
    $Message="Restored VoiceMail";
    $fileholder=substr($file, 0,-7);
-   exec('/bin/rm -rf /tmp/ampbackups.$fileholder');
-   exec('/bin/rm -rf '.$asterisk_conf['astspooldir'].'/voicemail');
+   exec("/bin/rm -rf /tmp/ampbackups.$fileholder");
+   exec('/bin/rm -rf '.$amp_conf['ASTSPOOLDIR'].'/voicemail');
    $tar_cmd="tar -PxvOz -f \"$dir\" /tmp/ampbackups.$fileholder/voicemail.tar.gz | tar -Pxvz";
    exec($tar_cmd);
    exec('/bin/rm -rf /tmp/ampbackups.$fileholder');                                                                                                         
  } else if($filetype=="Recordings") {                                                                                                                       
    $Message="Restored System Recordings";                                                                                                                   
    $fileholder=substr($file, 0,-7);                                                                                                                         
-   exec('/bin/rm -rf /tmp/ampbackups.$fileholder');                                                                                                         
+   exec("/bin/rm -rf /tmp/ampbackups.$fileholder");                                                                                                         
    $tar_cmd="tar -PxvOz -f \"$dir\" /tmp/ampbackups.$fileholder/recordings.tar.gz | tar -Pxvz";                                                             
    exec($tar_cmd);                                                                                                                                          
-   exec('/bin/rm -rf /tmp/ampbackups.$fileholder');                                                                                                         
+   exec("/bin/rm -rf /tmp/ampbackups.$fileholder");                                                                                                         
  } else if($filetype=="Configurations"){                                                                                                                    
    $Message="Restored System Configuration";                                                                                                                
    $fileholder=substr($file, 0,-7);
-   exec('/bin/rm -rf /tmp/ampbackups.$fileholder');                                                                                                         
+   exec("/bin/rm -rf /tmp/ampbackups.$fileholder");                                                                                                         
    $tar_cmd="tar -PxvOz -f \"$dir\" /tmp/ampbackups.$fileholder/configurations.tar.gz | tar -Pxvz";                                                         
    exec($tar_cmd);                                                                                                                                          
    $tar_cmd="tar -Pxvz -f \"$dir\" /tmp/ampbackups.$fileholder/asterisk.sql /tmp/ampbackups.$fileholder/astdb.dump";                                        
    exec($tar_cmd);                                                                                                                                          
-   $sql_cmd="mysql -u $amp_conf[AMPDBUSER] -p$amp_conf[AMPDBPASS] < /tmp/ampbackups.$fileholder/asterisk.sql";                                              
+   $sql_cmd="mysql -u ".$amp_conf['AMPDBUSER']." -p".$amp_conf['AMPDBPASS']." < /tmp/ampbackups.$fileholder/asterisk.sql";                                  
    exec($sql_cmd);                                                                                                                                          
-   exec($asterisk_conf['astvarlibdir']."/bin/restoreastdb.php $fileholder");                                                                                
-   exec('/bin/rm -rf /tmp/ampbackups.$fileholder');                                                                                                         
+   exec($amp_conf['AMPBIN']."/restoreastdb.php $fileholder");                                                                                               
+   exec("/bin/rm -rf /tmp/ampbackups.$fileholder");                                                                                                         
  } else if($filetype=="FOP"){                                                                                                                               
    $Message="Restored Operator Panel";                                                                                                                      
    $fileholder=substr($file, 0,-7);                                                                                                                         
-   exec('/bin/rm -rf /tmp/ampbackups.$fileholder');                                                                                                         
+   exec("/bin/rm -rf /tmp/ampbackups.$fileholder");                                                                                                         
    $tar_cmd="tar -PxvOz -f \"$dir\" /tmp/ampbackups.$fileholder/fop.tar.gz | tar -Pxvz";                                                                    
    exec($tar_cmd);                                                                                                                                          
-   exec('/bin/rm -rf /tmp/ampbackups.$fileholder');                                                                                                         
+   exec("/bin/rm -rf /tmp/ampbackups.$fileholder");                                                                                                         
  } else if($filetype=="CDR"){                                                                                                                               
    $Message="Restored CDR logs";                                                                                                                            
    $fileholder=substr($file, 0,-7);                                                                                                                         
-   exec('/bin/rm -rf /tmp/ampbackups.$fileholder');                                                                                                         
+   exec("/bin/rm -rf /tmp/ampbackups.$fileholder");                                                                                                         
    $tar_cmd="tar -PxvOz -f \"$dir\" /tmp/ampbackups.$fileholder/cdr.tar.gz | tar -Pxvz";                                                                    
    exec($tar_cmd);                                                                                                                                          
    $tar_cmd="tar -Pxvz -f \"$dir\" /tmp/ampbackups.$fileholder/asteriskcdr.sql";                                                                            
    exec($tar_cmd);                                                                                                                                          
-   $sql_cmd="mysql -u $amp_conf[AMPDBUSER] -p$amp_conf[AMPDBPASS] < /tmp/ampbackups.$fileholder/asteriskcdr.sql";                                           
+   $sql_cmd="mysql -u ".$amp_conf['AMPDBUSER']." -p".$amp_conf['AMPDBPASS']." < /tmp/ampbackups.$fileholder/asteriskcdr.sql";                               
    exec($sql_cmd);                                                                                                                                          
-   exec('/bin/rm -rf /tmp/ampbackups.$fileholder');                                                                                                         
+   exec("/bin/rm -rf /tmp/ampbackups.$fileholder");                                                                                                         
  }                                                                                                                                                          
   return ($Message);                                                                                                                                        
 }                                                                                                                                                           

Thanks. What do you want me to do though? Copy and paste the code you gave me?
(sorry, I’m not so experienced with linux coding)

Thanks so much

those are patches that would need to be applied against the current code (my assumption is that it is at least version 2.3 - those were made from 2.4 but backup has not changed). You apply a patch by doing:

patch original_file patch_file

or if it doesn’t apply try:

patch --ignore-whitespace original_file patch_file

in case the copying and pasting messed with spaces/tabs. You would need to put each of those into a file which becomes your patch file.

Thanks. I’m going to look at it when I get a change and test it out. I’ll let you know how it worked out.

same problem
with freepbx 2.3.1.3 installed:

made backups of files to be patched.

copied and pasted first code section into file restoreastdb.patch
copied this file from my windows workstation to server in question.
cd /var/www/html/admin/modules/backup/bin
patch --ignore-whitespace ./restoreastdb.php ./restoredastdb.patch
patching file ./restoreastdb.php
Hunk #2 succeeded at 40 with fuzz 1.

cd …
copied and pasted second code section into file schedule_functions.patch
copied this file from my workstation to server
cd /var/www/html/admin/modules/backup
patch --ignore-whitespace ./schedule_functions.php ./schedule_functions.patch

results in:
patching file ./schedule_functions.php
Hunk #1 succeeded at 16 with fuzz 1.
missing header for unified diff at line 14 of patch
can’t find file to patch at input line 14
Perhaps you should have used the -p or --strip option?
The text leading up to this was:

dataset&dir=$dir’)">";

File to patch:

Opened patch file with nano. line wrapping from copy and paste… fixed that line and saved patch.

restored original schedule_functions.php from backup copy, and patch applied successfully.

Will try restoring in a bit and post back.

Tried restoring on a test machine after applying the patches. Results are the same as before…
“database show ampuser 255” is still not showing any callerID info.

the file astdb.dump has the information in it. Is there a way to manually restore this file, or feed it into the astdbrestore.php file?

you can explode the original tarball just like the script does so it is sitting under /tmp and then pass in the datetime part of the filename to restoreastdb.php to get it to restore the data. It would be nice one of these days if the people having problems with this could debug and determine why it is not restoring it though, because I can’t reproduce the problem on the systems I have tried - the variables always get restored. (But I know a lot of people run into this - just need to see what is different).

How do we explode the script?
Thanks

Philippe is correct, restoring just the database results in correct caller id information being put back into the astdb.
I wish I had a better understanding of PHP - I’d tear into it and try and figure out what’s not working.

We truly appreciate the work you put into FreePBX - get some training lined up in the west coast of the States and we’d gladly send a few people.

to “explode”:
find the backup file under var/lib/asterisk/backups/*.tar.gz
copy that file to the root of the drive. (I don’t recall how to extract to a set location…)
eg: cp 20080211.06.41.23.tar.gz /
cd /
tar -xvzf 20080211.06.41.23.tar.gz (should extract everything to /tmp/ampbackups.20080211.06.41.23)

now run the astdbrestore.php command with the date time of the backup.
eg:
/var/www/html/admin/modules/backup/bin/restoreastdb.php 20080211.06.41.23

now you can check the running asterisk db by getting into the asterisk console, and running “database show ampuser [ext]”

-spoke too soon. successful restore was on freepbx 2.4 b2 - can’t get a restore to work on production 2.3.1.3.

  • failure noted above was my fault. I was attempting a restore of astdb.dump that didn’t contain any of the fields needed… I went back to a backup a few days before attempting the first restore, and confirmed the contents of the astdb.dump file, and then restored that.
    Success!
    This is an acceptable work around for now.

kjohnsoncda,
off subject but we are planning our West Coast class right now. Feel free to comment under the 2.4.0 release blog your preference of when and where. We will be blogging to get that discussion going but input now would be valuable in the pre-planning stage.

I all,

What I saw, is that outbound CID was not restored properly

Removing " (double quotes character) from astdb.dump on outbound CID lines solved the restoreastdb.php problem for me.

Hope this could help.

Bruno,

This is an old thread. What version are you talking about?

Same issue observed from 2.7 to 2.9, if I remember well.

From 2.10, backup/restore module seems to have been completely rethink, and restoreastdb.php is not present anymore in my distro (how is now performed the astdb restore, and how can I reproduce it by command line ? Need to search more)
Unfortunatly, no cidname and no outboundcid found in running astdb after a restore from gui module
Of course, a call to the ‘magic hidden’ resetall action from devices page creates those entries, but how can we still do like if all was right ?