Skip to content

Instantly share code, notes, and snippets.

@miceno
Created January 3, 2011 12:46
Show Gist options
  • Save miceno/763422 to your computer and use it in GitHub Desktop.
Save miceno/763422 to your computer and use it in GitHub Desktop.
Database backup and send by email
#!/bin/bash
function do_backup(){
FICHERO_BACKUP=$1
# Do the backup
$MYSQL_PATH/mysqldump -p"$password" \
--add-drop-table \
-h "$hostname" \
-u "$username" \
"$database" \
| $BIN_PATH/bzip2 -c > "$FICHERO_BACKUP"
}
BASEDIR=$(dirname $0)
BACKUPDIR=/private/backup
DBCONFIG=$BASEDIR/dbconfig.ini
test -f "$DBCONFIG" && source "$DBCONFIG"
if [ -z "$database" ]; then
echo "error: Configuration file not found '$DBCONFIG'"
exit 0
fi
EMAILTO="email@example.com"
BIN_PATH=/bin
MYSQL_PATH=/usr/bin
DIA=`$BIN_PATH/date -I`
FECHA=`$BIN_PATH/date +%Y-%m-%d_%H-%M`
FICHERO_BACKUP=$BACKUPDIR/dbdump-$FECHA.sql.bz2
cd "$BASEDIR"
do_backup "$FICHERO_BACKUP"
# Send by email
./send-email-attachment.pl "$EMAILTO" "$FICHERO_BACKUP" "$FECHA" "application/zip"
#!/usr/bin/perl
# Akadia AG, Arvenweg 4, CH-3604 Thun send_attachment.pl
# ----------------------------------------------------------------------
#
# File: send_attachment.pl
#
# Autor: Martin Zahn / 05.01.2003
#
# Purpose: Email attachments in Perl
#
# Location: $ORACLE_HOME\Database
#
# Certified: Perl 5.6.1, MIME-Lite-2.117 on Cygwin / Windows 2000
# ----------------------------------------------------------------------
use MIME::Lite;
use Net::SMTP;
$numArgs = $#ARGV + 1;
my $to_address = $ARGV[0];
my $my_file = $ARGV[1];
my $FECHA = $ARGV[2];
my $mime_type = $ARGV[3];
### Adjust sender, recipient and your SMTP mailhost
my $from_address = 'Backup Gallery2 AHPN <fotos@arxiuhistoricpoblenou.es>';
### Adjust subject and body message
my $subject = 'AHPN Gallery Database Backup ' . $FECHA;
my $message_body = "Backup de la base de datos del AHPN\n\nFecha: " . $FECHA;
### Create the multipart container
$msg = MIME::Lite->new (
From => $from_address,
To => $to_address,
Subject => $subject,
Type =>'multipart/mixed'
) or die "Error creating multipart container: $!\n";
### Add the text message part
$msg->attach (
Type => 'TEXT',
Data => $message_body
) or die "Error adding the text message part: $!\n";
### Add the file
$msg->attach (
Type => $mime_type,
Path => $my_file,
Filename => $my_file,
Disposition => 'attachment'
) or die "Error adding $file_gif: $!\n";
### Send the Message
#MIME::Lite->send('smtp', $mail_host, Timeout=>60);
MIME::Lite->send('sendmail' );
$msg->send;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment