Skip to content

Instantly share code, notes, and snippets.

@kezenwa
Forked from ZEROF/ftp-backup.pl
Created April 3, 2023 05:50
Show Gist options
  • Save kezenwa/90fa7703e46baf475f2aea7b2a8fffc7 to your computer and use it in GitHub Desktop.
Save kezenwa/90fa7703e46baf475f2aea7b2a8fffc7 to your computer and use it in GitHub Desktop.
FTP Perl backup script for Debian 7
#!/usr/bin/perl -w
# ORGINAL FTP BACKUP SCRIPT V1.00
# Version 1.01 2nd hand coded BY ZEROF <zerof at backbox.org>
# This script was updated because some providers support only FTP passive mode (like online.net)
# And I needed Debian support as well (replaced path of gzip)
# SFTP support didn't work, need few lines of new code I guess, I didn't spend to much time on that part
# i just disabled, for now. If you have idea about fixing this, keep me posted.
# Is this best solution you will find? Nop, find some more secured scipt like duplicity
# COPYRIGHT 2013 - WEBHOSTINGHERO.COM
# Orginal script http://www.webhostinghero.com/ftp-backup-script-for-websites-and-mysql/
# DELETE BACKUP AFTER FTP UPLOAD (0 = no, 1 = yes)
$delete_backup = 1;
# ENTER THE PATH TO THE DIRECTORY YOU WANT TO BACKUP, NO TRAILING SLASH
$homedir = '/var/www/public_html';
# ENTER THE PATH TO THE LOCAL DIRECTORY YOU WISH TO SAVE THE BACKUP FILE TO, NO TRAILING SLASH
# LOCAL BACKUP DIRECTORY MUST BE OUTSIDE THE HOME DIRECTORY
$backup_dest_dir = '/var';
# FTP PARAMETERS
$ftp_backup = 1;
#SFTP removed, need to be fixed
#$use_sftp = 0;
$ftp_host = "somehost.com";
$ftp_port = 21; # FTP (default 21) OR SFTP (default 22) PORT
$ftp_user = "user";
$ftp_pwd = "password";
$ftp_dir = "/backup";
$ftp_debug = 0;
# BACKUP FILE NAME OPTIONS
($a,$d,$d,$day,$month,$yearoffset,$r,$u,$o) = localtime();
$year = 1900 + $yearoffset;
$homedir_archive = "homedir_backup-$day-$month-$year.tar.gz";
$full_backup_file = "full_site_backup-$day-$month-$year.tar.gz";
# MYSQL BACKUP PARAMETERS
$dbhost = 'localhost';
$dbuser = 'user';
$dbpwd = 'password';
$mysql_backup_file = "mysql_databases-$day-$month-$year.sql.gz";
$backup_all_databases = 'no'; # IF SET TO NO, SPECIFY INDIVIDUAL DATABASE NAME(S) BELOW
# ENTER DATABASE NAMES TO BACKUP SEPARATED BY SPACES (ONLY IF backup_all_databases IS SET TO 'no')
$database_names = 'name';
# SYSTEM COMMANDS
$cmd_mysqldump = '/usr/bin/mysqldump';
#Fixed for Debian 7
$cmd_gzip = '/bin/gzip';
# ----------- DO NOT EDIT BELOW THIS LINE -----------
# CHECK TO SEE IF DIRECTORY BACKUP EXISTS
if(!-d $homedir)
{
die "Invalid path name (directory_to_backup): $homedir";
}
# CHECK TO SEE IF LOCAL BACKUP DIRECTORY EXISTS
if(!-d $backup_dest_dir)
{
print "\nCreating local backup directory: $backup_dest_dir\n";
mkdir $backup_dest_dir or die "Error creating local backup directory (backup_dest_dir): $backup_dest_dir ";
}
# CURRENT DATE / TIME
($a,$d,$d,$day,$month,$yearoffset,$r,$u,$o) = localtime();
$year = 1900 + $yearoffset;
# BACKUP FILES
print "\nArchiving home directory ($homedir)\n";
$syscmd = "tar --exclude $backup_dest_dir" . "/* -czf $backup_dest_dir/$homedir_archive -C $homedir .";
system($syscmd);
# MYSQL DATABASE BACKUP
if($backup_all_databases eq 'yes')
{
print "\nCreating SQL dump of all databases";
$syscmd = "$cmd_mysqldump --host=$dbhost --user=$dbuser --password=$dbpwd --add-drop-table --all-databases -c -l | $cmd_gzip > $backup_dest_dir/$mysql_backup_file";
}
elsif(!$database_names eq "")
{
$syscmd = "$cmd_mysqldump --host=$dbhost --user=$dbuser --password=$dbpwd --add-drop-table --databases $database_names -c -l | $cmd_gzip > $backup_dest_dir/$mysql_backup_file";
}
system($syscmd);
# CREATING FULL SITE BACKUP FILE
print "\nCreating full backup archive\n";
if(-e "$backup_dest_dir/$mysql_backup_file") {
$syscmd = "tar -czf $backup_dest_dir/$full_backup_file -C $backup_dest_dir $homedir_archive $mysql_backup_file";
}
else {
$syscmd = "tar -C $backup_dest_dir -cvzf $backup_dest_dir/$full_backup_file $homedir_archive";
}
system($syscmd);
# DELETING SITE AND MYSQL BACKUP FILES
if(-e "$backup_dest_dir/$mysql_backup_file") {
unlink("$backup_dest_dir/$mysql_backup_file");
}
unlink("$backup_dest_dir/$homedir_archive");
# UPLOADING FULL SITE BACKUP TO REMOTE FTP SERVER / "This part was updated, because i wanted to enable ftp passive mod"
if($ftp_backup == 1)
{
if($use_sftp == 0)
{
use Net::FTP;
print "\nUploading backup to remote server using FTP\n";
my $ftp = Net::FTP->new($ftp_host, Debug => $ftp_debug, port => $ftp_port Passive => 1)
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("$backup_dest_dir/$full_backup_file")
or warn "Upload failed ", $ftp->message;
$ftp->quit();
}
else
{
#SFTP removed, need to be fixed
# print "\nUploading backup to remote server using SFTP\n";
# use Net::SFTP;
my %args = (
user => $ftp_user,
password => $ftp_pwd,
debug => $ftp_debug,
ssh_args => {
port => $ftp_port
}
);
my $sftp = Net::SFTP->new($ftp_host, %args);
$sftp->do_opendir($ftp_dir)
or die "Can't open remote directory $ftp_dir";
$sftp->put("$backup_dest_dir/$full_backup_file", "$ftp_dir/$full_backup_file")
or die("Upload failed");
}
print "\nUpload completed\n";
}
# DELETING FULL SITE BACKUP
if($delete_backup == 1)
{
print "\nDeleting local backup archive\n";
unlink("$backup_dest_dir/$full_backup_file");
}
print "\n";#!/usr/bin/perl -w
# SIMPLE FTP BACKUP SCRIPT V1.01
# Version 1.01 2nd hand code BY ZEROF <zerof at backbox.org>
# This script was edited because some providers support only passive mode and I needed Debian support.
# Is thi
# COPYRIGHT 2013 - WEBHOSTINGHERO.COM
# Orginal script http://www.webhostinghero.com/ftp-backup-script-for-websites-and-mysql/
# DELETE BACKUP AFTER FTP UPLOAD (0 = no, 1 = yes)
$delete_backup = 1;
# ENTER THE PATH TO THE DIRECTORY YOU WANT TO BACKUP, NO TRAILING SLASH
$homedir = '/var/www/public_html';
# ENTER THE PATH TO THE LOCAL DIRECTORY YOU WISH TO SAVE THE BACKUP FILE TO, NO TRAILING SLASH
# LOCAL BACKUP DIRECTORY MUST BE OUTSIDE THE HOME DIRECTORY
$backup_dest_dir = '/var';
# FTP PARAMETERS
$ftp_backup = 1;
#SFTP removed, need to be fixed
#$use_sftp = 0;
$ftp_host = "somehost.com";
$ftp_port = 21; # FTP (default 21) OR SFTP (default 22) PORT
$ftp_user = "user";
$ftp_pwd = "password";
$ftp_dir = "/backup";
$ftp_debug = 0;
# BACKUP FILE NAME OPTIONS
($a,$d,$d,$day,$month,$yearoffset,$r,$u,$o) = localtime();
$year = 1900 + $yearoffset;
$homedir_archive = "homedir_backup-$day-$month-$year.tar.gz";
$full_backup_file = "full_site_backup-$day-$month-$year.tar.gz";
# MYSQL BACKUP PARAMETERS
$dbhost = 'localhost';
$dbuser = 'user';
$dbpwd = 'password';
$mysql_backup_file = "mysql_databases-$day-$month-$year.sql.gz";
$backup_all_databases = 'no'; # IF SET TO NO, SPECIFY INDIVIDUAL DATABASE NAME(S) BELOW
# ENTER DATABASE NAMES TO BACKUP SEPARATED BY SPACES (ONLY IF backup_all_databases IS SET TO 'no')
$database_names = 'name';
# SYSTEM COMMANDS
$cmd_mysqldump = '/usr/bin/mysqldump';
#Fixed for Debian 7
$cmd_gzip = '/bin/gzip';
# ----------- DO NOT EDIT BELOW THIS LINE -----------
# CHECK TO SEE IF DIRECTORY BACKUP EXISTS
if(!-d $homedir)
{
die "Invalid path name (directory_to_backup): $homedir";
}
# CHECK TO SEE IF LOCAL BACKUP DIRECTORY EXISTS
if(!-d $backup_dest_dir)
{
print "\nCreating local backup directory: $backup_dest_dir\n";
mkdir $backup_dest_dir or die "Error creating local backup directory (backup_dest_dir): $backup_dest_dir ";
}
# CURRENT DATE / TIME
($a,$d,$d,$day,$month,$yearoffset,$r,$u,$o) = localtime();
$year = 1900 + $yearoffset;
# BACKUP FILES
print "\nArchiving home directory ($homedir)\n";
$syscmd = "tar --exclude $backup_dest_dir" . "/* -czf $backup_dest_dir/$homedir_archive -C $homedir .";
system($syscmd);
# MYSQL DATABASE BACKUP
if($backup_all_databases eq 'yes')
{
print "\nCreating SQL dump of all databases";
$syscmd = "$cmd_mysqldump --host=$dbhost --user=$dbuser --password=$dbpwd --add-drop-table --all-databases -c -l | $cmd_gzip > $backup_dest_dir/$mysql_backup_file";
}
elsif(!$database_names eq "")
{
$syscmd = "$cmd_mysqldump --host=$dbhost --user=$dbuser --password=$dbpwd --add-drop-table --databases $database_names -c -l | $cmd_gzip > $backup_dest_dir/$mysql_backup_file";
}
system($syscmd);
# CREATING FULL SITE BACKUP FILE
print "\nCreating full backup archive\n";
if(-e "$backup_dest_dir/$mysql_backup_file") {
$syscmd = "tar -czf $backup_dest_dir/$full_backup_file -C $backup_dest_dir $homedir_archive $mysql_backup_file";
}
else {
$syscmd = "tar -C $backup_dest_dir -cvzf $backup_dest_dir/$full_backup_file $homedir_archive";
}
system($syscmd);
# DELETING SITE AND MYSQL BACKUP FILES
if(-e "$backup_dest_dir/$mysql_backup_file") {
unlink("$backup_dest_dir/$mysql_backup_file");
}
unlink("$backup_dest_dir/$homedir_archive");
# UPLOADING FULL SITE BACKUP TO REMOTE FTP SERVER
if($ftp_backup == 1)
{
if($use_sftp == 0)
{
use Net::FTP;
print "\nUploading backup to remote server using FTP\n";
my $ftp = Net::FTP->new($ftp_host, Debug => $ftp_debug, port => $ftp_port Passive => 1)
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("$backup_dest_dir/$full_backup_file")
or warn "Upload failed ", $ftp->message;
$ftp->quit();
}
else
{
#SFTP removed, need to be fixed
# print "\nUploading backup to remote server using SFTP\n";
# use Net::SFTP;
my %args = (
user => $ftp_user,
password => $ftp_pwd,
debug => $ftp_debug,
ssh_args => {
port => $ftp_port
}
);
my $sftp = Net::SFTP->new($ftp_host, %args);
$sftp->do_opendir($ftp_dir)
or die "Can't open remote directory $ftp_dir";
$sftp->put("$backup_dest_dir/$full_backup_file", "$ftp_dir/$full_backup_file")
or die("Upload failed");
}
print "\nUpload completed\n";
}
# DELETING FULL SITE BACKUP
if($delete_backup == 1)
{
print "\nDeleting local backup archive\n";
unlink("$backup_dest_dir/$full_backup_file");
}
print "\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment