Repository: incubator-trafficcontrol
Updated Branches:
  refs/heads/master 33066e294 -> 14afdb407


updates db dump to custom format with compression and adds api


Project: http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/repo
Commit: 
http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/commit/3f72d3d0
Tree: 
http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/tree/3f72d3d0
Diff: 
http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/diff/3f72d3d0

Branch: refs/heads/master
Commit: 3f72d3d0a66f01e89b341c3de88def088e8090db
Parents: 33066e2
Author: Derek Gelinas <derek_geli...@cable.comcast.com>
Authored: Wed Apr 19 19:48:11 2017 -0400
Committer: Jeremy Mitchell <mitchell...@gmail.com>
Committed: Wed Apr 19 19:26:53 2017 -0600

----------------------------------------------------------------------
 traffic_ops/app/lib/API/Database.pm     | 71 ++++++++++++++++++++++++++++
 traffic_ops/app/lib/TrafficOpsRoutes.pm |  3 ++
 traffic_ops/app/lib/UI/GenDbDump.pm     | 11 +++--
 traffic_ops/app/lib/UI/Tools.pm         |  2 +-
 4 files changed, 82 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/blob/3f72d3d0/traffic_ops/app/lib/API/Database.pm
----------------------------------------------------------------------
diff --git a/traffic_ops/app/lib/API/Database.pm 
b/traffic_ops/app/lib/API/Database.pm
new file mode 100644
index 0000000..52a54ad
--- /dev/null
+++ b/traffic_ops/app/lib/API/Database.pm
@@ -0,0 +1,71 @@
+package API::Database;
+#
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+#
+#
+
+use Mojo::Base 'Mojolicious::Controller';
+use Data::Dumper;
+use UI::Utils;
+use IO::Compress::Gzip qw(gzip $GzipError);
+
+sub dbdump {
+       my $self = shift;
+       my $filename = $self->get_filename();
+
+       if ( !&is_admin($self) ) {
+               return $self->forbidden();
+       }
+
+       my ($db_name, $host, $port) = $Schema::dsn =~ 
/:database=([^;]*);host=([^;]+);port=(\d+)/;
+       my $db_user = $Schema::user;
+       my $db_pass = $Schema::pass;
+
+       my $ok = open my $fh, '-|', "PGPASSWORD=/\"$db_pass/\" pg_dump -b -Fc 
--no-owner -h $host -p $port -U $db_user";
+       if (! $ok ) {
+               $self->internal_server_error( { Error => "Error dumping 
database" } );  
+               return;
+       }
+
+       # slurp it in..
+       undef $/;
+       my $data = <$fh>;
+       my $zipdata;
+       gzip \$data => \$zipdata;
+
+
+       $self->res->headers->content_type("application/download");
+       $self->res->headers->content_disposition( "attachment; filename=\"" . 
$filename . "\"" );
+       $self->render( data => $zipdata );
+       close $fh;
+}
+
+sub get_filename {
+       my ( $sec, $min, $hour, $day, $month, $year ) = (localtime)[ 0, 1, 2, 
3, 4, 5 ];
+    $month = sprintf '%02d', $month + 1;
+    $day   = sprintf '%02d', $day;
+    $hour  = sprintf '%02d', $hour;
+    $min   = sprintf '%02d', $min;
+    $sec   = sprintf '%02d', $sec;
+    $year += 1900;
+    my $host = `hostname`;
+    chomp($host);
+
+    my $extension = ".dump.gz";
+    my $filename = "to-backup-" . $host . "-" . $year . $month . $day . $hour 
. $min . $sec . $extension;
+    return $filename;
+}
+
+1;

http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/blob/3f72d3d0/traffic_ops/app/lib/TrafficOpsRoutes.pm
----------------------------------------------------------------------
diff --git a/traffic_ops/app/lib/TrafficOpsRoutes.pm 
b/traffic_ops/app/lib/TrafficOpsRoutes.pm
index 05dbd1b..15aaac8 100644
--- a/traffic_ops/app/lib/TrafficOpsRoutes.pm
+++ b/traffic_ops/app/lib/TrafficOpsRoutes.pm
@@ -474,6 +474,9 @@ sub api_routes {
        $r->get("/api/$version/servers/#id/configfiles/ats/#filename")->over( 
authenticated => 1 )->to ( 'ApacheTrafficServer#get_server_config', namespace 
=> 'API::Configs' );
        $r->get("/api/$version/cdns/#id/configfiles/ats/#filename")->over( 
authenticated => 1 )->to ( 'ApacheTrafficServer#get_cdn_config', namespace => 
'API::Configs' );
 
+       # -- DB DUMP
+       $r->get("/api/$version/dbdump")->over( authenticated => 1 )->to( 
'Database#dbdump', namespace => $namespace );
+
        # -- DELIVERYSERVICES
        # -- DELIVERYSERVICES: CRUD
        $r->get("/api/$version/deliveryservices")->over( authenticated => 1 
)->to( 'Deliveryservice#index', namespace => $namespace );

http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/blob/3f72d3d0/traffic_ops/app/lib/UI/GenDbDump.pm
----------------------------------------------------------------------
diff --git a/traffic_ops/app/lib/UI/GenDbDump.pm 
b/traffic_ops/app/lib/UI/GenDbDump.pm
index 22509ef..5bc6ce9 100644
--- a/traffic_ops/app/lib/UI/GenDbDump.pm
+++ b/traffic_ops/app/lib/UI/GenDbDump.pm
@@ -18,12 +18,13 @@ package UI::GenDbDump;
 use Mojo::Base 'Mojolicious::Controller';
 use Data::Dumper;
 use UI::Utils;
+use IO::Compress::Gzip qw(gzip $GzipError);
 
 sub dbdump {
        my $self = shift;
        my $filename = $self->param('filename');
 
-       if ( !&is_oper($self) ) {
+       if ( !&is_admin($self) ) {
                $self->internal_server_error( { Error => "Insufficient 
permissions for DB Dump. Admin or Operations access is required." } );   
                return;
        }
@@ -31,9 +32,8 @@ sub dbdump {
        my ($db_name, $host, $port) = $Schema::dsn =~ 
/:database=([^;]*);host=([^;]+);port=(\d+)/;
        my $db_user = $Schema::user;
        my $db_pass = $Schema::pass;
-       my $uri = sprintf 'postgresql://%s:%s@%s:%s/%s', $db_user, $db_pass, 
$host, $port, $db_name;
 
-       my $ok = open my $fh, '-|', "pg_dump $uri -C --column-insert";
+       my $ok = open my $fh, '-|', "PGPASSWORD=/\"$db_pass/\" pg_dump -b -Fc 
--no-owner -h $host -p $port -U $db_user";
        if (! $ok ) {
                $self->internal_server_error( { Error => "Error dumping 
database" } );  
                return;
@@ -42,10 +42,13 @@ sub dbdump {
        # slurp it in..
        undef $/;
        my $data = <$fh>;
+       my $zipdata;
+       gzip \$data => \$zipdata;
+
 
        $self->res->headers->content_type("application/download");
        $self->res->headers->content_disposition( "attachment; filename=\"" . 
$filename . "\"" );
-       $self->render( data => $data );
+       $self->render( data => $zipdata );
        close $fh;
 }
 

http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/blob/3f72d3d0/traffic_ops/app/lib/UI/Tools.pm
----------------------------------------------------------------------
diff --git a/traffic_ops/app/lib/UI/Tools.pm b/traffic_ops/app/lib/UI/Tools.pm
index 8b385fb..199f8f6 100644
--- a/traffic_ops/app/lib/UI/Tools.pm
+++ b/traffic_ops/app/lib/UI/Tools.pm
@@ -153,7 +153,7 @@ sub db_dump {
     my $host = `hostname`;
     chomp($host);
 
-    my $extension = ".psql";
+    my $extension = ".dump.gz";
     my $filename = "to-backup-" . $host . "-" . $year . $month . $day . $hour 
. $min . $sec . $extension;
     $self->stash( filename => $filename );
     &stash_role($self);

Reply via email to