is it possible to migrate only one domain per time from a server A to a server B ?
or is it more practical and easy migrate by hand?
or is it more practical and easy migrate by hand?
#!/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]);
}
?>
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.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??