It would be handy to know what language you are looking to do this in? I do DB backups as follows in php on a mysql database. This e-mails me an sql file I can then import.
backup_tables('localhost','username','password','DB');
function backup_tables($host,$user,$pass,$name,$tables = '*')
{
$link = mysql_connect($host,$user,$pass);
mysql_select_db($name,$link);
if($tables == '*')
{
$tables = array();
$result = mysql_query('SHOW TABLES');
while($row = mysql_fetch_row($result))
{
$tables[] = $row[0];
}
}
else
{
$tables = is_array($tables) ? $tables : explode(',',$tables);
}
foreach($tables as $table)
{
$result = mysql_query('SELECT * FROM `'.$table.'`');
$num_fields = mysql_num_fields($result);
$return.= 'DROP TABLE `'.$table.'`;';
$row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE `'.$table.'`'));
$return.= "\n\n".$row2[1].";\n\n";
for ($i = 0; $i < $num_fields; $i++)
{
while($row = mysql_fetch_row($result))
{
$return.= 'INSERT INTO '.$table.' VALUES(';
for($j=0; $j<$num_fields; $j++)
{
$row[$j] = addslashes($row[$j]);
$row[$j] = ereg_replace("\n","\\n",$row[$j]);
if (isset($row[$j])) { $return.= '"'.$row[$j].'"' ; } else { $return.= '""'; }
if ($j<($num_fields-1)) { $return.= ','; }
}
$return.= ");\n";
}
}
$return.="\n\n\n";
}
$filename = 'db-backup-'.time().'-'.(md5(implode(',',$tables))).'.sql';
$handle = fopen($filename,'w+');
fwrite($handle,$return);
fclose($handle);
$to = 'db@tornbuddy.com';
$subject = 'DB Backup';
$random_hash = md5(date('r', time()));
$headers = "From: db@tornbuddy.com\r\nReply-To: no-reply@tornbuddy.com";
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
$attachment = chunk_split(base64_encode(file_get_contents($filename)));
ob_start();
?>
--PHP-mixed-<?php echo $random_hash; ???>
Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ???>"
--PHP-alt-<?php echo $random_hash; ???>
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
DB backup
--PHP-alt-<?php echo $random_hash; ???>
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
<h2>DB Backup</h2>
<?php echo $filename; ???>
--PHP-alt-<?php echo $random_hash; ???>--
--PHP-mixed-<?php echo $random_hash; ???>
Content-Type: application/zip; name="db_backup.sql"
Content-Transfer-Encoding: base64
Content-Disposition: attachment
<?php echo $attachment; ???>
--PHP-mixed-<?php echo $random_hash; ???>--
//copy current buffer contents into $message variable and delete current output buffer
$message = ob_get_clean();
//send the email
$mail_sent = @mail( $to, $subject, $message, $headers );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed"
unlink($filename);
}
?>