📄 mysql_backup.cgi
字号:
# Upload the file
$ftp->put("$backup_gzip_file","$upload_gzip_filename") or
&error_message(qq~Error! Net::FTP couldn't upload $backup_gzip_file at $ftp_host : $!\n~);
$print_text = "\nListing file $upload_gzip_filename at $ftp_host\n";
$body_text .= $print_text;
print $print_text;
# List file to see if the file uploaded
@lines = $ftp->dir("$upload_gzip_filename") or
&error_message(qq~Error! Net::FTP couldn't list $upload_gzip_filename at $ftp_host : $!\n~);
$dir_print_text = "\nDIR: @lines\n";
$body_text .= $dir_print_text;
print $dir_print_text;
$print_text = "\nDisconnecting from ftp site: $ftp_host\n";
$body_text .= $print_text;
print $print_text;
# Disconnect
$ftp->quit() or
&error_message(qq~Error! Net::FTP couldn't disconnect from $ftp_host : $!\n~);
# do final check on uploaded file
if ( "$dir_print_text" !~ /$upload_gzip_filename/ )
{
&error_message(qq~Error! Net::FTP failed to Upload $upload_gzip_filename at $ftp_host : $!\n~);
}
else
{
$print_text = "\nUploaded File $upload_gzip_filename Matched at ftp site: $ftp_host\n";
$body_text .= $print_text;
print $print_text;
}
}
# now email admin notification of backup, with attached file option
#............................................................................
if ( $email_backup eq 'yes' )
{
$print_text = "\nEmailing GZip File.\n";
$body_text .= $print_text;
print $print_text;
MIME::Lite->send('sendmail', "$mailprog");
# Create a new multipart message:
$msg = new MIME::Lite
From =>"$admin_email_from",
To =>"$admin_email_to",
Subject =>"$subject",
Type =>"multipart/mixed";
# Add parts
attach $msg
Type =>"TEXT",
Data =>"\nTar.Gzip File\n[$backup_gzip_file]\nAttached\n$body_text";
attach $msg
Type =>"x-gzip",
Encoding =>"base64",
Path =>"$backup_gzip_file",
Filename =>"$backup_gzip_file";
$msg->send || &error_message(qq~Error!\n\nError in Mailing Program!~);
}
else
{
# just send notice, without attachment
&mail_to($admin_email_to, $admin_email_from, $subject, $body_text, $admin_email_from);
}
$print_text = "\nDone! Exiting from Script.\n\n";
$body_text .= $print_text;
print $print_text;
exit;
###################################################################################
# connect_to_db
sub connect_to_db
{
# &connect_to_db($db_main);
my ($db_main) = @_;
if ( $password_location eq 'this_file' )
{
$dbh = DBI->connect("DBI:mysql:$db_main:$db_host:$db_port", $user, $password)
|| &error_message(qq~Error!\n
You were unable to connect to the database.\n
$DBI::errstr~);
}
elsif ( $password_location eq 'cnf' )
{
$dbh = DBI->connect("DBI:mysql:$db_main:$db_host:$db_port"
. ";mysql_read_default_file=$cnf_file"
. ";mysql_read_default_group=$cnf_group",
$user, $password)
|| &error_message(qq~Error!\n
You were unable to connect to the database.\n
$DBI::errstr~);
}
else
{
&error_message(qq~Error!\n
... connecting to the Database.\n
You were unable to connect to the database.
Please check your setup.
~);
}
# $dbh->{PrintError} = 1;
# $dbh->{RaiseError} = 1;
}
###################################################################################
# logout
sub logout
{
warn $DBI::errstr if $DBI::err;
if ( $dbh ){$rcdb = $dbh->disconnect;}
}
###################################################################################
# error_message
sub error_message
{
# &error_message($error_text);
my ($error_text) = @_;
my $subject = "$site_name MySQL Backup Error";
&mail_to($admin_email_to, $admin_email_from, $subject, $error_text, $admin_email_from);
print qq~
\n
$subject\n
$error_text\n
~;
exit;
}
###################################################################################
# mail_to
sub mail_to
{
# &mail_to($email_to, $email_from, $subject, $mail_body, $reply_to);
my ($email_to, $email_from, $subject, $mail_body, $reply_to) = @_;
if ( $reply_to !~ /\@/ ){$reply_to = $email_from;}
open (MAIL, "|$mailprog") || die print qq~Error!\n\nCan't open $mailprog!~;
print MAIL "To: $email_to\n";
print MAIL "From: $email_from\n";
print MAIL "Subject: $subject\n";
print MAIL "Reply-To: $reply_to\n";
print MAIL "\n";
print MAIL "$mail_body";
print MAIL "\n";
close (MAIL);
}
###################################################################################
# do_backup
sub do_backup
{
# &do_backup($db_main, $table_name);
my ($db_main, $table_name) = @_;
my $response_text = '';
my $sth, $rv, $backup_file, $mysqldumpcommand;
my $backup_str, $row_string, $field_value;
my $len_field_terminate;
my @row;
$backup_file = $file_prefix . "." . $date_text . "_" . $db_main . "." . $table_name . "." . $mysql_dump_file_ext;
if ( $backup_type eq 'mysqldump' )
{
if ( $password_location eq 'cnf' )
{
$mysqldumpcommand = "$mysqldump_cmd --host=$db_host $mysqldump_params";
}
else
{
$mysqldumpcommand = "$mysqldump_cmd --host=$db_host --user=$user --password=$password $mysqldump_params";
}
$response_text .= `$mysqldumpcommand $db_main $table_name > $backup_file`;
}
elsif ( $backup_type eq 'outfile' )
{
$backup_str = qq~
select * from $table_name
into outfile '$backup_file'
$outfile_params
~;
$sth = $dbh->do("$backup_str")
or &error_message(qq~Error!\n
Can't backup data: $DBI::errstr~);
}
else
{
unless ( open(FILE, ">$backup_file" ))
{
&error_message(qq~Error!\n
Can't open File $backup_file.~);
}
$sth = $dbh->prepare("select * from $table_name")
or &error_message(qq~Error!\n
Can't do select for backup: $DBI::errstr~);
$rv = $sth->execute
or &error_message(qq~Error!\n
Can't execute the query: $DBI::errstr~);
while ( @row = $sth->fetchrow_array )
{
$row_string = '';
foreach $field_value (@row)
{
$row_string .= $backup_field_enclosed_by .
$field_value .
$backup_field_enclosed_by .
$backup_field_terminate;
}
$len_field_terminate = length($backup_field_terminate);
if ( substr($row_string,-$len_field_terminate,$len_field_terminate) eq $backup_field_terminate)
{
substr($row_string, -$len_field_terminate,$len_field_terminate) = '';
}
$row_string .= $backup_line_terminate;
print FILE $row_string;
}
close(FILE);
}
if ( $chmod_backup_file eq 'yes' )
{
chmod 0600, $backup_file;
}
$response_text .= ' File: ';
$response_text .= `ls -la $backup_file`;
return ($response_text);
}
###################################################################################
# clean_old_files
sub clean_old_files
{
# $mysql_backup_dir
# $seconds_to_save = $increments_to_save * $seconds_multiplier;
# call this subroutine with the '$full_dir_name'
my ($full_dir_name) = @_;
unless ( -e $full_dir_name ){return;}
$save_time = time() - $seconds_to_save;
$old_files = 0;
print qq~\nRemoving Files Older than $increments_to_save $increment_type\n~;
$body_text .= "\nRemoving Files Older than $increments_to_save $increment_type\n";
$body_text .= "\nFiles to be removed:\n\n";
opendir (DIRHANDLE, $full_dir_name);
# we use $file_prefix to make it safer; we don't want to delete
# any files except those matching the file spec
@filelist = grep { /^$file_prefix\./ } readdir(DIRHANDLE);
closedir (DIRHANDLE);
@sortlist = sort(@filelist);
my $file_count = @sortlist;
my $file_msg = "File Count in Backup Dir: $file_count \n";
print $file_msg;
$body_text .= $file_msg;
# loop through directory
foreach $infile (@sortlist)
{
$infile = "$full_dir_name/$infile";
($modtime) = (stat($infile))[9];
if ( $modtime < $save_time )
{
# file is older, so delete it
$old_files++;
# check if file is a directory
if ( -d $infile )
{
$body_text .= "\n - Deleting Tar Subdir: $infile -\n";
$deleted_dir = `rm -r -v $infile`;
if ( $deleted_dir eq "$infile" )
{
$body_text .= "\n - Deleted Tar Subdir Correctly.\n";
}
else
{
$body_text .= "\n - Problem Deleting Tar Subdir.\n";
}
}
else
{
$body_text .= "\n" . `ls -la $infile`;
$delete_count = unlink "$infile";
if ( ! -e $infile and $delete_count == 1 )
{
$body_text .= "$delete_count File Removed: ($infile\)\n";
}
else
{
$body_text .= "\nProblem Removing File: $infile\n";
}
}
}
else
{
$body_text .= "\n- Keeping: $infile -\n";
}
}
}
###################################################################################
# backup_date_string notes
# this is a handy method to initialize date display
# it requires the POSIX and Time::Local calls above
# I use this because I got tired of messing with date routines
# strftime notes:
# %A - full weekday name
# %B - full month name
# %m - month as a decimal number (range 01 to 12).
# %d - day with leading zero
# %Y - 4 digit year
# %H - hour as a decimal number using a 24-hour clock (range 00 to 23).
# %l - hour using 12 hour clock, with no leading zero
# %M - minute as decimal, 00-59
# %S - second as a decimal number (range 00 to 61).
# %p - AM or PM
# %Z - time zone letters
# e.g: $backup_date_string = strftime("%Y-%m-%d_%H.%M.%S", localtime);
###################################################################################
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -