[mylvmbackup] Re: Using mylvmbackup with bacula?

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Jason A. Kates wrote:
> I was wondering if anybody has updated the mylvmbackup to use with
> backup software such as bacula.
> 
> The ideal would be to use the 1st section of the mylvmbackup as a
> pre-script to aquiesce  the database then setup a snapshot lvm mount.
> 
> The backup software would then do a backup of the snapshot mount. (The
> tar wouldn't be used as it's redundant).
> 
> The backup software would when run the 2nd half of the mylvmbackup
> script to un-mount and remove the snapshot.
>                               Thanks -Jason

I wanted to do the same thing, but I did it the reverse of what you're
suggesting, sort of.

This patch simply checks for /etc/mylvmbackup/$hookname, if it
exists and is runnable, run it and log an error if it failed to run or
returned nonzero, continuing on its merry way regardless. If no hooks
are found normal operation is not affected. I moved the default conf
location to its own directory so as not to pollute /etc/.

For $hookname in:
preconnect
preflush
presnapshot
preunlock
predisconnect
premount
precopy
prebackup
precleanup

It could be extended trivially to include post- hooks of course but that
seems like overkill to me.

Not exhaustively tested, neither I nor Gemini SBS are responsible if
this fails, deletes your backups or paints your cat purple. If you
aren't careful with your hooks you can and will compromise your backup
procedure. Hooks run with the privileges of the backup user and are an
obvious, honking security hole if the permissions are wrong.

- --
Patrick Hahn                            patrick@xxxxxxxxxxxxx
Gemini SBS, LLC                             +1 (646) 723-2334


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFIPBN6POEWh13jFUcRAtEeAJ9yjHLunGmkj6KCH1RHFrIJW38TxwCgtqoD
LyHaVCCO2HnTeII+9mMKuJ4=
=npli
-----END PGP SIGNATURE-----
diff --git a/Makefile b/Makefile
index bc9dd3d..7196251 100644
--- a/Makefile
+++ b/Makefile
@@ -42,7 +42,7 @@ DISTFILES = COPYING \
        TODO
 CLEANFILES = $(NAME).spec $(NAME) $(MAN1) $(MAN1).html
 prefix = /usr/local
-sysconfdir = /etc
+sysconfdir = /etc/mylvmconfig
 bindir = $(prefix)/bin
 distdir = $(NAME)-$(VERSION)
 mandir = $(prefix)/man
diff --git a/mylvmbackup.pl.in b/mylvmbackup.pl.in
index 9dc4e23..09289df 100755
--- a/mylvmbackup.pl.in
+++ b/mylvmbackup.pl.in
@@ -33,7 +33,7 @@ my $version='@VERSION@';
 # syslog-related options
 my $syslog_ident = 'mylvmbackup';
 my $syslog_args = 'pid,ndelay';
-my $configfile = "/etc/mylvmbackup.conf";
+my $configfile = "/etc/mylvmbackup/mylvmbackup.conf";
 my $configfile2 = "";
 
 my $backupdir;
@@ -182,12 +182,14 @@ if(length($port) > 0) {
  $dsn .= ";port=".$port;
 }
 
+run_hook ("preconnect");
 log_msg ("Connecting to database...", LOG_INFO);
 my $dbh= DBI->connect($dsn,$user,$password)
   or log_msg ($DBI::errstr, LOG_ERR) && die $DBI::errstr;
 
 unless ($skip_flush_tables == 1)
 {
+  run_hook ("preflush");
   if($extra_flush_tables == 1)
   {
     log_msg ("Flushing tables (initial)...", LOG_INFO);
@@ -203,18 +205,22 @@ unless ($skip_flush_tables == 1)
 log_msg ("Taking position record...", LOG_INFO);
 &create_pos_file($dbh);
 
+run_hook ("presnapshot");
 log_msg ("Taking snapshot...", LOG_INFO);
 create_snapshot();
 
+run_hook ("preunlock");
 log_msg ("Unlocking tables...", LOG_INFO);
 $dbh->do("UNLOCK TABLES") 
   or log_msg ($DBI::errstr, LOG_ERR) && die $DBI::errstr;
 
+run_hook ("predisconnect");
 log_msg ("Disconnecting from database...", LOG_INFO);
 $dbh->disconnect;
 
 if ($snapshot_created)
 {
+  run_hook("premount");
   log_msg ("Mounting snapshot...", LOG_INFO);
   if (mount_snapshot() and mount_posdir_bind())
   {
@@ -223,15 +229,18 @@ if ($snapshot_created)
       log_msg ("Recovering innodb...", LOG_INFO);
       do_innodb_recover();
     }
+    run_hook("precopy");
     log_msg ("Copying $mycnf_basename...", LOG_INFO);
     create_mycnf_file();
 
+    run_hook("prebackup");
     log_msg ("Taking actual backup...", LOG_INFO);
     do_backup_tar() if ($backuptype eq 'tar');
     do_backup_rsync() if ($backuptype eq 'rsync');
   }    
 }
 
+run_hook("precleanup");
 log_msg ("Cleaning up...", LOG_INFO);
 cleanup();
 exit 0;
@@ -685,4 +694,23 @@ sub lvm_version
   return $lv;
 }
 
+sub run_hook
+{
+  my $hookname = shift;
+  my $hookfile = "/etc/mylvmbackup/".$hookname;
+  if ( (-e $hookfile) && (-r $hookfile) && (-x $hookfile))
+  {
+    log_msg ("Hook ".$hookname." found, running it", LOG_INFO);
+    system($hookfile);
+    if ( $? == -1 )
+    {
+      log_msg ("Hook failed: $!\n", LOG_ERR);
+    }
+    else
+    {
+      log_msg (sprintf("Hook exited with value %d", $? >> 8), LOG_ERR) unless 
($? >> 8 == 0) 
+    }
+  }
+}
+
 # vim: ts=2 sw=2 expandtab ft=perl:

Other related posts: