single domain migration from server A to B

Status
Not open for further replies.

ad5ou

Active Member
Jun 12, 2018
892
204
43
There isn’t an easy “run this to export domain” option.
If you are good with sql queries, you could export domain specific settings filtered by domain uuid but you would also need to capture voicemails etc from file system.
 

Adrian Fretwell

Well-Known Member
Aug 13, 2017
1,498
413
83
You are welcome to play with this .php code that I wrote some time ago. I hacked it together when we thought we had a need to move some customers from one Fusion instance to another but the need went away, so I never finished or testing it properly. Nor did I write the import counterpart.

I think a tool to move domains from one box to another would be a valuable addition to our kit. I don't want to re-invent the wheel, maybe someone has alreday done this. It would be interesting to hear @markjcrane views on this. Maybe we could produce a domain import/export App?

I named the file backup-single-domain.php Play / Use at your own risk.

PHP:
#!/usr/bin/php
<?php

if ($argc < 2 )
{
    exit( "Usage: backup-single-domain.php <domain>\n   Eg: backup-single-domain.php customer1.mydomain.co.uk\n\n" );
}


$dbconn = pg_connect("host=127.0.0.1 port=5432 dbname=fusionpbx user=fusionpbx password=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");

if (!$dbconn) {
    echo "An error occurred connecting to the database.\n\n";
    exit(1);
}

$sql = "select * from v_domains where domain_name = '".$argv[1]."'";
$result = pg_query($dbconn, $sql);
if (!$result) {
    echo "An error occurred with the v_domains database query.\n\n";
    exit(1);
}
$rec_count = pg_num_rows($result);

if ($rec_count < 1) {
    echo "Error: Domain does not exist.\n\n";
    exit(1);
}

if ($rec_count > 1) {
    echo "Error: More than 1 Domain selected.\n\n";
    exit(1);
}

$domain = pg_fetch_row($result);
pg_free_result($result);
unset($sql);

$backup_base_dir = "/var/backups/fusionpbx/domains/";
$backup_date = date("Y-m-d");
$backup_dir = $backup_base_dir.$argv[1]."/".$backup_date."/";

if (!file_exists($backup_dir."tables")) {
    mkdir($backup_dir."tables", 0777, TRUE);
    chmod($backup_dir."tables", 0777);
    chown($backup_dir."tables", "www-data");
    chgrp($backup_dir."tables", "www-data");
}

if (!file_exists($backup_dir."recordings")) {
    mkdir($backup_dir."recordings", 0777, TRUE);
    chmod($backup_dir."recordings", 0777);
    chown($backup_dir."recordings", "www-data");
    chgrp($backup_dir."recordings", "www-data");
}

if (!file_exists($backup_dir."storage/fax")) {
    mkdir($backup_dir."storage/fax", 0777, TRUE);
    chmod($backup_dir."storage/fax", 0777);
    chown($backup_dir."storage/fax", "www-data");
    chgrp($backup_dir."storage/fax", "www-data");
}

if (!file_exists($backup_dir."storage/voicemail/default")) {
    mkdir($backup_dir."storage/voicemail/default", 0777, TRUE);
    chmod($backup_dir."storage/voicemail/default", 0777);
}

recursive_copy("/var/lib/freeswitch/recordings/".$argv[1], $backup_dir."recordings");
recursive_copy("/var/lib/freeswitch/storage/fax/".$argv[1], $backup_dir."storage/fax");
recursive_copy("/var/lib/freeswitch/storage/voicemail/default/".$argv[1], $backup_dir."storage/voicemail/default");

