frank1 has asked for the wisdom of the Perl Monks concerning the following question:

Am trying to backup a mysql database and replace the existing file. am using this module

MySQL::Backup

but its not working. am getting the following errors, tho i don't have much idea on it and its documentation is not clear

Use of uninitialized value $null in concatenation (.) or string at MySQL/Backup.pm line 103.: File name too long at db_backup.pl line 28.
#!/usr/bin/perl -wT use lib '.'; use strict; use warnings; use MySQL::Backup; use File::Basename; use CGI qw/:standard/; my $host = "162db.myhost.org"; my $user = "user"; my $pwd = "password"; my $database = "dbusers"; my $backup_folder = '/usr/home/ken/public_html/srcoc'; my $mb = new MySQL::Backup($database,$host,$user,$pwd,{'USE_REPLACE' = +> 0, 'SHOW_TABLE_NAMES' => 0}); $mb->create_structure(); my $backup_db = $mb->data_backup(); my $file = $backup_db; $file .= ".sql"; if ($backup_db) { open ( UPLOADFILE, "+>$backup_folder/$file" ) or die "$!"; binmode UPLOADFILE; while ( <UPLOADFILE> ) { print UPLOADFILE; } close UPLOADFILE; } else { exit 1; }

Replies are listed 'Best First'.
Re: backuping mysql
by marto (Cardinal) on Jul 03, 2024 at 13:20 UTC

    It doesn't look like you've understood the module documentation. Read what the method $mb->data_backup() does before you try and treat it as a file name.

      $mb->data_backup() returns a full DATA backup of current database

      i thought when i do like this

      my $backup_db = $mb->data_backup(); my $file = $backup_db; $file .= ".sql";

      i will be able to have the full backup file and give it extension and save it to my dir

Re: backuping mysql
by Anonymous Monk on Jul 03, 2024 at 16:05 UTC
    my $mb = new MySQL::Backup($database,$host,$user,$pwd,{'USE_REPLACE' = +> 0, 'SHOW_TABLE_NAMES' => 0}); $mb->create_structure(); my $backup_db = $mb->data_backup(); my $file = $backup_db; $file .= ".sql"; if ($backup_db) { open ( UPLOADFILE, "+>$backup_folder/$file" ) or die "$!"; binmode UPLOADFILE; while ( <UPLOADFILE> ) { print UPLOADFILE; } close UPLOADFILE; } else { exit 1; }

    This is not tested or anything but notice the differences:

    if (my $mb = new MySQL::Backup($database,$host,$user,$pwd,{'USE_REPLAC +E' => 0, 'SHOW_TABLE_NAMES' => 0})) { my $file = join '', 'db.backup.', time, '.sql'; if (my $backup_db = $mb->data_backup) { open my $fh, "+>", "$backup_folder/$file" or die "Can't open $back +up_folder/$file: $!"; print $fh $backup_db; close $fh; } else { die "Can't backup database!"; } else { die "Can't connect to database!"; }
      I was going to concatenate the filename but didn't like the number of dots:
      my $file = 'db.backup.' . time . '.sql';
      
      So I used join but forgot to clean up the dots:
      my $file = join '', 'db.backup.', time, '.sql';
      
      Still works the same but should be like this:
      my $file = join '.', 'db.backup', time, 'sql';
      

        or even:

        my $file = sprintf 'db.backup.%i.sql', time;

        🦛

      thank you, i got it working now by following your demo