--- VZDump.pm.orig	2012-02-09 21:07:49.246415341 +0200
+++ VZDump.pm	2012-02-09 21:06:42.754434349 +0200
@@ -33,6 +33,7 @@
 use PVE::VZDump::OpenVZ;
 use Time::localtime;
 use Time::Local;
+use DBI;
 
 my @posix_filesystems = qw(ext3 ext4 nfs nfs4 reiserfs xfs);
 
@@ -743,6 +744,53 @@
     run_command ($logfd, $cmd);
 }
 
+sub mysql_flush {
+    my ($self, $vmid, $logfd) = @_;
+
+    my $opts = $self->{opts};
+
+    my $dbh = undef;
+
+    if (defined ($opts->{'mysql-vmid'})
+	and defined ($opts->{'mysql-default-file'})
+	and -f $opts->{'mysql-default-file'}) {
+
+	foreach my $mysql_vmid (@{$opts->{'mysql-vmid'}}) {
+	    if ($vmid == $mysql_vmid) {
+
+		my $dsn = "DBI:mysql:database=mysql;"
+		    . "mysql_read_default_file=$opts->{'mysql-default-file'};"
+		    . "mysql_read_default_group=$vmid";
+
+		debugmsg ('info', "MySQL: $dsn", $logfd);
+		$dbh = DBI->connect($dsn);
+		if ($dbh) {
+		    debugmsg ('info', "MySQL: flushing tables", $logfd);
+		    $dbh->do("FLUSH TABLES")
+			or debugmsg ('warn', "MySQL: can't flush tables: $DBI::errstr", $logfd);
+		    debugmsg ('info', "MySQL: flushing tables with read lock", $logfd);
+		    $dbh->do("FLUSH TABLES WITH READ LOCK")
+			or debugmsg ('warn', "MySQL: can't flush tables with read lock: $DBI::errstr", $logfd);
+		} else {
+		    debugmsg ('warn', "MySQL: can't connect to database: $DBI::errstr", $logfd);
+		}
+		last;
+	    }
+	}
+    }
+    return $dbh;
+}
+
+sub mysql_unlock {
+    my ($self, $dbh, $logfd) = @_;
+
+    debugmsg ('info', "MySQL: unlocking tables", $logfd);
+    $dbh->do("UNLOCK TABLES")
+        or debugmsg ('warn', "MySQL: can't  unlock tables: $DBI::errstr", $logfd);;
+    debugmsg ('info', "MySQL: disconnecting from database", $logfd);
+    $dbh->disconnect;
+}
+
 sub exec_backup_task {
     my ($self, $task) = @_;
 	 
@@ -910,8 +958,12 @@
 
 	    $self->run_hook_script ('pre-restart', $task, $logfd);
 
+	    my $dbh = $self->mysql_flush ($vmid, $logfd);
+
 	    $plugin->snapshot ($task, $vmid);
 
+	    $self->mysql_unlock ($dbh, $logfd) if $dbh;
+
 	    if ($snapshot_count > 1) {
 		debugmsg ('info', "resume vm", $logfd);
 		$cleanup->{resume} = 0;
