Learn about Centmin Mod LEMP Stack today
Become a Member

Auto Database Backup Script 1.0

Export and Upload your Database to Offsite location with Cron

  1. bojan horvatic

    bojan horvatic New Member

    6
    1
    3
    Feb 6, 2016
    Ratings:
    +1
    Local Time:
    11:45 AM
    damn that was fast
    thanks mate!

    however, now I'm getting this
    Code:
    Can't use an undefined value as a symbol reference at /usr/share/perl5/Net/FTP/dataconn.pm line 54.
    
    
    Hope you'll have the patience for this as well

     
  2. eva2000

    eva2000 Administrator Staff Member

    53,142
    12,110
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +18,645
    Local Time:
    7:45 PM
    Nginx 1.27.x
    MariaDB 10.x/11.4+
    might need @Matt's input as I don't realy work much with perl heh
     
  3. Matt

    Matt Well-Known Member

    925
    414
    63
    May 25, 2014
    Rotherham, UK
    Ratings:
    +669
    Local Time:
    10:45 AM
    1.5.15
    MariaDB 10.2
    Need to see the full content of your script to see why it's not working, because line 54 is the last one of the upload script.
     
  4. bojan horvatic

    bojan horvatic New Member

    6
    1
    3
    Feb 6, 2016
    Ratings:
    +1
    Local Time:
    11:45 AM
    Hi Matt

    Here it is, and thanks :)
    btw, I need it for the single database.
    Code:
    #!/usr/bin/perl -w
    # Z22SE.co.uk
    # Used in conjunction with dbbackup.sh
    # Uploads current database backup, and removes files from remote location more than $old
    use Net::FTP;
    use Date::Manip;
    
    # FTP PARAMETERS
    $ftp_backup = 1;
    $today = UnixDate("today","%Y-%m-%d");
    $old = UnixDate("5 days ago","%Y-%m-%d");
    $ftp_host = "xxxxxxxxxxx";
    $ftp_port = "21";
    $ftp_user = "xxxxxxxxx";
    $ftp_pwd = "xxxxxxx";
    $ftp_dir = "databases";
    $dir = "/home/nginx/domains/eliterehabplacement.com/backup";
    
    # Array of Database Files to Upload
    @files = ("$dir/wp11571376db_6278.$today.sql.bz2");
    
    # Array of Old Database Files to be Deleted
    @old_files = ("wp11571376db_6278.$old.sql.bz2");
    
    &backup();
    
    # UPLOADING BACKUP TO BACKUP SERVER
    sub backup {
    if($ftp_backup == 1)
    {
            my $ftp = Net::FTP->new($ftp_host, Port => $ftp_port, Debug => 0)
              or die "Cannot connect to server: $@";
    
            $ftp->login($ftp_user, $ftp_pwd)
              or die "Cannot login ", $ftp->message;
    
            $ftp->cwd($ftp_dir)
              or die "Can't CWD to remote FTP directory ", $ftp->message;
    
            $ftp->binary();
    
            foreach $file (@files) {
            $ftp->put($file)
              or warn "Upload failed ", $ftp->message;
            }
    
            foreach $oldfile (@old_files) {
            $ftp->delete($oldfile)
               or warn "Delete failed ", $ftp->message;
            }
    
            $ftp->quit();
    }
    }
    
    
     
    Last edited by a moderator: Nov 11, 2016
  5. eva2000

    eva2000 Administrator Staff Member

    53,142
    12,110
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +18,645
    Local Time:
    7:45 PM
    Nginx 1.27.x
    MariaDB 10.x/11.4+
  6. bojan horvatic

    bojan horvatic New Member

    6
    1
    3
    Feb 6, 2016
    Ratings:
    +1
    Local Time:
    11:45 AM
    you are right but I'll change the pass later
    and I think only my server's IP allowed
    at least that was the idea :D
     
  7. Matt

    Matt Well-Known Member

    925
    414
    63
    May 25, 2014
    Rotherham, UK
    Ratings:
    +669
    Local Time:
    10:45 AM
    1.5.15
    MariaDB 10.2
    This might be because you are only uploading a single file, rather than an array with multiple files.

    Try this version

    Code:
    #!/usr/bin/perl -w
    # Z22SE.co.uk
    # Used in conjunction with dbbackup.sh
    # Uploads current database backup, and removes files from remote location more than $old
    use Net::FTP;
    use Date::Manip;
    
    # FTP PARAMETERS
    $ftp_backup = 1;
    $today = UnixDate("today","%Y-%m-%d");
    $old = UnixDate("5 days ago","%Y-%m-%d");
    $ftp_host = "xxxxxxxxxxx";
    $ftp_port = "21";
    $ftp_user = "xxxxxxxxx";
    $ftp_pwd = "xxxxxxx";
    $ftp_dir = "databases";
    $dir = "/home/nginx/domains/eliterehabplacement.com/backup";
    
    # Array of Database Files to Upload
    $file = "$dir/wp11571376db_6278.$today.sql.bz2";
    
    # Array of Old Database Files to be Deleted
    $oldfile = "wp11571376db_6278.$old.sql.bz2";
    
    &backup();
    
    # UPLOADING BACKUP TO BACKUP SERVER
    sub backup {
    if($ftp_backup == 1)
    {
            my $ftp = Net::FTP->new($ftp_host, Port => $ftp_port, Debug => 0)
              or die "Cannot connect to server: $@";
    
            $ftp->login($ftp_user, $ftp_pwd)
              or die "Cannot login ", $ftp->message;
    
            $ftp->cwd($ftp_dir)
              or die "Can't CWD to remote FTP directory ", $ftp->message;
    
            $ftp->binary();
    
            $ftp->put($file)
              or warn "Upload failed ", $ftp->message;
    
            $ftp->delete($oldfile)
               or warn "Delete failed ", $ftp->message;
    
            $ftp->quit();
    }
    }
    
     
  8. bojan horvatic

    bojan horvatic New Member

    6
    1
    3
    Feb 6, 2016
    Ratings:
    +1
    Local Time:
    11:45 AM
    Thanks mate, I appreciate your help a lot
    Unfortunately it didn't work
    Perhaps it's something related to ovh backup thing

    got this message
    Code:
    Can't use an undefined value as a symbol reference at /usr/share/perl5/Net/FTP/dataconn.pm line 54.
    
     
  9. Matt

    Matt Well-Known Member

    925
    414
    63
    May 25, 2014
    Rotherham, UK
    Ratings:
    +669
    Local Time:
    10:45 AM
    1.5.15
    MariaDB 10.2
    Try changing this line:
    Code:
    my $ftp = Net::FTP->new($ftp_host, Port => $ftp_port, Debug => 0)
    to
    Code:
    my $ftp = Net::FTP->new($ftp_host, Port => $ftp_port, Debug => 0, Passive => 0)
    It's been a while since I last used this script.