From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <d.csapak@proxmox.com>
Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits))
 (No client certificate requested)
 by lists.proxmox.com (Postfix) with ESMTPS id 8C18571922
 for <pbs-devel@lists.proxmox.com>; Thu, 19 May 2022 12:08:59 +0200 (CEST)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
 by firstgate.proxmox.com (Proxmox) with ESMTP id 830436762
 for <pbs-devel@lists.proxmox.com>; Thu, 19 May 2022 12:08:29 +0200 (CEST)
Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com
 [94.136.29.106])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256)
 (No client certificate requested)
 by firstgate.proxmox.com (Proxmox) with ESMTPS id 937DC66F7
 for <pbs-devel@lists.proxmox.com>; Thu, 19 May 2022 12:08:27 +0200 (CEST)
Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1])
 by proxmox-new.maurer-it.com (Proxmox) with ESMTP id C2EAD4370B
 for <pbs-devel@lists.proxmox.com>; Thu, 19 May 2022 12:08:21 +0200 (CEST)
From: Dominik Csapak <d.csapak@proxmox.com>
To: pbs-devel@lists.proxmox.com
Date: Thu, 19 May 2022 12:08:18 +0200
Message-Id: <20220519100820.1829147-4-d.csapak@proxmox.com>
X-Mailer: git-send-email 2.30.2
In-Reply-To: <20220519100820.1829147-1-d.csapak@proxmox.com>
References: <20220519100820.1829147-1-d.csapak@proxmox.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
X-SPAM-LEVEL: Spam detection results:  0
 AWL 0.117 Adjusted score from AWL reputation of From: address
 BAYES_00                 -1.9 Bayes spam probability is 0 to 1%
 KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment
 SPF_HELO_NONE           0.001 SPF: HELO does not publish an SPF Record
 SPF_PASS               -0.001 SPF: sender matches SPF record
 T_SCC_BODY_TEXT_LINE    -0.01 -
 URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See
 http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more
 information. [drive.rs]
Subject: [pbs-devel] [PATCH proxmox-backup 3/5] api/tape/inventory:
 optionally try to restore catalogs
X-BeenThere: pbs-devel@lists.proxmox.com
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: Proxmox Backup Server development discussion
 <pbs-devel.lists.proxmox.com>
List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pbs-devel>, 
 <mailto:pbs-devel-request@lists.proxmox.com?subject=unsubscribe>
List-Archive: <http://lists.proxmox.com/pipermail/pbs-devel/>
List-Post: <mailto:pbs-devel@lists.proxmox.com>
List-Help: <mailto:pbs-devel-request@lists.proxmox.com?subject=help>
List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel>, 
 <mailto:pbs-devel-request@lists.proxmox.com?subject=subscribe>
X-List-Received-Date: Thu, 19 May 2022 10:08:59 -0000

in a disaster recovery case, it is useful to not only re-invetorize
the labels + media-sets, but also to try to recover the catalogs
from the tape (to know whats on there). This adds an option to
the inventory api call that tries to do a fast catalog restore
from each tape to be inventorized.

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
---
 src/api2/tape/drive.rs | 41 +++++++++++++++++++++++++++++++++++------
 1 file changed, 35 insertions(+), 6 deletions(-)

diff --git a/src/api2/tape/drive.rs b/src/api2/tape/drive.rs
index 28c0f0f5..3394afe9 100644
--- a/src/api2/tape/drive.rs
+++ b/src/api2/tape/drive.rs
@@ -848,6 +848,13 @@ pub async fn inventory(drive: String) -> Result<Vec<LabelUuidMap>, Error> {
             "read-all-labels": {
                 description: "Load all tapes and try read labels (even if already inventoried)",
                 type: bool,
+                default: false,
+                optional: true,
+            },
+            "catalog": {
+                description: "Restore the catalog from tape.",
+                type: bool,
+                default: false,
                 optional: true,
             },
         },
@@ -867,10 +874,13 @@ pub async fn inventory(drive: String) -> Result<Vec<LabelUuidMap>, Error> {
 /// then loads any unknown media into the drive, reads the label, and
 /// store the result to the media database.
 ///
+/// If `catalog` is true, also tries to restore the catalog from tape.
+///
 /// Note: This updates the media online status.
 pub fn update_inventory(
     drive: String,
     read_all_labels: Option<bool>,
+    catalog: Option<bool>,
     rpcenv: &mut dyn RpcEnvironment,
 ) -> Result<Value, Error> {
     let upid_str = run_drive_worker(
@@ -889,6 +899,8 @@ pub fn update_inventory(
             let mut inventory = Inventory::load(TAPE_STATUS_DIR)?;
 
             update_changer_online_status(&config, &mut inventory, &changer_name, &label_text_list)?;
+            let catalog = catalog.unwrap_or(false);
+            let read_all_labels = read_all_labels.unwrap_or(false);
 
             for label_text in label_text_list.iter() {
                 if label_text.starts_with("CLN") {
@@ -898,11 +910,13 @@ pub fn update_inventory(
 
                 let label_text = label_text.to_string();
 
-                if !read_all_labels.unwrap_or(false)
-                    && inventory.find_media_by_label_text(&label_text).is_some()
-                {
-                    task_log!(worker, "media '{}' already inventoried", label_text);
-                    continue;
+                if !read_all_labels {
+                    if let Some(media_id) = inventory.find_media_by_label_text(&label_text) {
+                        if !catalog || MediaCatalog::exists(TAPE_STATUS_DIR, &media_id.label.uuid) {
+                            task_log!(worker, "media '{}' already inventoried", label_text);
+                            continue;
+                        }
+                    }
                 }
 
                 if let Err(err) = changer.load_media(&label_text) {
@@ -947,7 +961,22 @@ pub fn update_inventory(
                             let _pool_lock = lock_media_pool(TAPE_STATUS_DIR, pool)?;
                             let _lock = lock_media_set(TAPE_STATUS_DIR, uuid, None)?;
                             MediaCatalog::destroy_unrelated_catalog(TAPE_STATUS_DIR, &media_id)?;
-                            inventory.store(media_id, false)?;
+                            inventory.store(media_id.clone(), false)?;
+
+                            if catalog {
+                                let media_set = inventory.compute_media_set_members(uuid)?;
+                                if let Err(err) = fast_catalog_restore(
+                                    &worker,
+                                    &mut drive,
+                                    &media_set,
+                                    &media_id.label.uuid,
+                                ) {
+                                    task_warn!(
+                                        worker,
+                                        "could not restore catalog for {label_text}: {err}"
+                                    );
+                                }
+                            }
                         } else {
                             let _lock = lock_unassigned_media_pool(TAPE_STATUS_DIR)?;
                             MediaCatalog::destroy(TAPE_STATUS_DIR, &media_id.label.uuid)?;
-- 
2.30.2