--- /dev/null
+Return-Path: <bremner@tesseract.cs.unb.ca>\r
+X-Original-To: notmuch@notmuchmail.org\r
+Delivered-To: notmuch@notmuchmail.org\r
+Received: from localhost (localhost [127.0.0.1])\r
+ by olra.theworths.org (Postfix) with ESMTP id 0FADA431FC7\r
+ for <notmuch@notmuchmail.org>; Thu, 3 Apr 2014 19:44:41 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: 0\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=0 tagged_above=-999 required=5 tests=[none]\r
+ autolearn=disabled\r
+Received: from olra.theworths.org ([127.0.0.1])\r
+ by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
+ with ESMTP id nhOasN8K3G97 for <notmuch@notmuchmail.org>;\r
+ Thu, 3 Apr 2014 19:44:37 -0700 (PDT)\r
+Received: from mx.xen14.node3324.gplhost.com (gitolite.debian.net\r
+ [87.98.215.224]) (using TLSv1 with cipher AES256-SHA (256/256 bits))\r
+ (No client certificate requested)\r
+ by olra.theworths.org (Postfix) with ESMTPS id B0128431FBF\r
+ for <notmuch@notmuchmail.org>; Thu, 3 Apr 2014 19:44:32 -0700 (PDT)\r
+Received: from remotemail by mx.xen14.node3324.gplhost.com with local (Exim\r
+ 4.72) (envelope-from <bremner@tesseract.cs.unb.ca>)\r
+ id 1WVtXU-00017G-Nl; Fri, 04 Apr 2014 02:07:28 +0000\r
+Received: (nullmailer pid 4020 invoked by uid 1000); Thu, 03 Apr 2014\r
+ 19:41:39 -0000\r
+From: David Bremner <david@tethera.net>\r
+To: notmuch@notmuchmail.org\r
+Subject: [Patch v6 5/6] notmuch-new: backup tags before database upgrade\r
+Date: Thu, 3 Apr 2014 16:41:22 -0300\r
+Message-Id: <1396554083-3892-6-git-send-email-david@tethera.net>\r
+X-Mailer: git-send-email 1.9.0\r
+In-Reply-To: <1396554083-3892-1-git-send-email-david@tethera.net>\r
+References: <1396554083-3892-1-git-send-email-david@tethera.net>\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.13\r
+Precedence: list\r
+List-Id: "Use and development of the notmuch mail system."\r
+ <notmuch.notmuchmail.org>\r
+List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
+List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
+List-Post: <mailto:notmuch@notmuchmail.org>\r
+List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
+List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
+X-List-Received-Date: Fri, 04 Apr 2014 02:44:41 -0000\r
+\r
+All we do here is calculate the backup filename, and call the existing\r
+dump routine.\r
+\r
+Also take the opportity to add a message about being safe to\r
+interrupt.\r
+---\r
+ notmuch-new.c | 29 ++++++++++++++++++++++++++++-\r
+ test/T530-upgrade.sh | 4 +++-\r
+ 2 files changed, 31 insertions(+), 2 deletions(-)\r
+\r
+diff --git a/notmuch-new.c b/notmuch-new.c\r
+index 82acf69..d269c7c 100644\r
+--- a/notmuch-new.c\r
++++ b/notmuch-new.c\r
+@@ -989,8 +989,35 @@ notmuch_new_command (notmuch_config_t *config, int argc, char *argv[])\r
+ return EXIT_FAILURE;\r
+ \r
+ if (notmuch_database_needs_upgrade (notmuch)) {\r
+- if (add_files_state.verbosity >= VERBOSITY_NORMAL)\r
++ time_t now = time (NULL);\r
++ struct tm *gm_time = gmtime (&now);\r
++\r
++ /* since dump files are written atomically, the amount of\r
++ * harm from overwriting one within a second seems\r
++ * relatively small. */\r
++\r
++ const char *backup_name =\r
++ talloc_asprintf (notmuch, "%s/dump-%04d%02d%02dT%02d%02d%02d.gz",\r
++ dot_notmuch_path,\r
++ gm_time->tm_year + 1900,\r
++ gm_time->tm_mon + 1,\r
++ gm_time->tm_mday,\r
++ gm_time->tm_hour,\r
++ gm_time->tm_min,\r
++ gm_time->tm_sec);\r
++\r
++ if (add_files_state.verbosity >= VERBOSITY_NORMAL) {\r
+ printf ("Welcome to a new version of notmuch! Your database will now be upgraded.\n");\r
++ printf ("This process is safe to interrupt.\n");\r
++ printf ("Backing up tags to %s...\n", backup_name);\r
++ }\r
++\r
++ if (notmuch_database_dump (notmuch, backup_name, "",\r
++ DUMP_FORMAT_BATCH_TAG, TRUE)) {\r
++ fprintf (stderr, "Backup failed. Aborting upgrade.");\r
++ return EXIT_FAILURE;\r
++ }\r
++\r
+ gettimeofday (&add_files_state.tv_start, NULL);\r
+ notmuch_database_upgrade (notmuch,\r
+ add_files_state.verbosity >= VERBOSITY_NORMAL ? upgrade_print_progress : NULL,\r
+diff --git a/test/T530-upgrade.sh b/test/T530-upgrade.sh\r
+index 67bbf31..d46e3d1 100755\r
+--- a/test/T530-upgrade.sh\r
++++ b/test/T530-upgrade.sh\r
+@@ -26,9 +26,11 @@ output=$(notmuch search path:foo)\r
+ test_expect_equal "$output" ""\r
+ \r
+ test_begin_subtest "database upgrade from format version 1"\r
+-output=$(notmuch new)\r
++output=$(notmuch new | sed -e 's/^Backing up tags to .*$/Backing up tags to FILENAME/')\r
+ test_expect_equal "$output" "\\r
+ Welcome to a new version of notmuch! Your database will now be upgraded.\r
++This process is safe to interrupt.\r
++Backing up tags to FILENAME\r
+ Your notmuch database has now been upgraded to database format version 2.\r
+ No new mail."\r
+ \r
+-- \r
+1.9.0\r
+\r