Script Archive: Mysql Backup

!/usr/bin/perl -w
 use strict;
 Backup - Script used to backup MySQL databases to an NFS share.
 VERSION: 1.4
 DATE: 12032010
 AUTHOR: packetmad
 This is a Freebsd 4.x-centric script. It may run on other
 versions of Freebsd, or any other BSD for that matter, but I
 wrote it on a Freebsd 4.7 system. It's not even guaranteed
 to run on that.
 Speaking of guarantees, warranties, etc., there ain't one, so
 don't even try. :P
 I am not responsible for the output of this script, nor am I
 responsible for any damage or data loss as a result of using
 this script. In short, I am not responsible for anything
 regarding this script.
 Furthermore, I am not responsible if this script causes
 Your dog to die, your wife to leave you, or
 your truck to break down.
 I am releasing this script under the conventions of the BSD
 license. You can use it, modify it, sleep with it, or
 whatever. If you do find this script useful or have
 suggestions on a better way to do some things contained here
 in, I welcome all correspondence.
 The latest version of this script can be obtained at:
 http://tech.kulish.com
 USE THIS SCRIPT AT YOUR OWN RISK!!!!!!!!!!!!!!!!!!
 Change Log
 v1.1
 Date: 11302002
 Initial Release, absolutely BUG FREE! :)
 v1.2
 Date: 12152005
 Added compression functionality
 Added naming of backup files by date
 Changed longterm storeage directory for 1back to store1
 v1.3
 Date: 11012009
 Cleaned up naming
 Adjusted $bdir variable name for readability in script
 v1.4
 Date: 12032010
 Added structured email header
 Changed (again) longterm storage to archive
 Testing against MariaDB, success
 Change Control:
 01/05/2011  Removed unused drupal databases
 #
 Declare variables and arrays.
 #
 my (
 $bdir, $sysname, $logfile, %databases, $key, $database, $rightnow, $headersysname, $headero, $slevel, $headerdate, $subjectdate
 );
 chomp ( $headersysname = /bin/hostname -f );
 chomp ( $headero = /bin/uname -o );
 chomp ( $slevel = /bin/uname -r );
 chomp ( $headerdate = /bin/date );
 chomp ( $subjectdate = /bin/date +%m%d%Y:%H%M.%S );
 $bdir = '/backup/databases';
 chomp ( $sysname = /bin/uname -n );
 chomp ( $rightnow = /bin/date +%m-%d-%G_%H%M_ );
 print $rightnow;
 Databases we want to backup and associated archive names.
 Database => sql file pairs.
 %databases = (
 'mon_observium' => 'mon_observium.sql',
 'wp_kulishcars' => 'wp_kulishcars.sql',
 'wp_kulishfamily' => 'wp_kulishfamily.sql',
 'wp_kulishtech' => 'wp_kulishtech.sql',
 'mysql' => 'mysql.sql'
 );
 #
 Backup Section
 #
 foreach $key (sort keys %databases) {
 $database = $databases{$key};
 /usr/bin/mysqldump $key > $bdir/$database;}
 #
 Compression Functionality
 #
 foreach $key (sort keys %databases) {
 $database = $databases{$key};
 /bin/gzip -c $bdir/$database > $bdir/$rightnow$database.gz;}
 scp them to the fileshare
 scp $bdir$sysname/*.gz USERNAME\@SERVER:databases;
 #
 Clean backup dir
 #
 rm -rf $bdir/*.sql;
 /bin/mv $bdir/*.gz $bdir/archive;
 Build Header
 /bin/echo "" > /var/log/mysql-backup.log;
 /bin/echo "Node: $headersysname" >> /var/log/mysql-backup.log;
 /bin/echo "OS:   $headero $slevel" >> /var/log/mysql-backup.log;
 /bin/echo "Date: $headerdate" >> /var/log/mysql-backup.log;
 /bin/echo "" >> /var/log/mysql-backup.log;
 /bin/ls -lh /backup/databases | /bin/grep sql >> /var/log/mysql-backup.log;
 Send email notification
 /bin/cat /var/log/mysql-backup.log | /usr/bin/mailx root -s "mysql-backup.pl: $subjectdate";

Hits: 0

Simple MySQL Backup Script

Below is a simple database backup script to use with MySQL.

#!/usr/bin/perl -w

use strict;

# Backup – Script used to backup MySQL databases to an NFS share.
# VERSION: 1.2
# DATE: 12152005
# AUTHOR: packetmad

# This is a Freebsd 4.x-centric script. It may run on other
# versions of Freebsd, or any other BSD for that matter, but I
# wrote it on a Freebsd 4.7 system. It’s not even guaranteed
# to run on that.

# Speaking of guarantees, warranties, etc., there ain’t one, so
# don’t even try. :P
# I am not responsible for the output of this script, nor am I
# responsible for any damage or data loss as a result of using
# this script. In short, I am not responsible for anything
# regarding this script.
# Furthermore, I am not responsible if this script causes
# Your dog to die, your wife to leave you, or
# your truck to break down.
# I am releasing this script under the conventions of the BSD
# license. You can use it, modify it, sleep with it, or
# whatever. If you do find this script useful or have
# suggestions on a better way to do some things contained here
# in, I welcome all correspondence.
# The latest version of this script can be obtained at:
# http://tech.kulish.com

# USE THIS SCRIPT AT YOUR OWN RISK!!!!!!!!!!!!!!!!!!

# Change Log
# v1.1
# Date: 11302002
# Initial Release, absolutely BUG FREE! :)
# v1.2
# Date: 12152005
# Added compression functionality
# Added naming of backup files by date
# Changed longterm storeage directory for 1back to store1

###
# Declare variables and arrays.
###

my (
$bdir, $sysname, $logfile, %databases, $key, $database, $rightnow
);

$bdir = ‘/databases/’;
chomp ( $sysname = `uname -n` );
chomp ( $rightnow = `date +%m%d%G%H%M` );
#print $rightnow;
# Databases we want to backup and associated archive names.
# Database => sql file pairs.

%databases = (‘testdb1’ => ‘testdb1.sql’,
‘testdb2’ => ‘testdb2.sql’);

###
# Backup Section
###
foreach $key (sort keys %databases) {
$database = $databases{$key};
`mysqldump -uUSERNAME -pPASSWORD $key >> $bdir$sysname/$database`;}

###
# Compression Functionality
###

foreach $key (sort keys %databases) {
$database = $databases{$key};
`gzip -c $bdir$sysname/$database > $bdir$sysname/$rightnow$database.gz`;}

# scp them to the fileshare
#`scp $bdir$sysname/*.gz USERNAME\@SERVER:databases`;

###
# Clean backup dir
###

`rm -rf $bdir$sysname/*.sql`;
`mv $bdir$sysname/*.gz $bdir$sysname/store1`;

Hits: 9

Win32 Perl

Actually did my first win32 perl script at work today. What a pita that can be when you are changing from *n?x perl. Most of the problems I encountered were editor issues. Guess I should have just gotten vi for Windows and been done with it.

###
# First we’ll change into the directory passed in the command line
###
$workdirectory = @ARGV[0];
#print (“$workdirectory\n”);
chdir (“$workdirectory”) || die “cannot cd to $workdirectory”;

###
# Lets get a numeric date for use in naming the zip files
###
$insanedate = `date /t`;
# print $insanedate;
$insanedate =~ /.*?(\d{2})\/(\d{2})\/(\d{4})/;
$sanedate = “$1$2$3”;
# print “$sanedate\n”;

###
# Time to backup the .bak files
###
# must include the extension in the form of *.ext
@baklist = `dir /b *.bak`;
if (!@baklist)
{
# Drop out of the script if there is nothing to backup!
print “No files to backup”;
die;
}
else
{
# This will create a zipfile with the current date.zip
`pkzip $sanedate.zip *.bak`;

###
# Take care of directory cleanup
###
@filelist = `dir /b /o:d *.zip`;
# The $keeperN files are the ones that will be (newest) saved,
# anything left in the list will be removed from the directory
if (@filelist)
{
$keeper1 = pop (@filelist);
$keeper2 = pop (@filelist);
# $keeper3 = pop (@filelist);
# print (“$keeper1”);
# print (“$keeper2”);
# print (“$keeper3”);

# DANGER Will Robinson!! DANGER!!
`del *.bak`;
# Activating the following del statement has the potential to
# do lots of damage. Please check working directory first!
foreach $leftover (@filelist)
{
print $leftover;
# `del $leftover`;
}
}
else
{
print “No zip files, CAUTION!”;
die;
}
}

Hits: 1