export_data($dbconn, $backup_dir, "v_bridges", $domain[0]);
export_data($dbconn, $backup_dir, "v_call_block", $domain[0]);
export_data($dbconn, $backup_dir, "v_call_broadcasts", $domain[0]);
export_data($dbconn, $backup_dir, "v_call_center_agents", $domain[0]);
export_data($dbconn, $backup_dir, "v_call_center_queues", $domain[0]);
export_data($dbconn, $backup_dir, "v_call_center_tiers", $domain[0]);
export_data($dbconn, $backup_dir, "v_call_flows", $domain[0]);
export_data($dbconn, $backup_dir, "v_call_recordings", $domain[0]);
export_data($dbconn, $backup_dir, "v_conference_centers", $domain[0]);
export_data($dbconn, $backup_dir, "v_conference_rooms", $domain[0]);
export_data($dbconn, $backup_dir, "v_conference_session_details", $domain[0]);
export_data($dbconn, $backup_dir, "v_conference_sessions", $domain[0]);
export_data($dbconn, $backup_dir, "v_conference_users", $domain[0]);
export_data($dbconn, $backup_dir, "v_conferences", $domain[0]);
export_data($dbconn, $backup_dir, "v_contact_addresses", $domain[0]);
export_data($dbconn, $backup_dir, "v_contact_emails", $domain[0]);
export_data($dbconn, $backup_dir, "v_contact_groups", $domain[0]);
export_data($dbconn, $backup_dir, "v_contact_notes", $domain[0]);
export_data($dbconn, $backup_dir, "v_contact_phones", $domain[0]);
export_data($dbconn, $backup_dir, "v_contact_relations", $domain[0]);
export_data($dbconn, $backup_dir, "v_contact_settings", $domain[0]);
export_data($dbconn, $backup_dir, "v_contact_times", $domain[0]);
export_data($dbconn, $backup_dir, "v_contact_urls", $domain[0]);
export_data($dbconn, $backup_dir, "v_contact_users", $domain[0]);
export_data($dbconn, $backup_dir, "v_contacts", $domain[0]);
export_data($dbconn, $backup_dir, "v_database_transactions", $domain[0]);
export_data($dbconn, $backup_dir, "v_destinations", $domain[0]);
export_data($dbconn, $backup_dir, "v_device_keys", $domain[0]);
export_data($dbconn, $backup_dir, "v_device_lines", $domain[0]);
export_data($dbconn, $backup_dir, "v_device_profiles", $domain[0]);
export_data($dbconn, $backup_dir, "v_device_settings", $domain[0]);
export_data($dbconn, $backup_dir, "v_devices", $domain[0]);
export_data($dbconn, $backup_dir, "v_dialplan_details", $domain[0]);
export_data($dbconn, $backup_dir, "v_dialplans", $domain[0]);
export_data($dbconn, $backup_dir, "v_domain_settings", $domain[0]);
export_data($dbconn, $backup_dir, "v_domains", $domain[0]);
export_data($dbconn, $backup_dir, "v_email_templates", $domain[0]);
export_data($dbconn, $backup_dir, "v_emails", $domain[0]);
export_data($dbconn, $backup_dir, "v_extension_users", $domain[0]);
export_data($dbconn, $backup_dir, "v_extensions", $domain[0]);
export_data($dbconn, $backup_dir, "v_fax", $domain[0]);
export_data($dbconn, $backup_dir, "v_fax_files", $domain[0]);
export_data($dbconn, $backup_dir, "v_fax_logs", $domain[0]);
export_data($dbconn, $backup_dir, "v_fax_users", $domain[0]);
export_data($dbconn, $backup_dir, "v_follow_me", $domain[0]);
export_data($dbconn, $backup_dir, "v_follow_me_destinations", $domain[0]);
export_data($dbconn, $backup_dir, "v_gateways", $domain[0]);
export_data($dbconn, $backup_dir, "v_group_permissions", $domain[0]);
export_data($dbconn, $backup_dir, "v_group_users", $domain[0]);
export_data($dbconn, $backup_dir, "v_groups", $domain[0]);
export_data($dbconn, $backup_dir, "v_ivr_menu_options", $domain[0]);
export_data($dbconn, $backup_dir, "v_ivr_menus", $domain[0]);
export_data($dbconn, $backup_dir, "v_meeting_users", $domain[0]);
export_data($dbconn, $backup_dir, "v_meetings", $domain[0]);
export_data($dbconn, $backup_dir, "v_music_on_hold", $domain[0]);
export_data($dbconn, $backup_dir, "v_phrase_details", $domain[0]);
export_data($dbconn, $backup_dir, "v_phrases", $domain[0]);
export_data($dbconn, $backup_dir, "v_pin_numbers", $domain[0]);
export_data($dbconn, $backup_dir, "v_recordings", $domain[0]);
export_data($dbconn, $backup_dir, "v_ring_group_destinations", $domain[0]);
export_data($dbconn, $backup_dir, "v_ring_group_users", $domain[0]);
export_data($dbconn, $backup_dir, "v_ring_groups", $domain[0]);
export_data($dbconn, $backup_dir, "v_services", $domain[0]);
export_data($dbconn, $backup_dir, "v_sip_profile_domains", $domain[0], "sip_profile_domain_uuid");
export_data($dbconn, $backup_dir, "v_streams", $domain[0]);
export_data($dbconn, $backup_dir, "v_user_settings", $domain[0]);
export_data($dbconn, $backup_dir, "v_users", $domain[0]);
export_data($dbconn, $backup_dir, "v_voicemail_destinations", $domain[0]);
export_data($dbconn, $backup_dir, "v_voicemail_greetings", $domain[0]);
export_data($dbconn, $backup_dir, "v_voicemail_messages", $domain[0]);
export_data($dbconn, $backup_dir, "v_voicemail_options", $domain[0]);
export_data($dbconn, $backup_dir, "v_voicemails", $domain[0]);
export_data($dbconn, $backup_dir, "v_xml_cdr", $domain[0]);


pg_close($dbconn);

exit(0);

function recursive_copy($src, $dst) {
    $dir = opendir($src);
    @mkdir($dst);
    while(false !== ( $file = readdir($dir)) ) {
        if (( $file != '.' ) && ( $file != '..' )) {
            if ( is_dir($src."/".$file) ) {
                recursive_copy($src."/".$file, $dst."/".$file);
            }
            else {
                copy($src."/".$file, $dst."/".$file);
        chown($dst."/".$file, "www-data");
        chgrp($dst."/".$file, "www-data");

            }
        }
    }
    closedir($dir);
}

function export_data($dbconn,  $_backup_dir, $_table, $_uuid, $_uuid_column = "domain_uuid") {
    $sql = "select count(*) from ".$_table." where ".$_uuid_column." = '".$_uuid."'";
    $result = pg_query($dbconn, $sql);
    $count = pg_fetch_row($result);
    pg_free_result($result);
    unset($sql);

    if ($count[0] > 0) {
        $sql = "copy (select * from ".$_table." where ".$_uuid_column." = '".$_uuid."') to '".$_backup_dir."tables/".$_table.".tsv'";
        pg_query($dbconn, $sql);
        unset($sql);
        echo "Table: ".$_table." ".$count[0]." records.\n";
    }
    return($count[0]);
}

?>
 
Last edited:

rnpsh19

New Member
Jan 27, 2019
23
2
3
44
I've had to do this a few times, you can export ext;s to csv then import it into the new site. Same with devices, I download the voicemail greetings, then recreate anything else, ring groups, queues, and IVR's. Not the fastest, but works.
 
  • Like
Reactions: JamesBorne

Adrian Fretwell

Well-Known Member
Aug 13, 2017
1,498
413
83
I am not aware of a complete solution, we have just managed using manual file copy (rsync) and manual DB extracts using the Postgesql copy command. I don't mind putting some time in to code a solution, but as I said in my post on Jan 3 2020, it would be nice to hear what @markjcrane has to say on the subject.
 

Adrian Fretwell

Well-Known Member
Aug 13, 2017
1,498
413
83
Cheers Adrian,

If Server A was a VM.. could you simply clone it into Server B and then delete the domains you don't need??
I'm sure you could, just in the same way you could export and import the whole database. What I am really interested in, is the ability to move a single domain from one live server to another live server without affecting any of the existing domains on either server.
 
  • Like
Reactions: Andyd358
Status
Not open for further replies.