]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
python:netcmd: Create a SHA256SUM file with checksums
authorAndreas Schneider <asn@samba.org>
Wed, 15 Feb 2023 07:10:03 +0000 (08:10 +0100)
committerAndrew Bartlett <abartlet@samba.org>
Thu, 6 Jun 2024 20:25:36 +0000 (20:25 +0000)
This allows to verify the backup tarball contents with:

  sha256sum -c SHA256SUM

Signed-off-by: Andreas Schneider <asn@samba.org>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
python/samba/netcmd/domain/backup.py

index 936e277d0bf0a8a1faafcb1299790cf78ffc3172..b27105116dccf2a68119b87f455d81e34574bef7 100644 (file)
@@ -56,6 +56,7 @@ from samba.dsdb import _dsdb_load_udv_v2
 from samba.ndr import ndr_pack
 from samba.credentials import SMB_SIGNING_REQUIRED
 from samba import safe_tarfile as tarfile
+import hashlib
 
 
 # work out a SID (based on a free RID) to use when the domain gets restored.
@@ -133,6 +134,14 @@ def backup_filepath(targetdir, name, time_str):
     return os.path.join(targetdir, filename)
 
 
+def create_sha256sum(filename):
+    hash = hashlib.new('sha256')
+    with open(filename, "rb") as f:
+        for chunk in iter(lambda: f.read(65536), b""):
+            hash.update(chunk)
+    return hash.hexdigest()
+
+
 def create_backup_tar(logger, tmpdir, backup_filepath):
     # Adds everything in the tmpdir into a new tar file
     logger.info("Creating backup file %s..." % backup_filepath)
@@ -1228,20 +1237,36 @@ class cmd_domain_backup_offline(samba.netcmd.Command):
         os.remove(backup_fn)
 
         logger.info('building backup tar')
+
+        chksum_list = []
+
         for path in all_files:
             arc_path = self.get_arc_path(path, paths)
 
             if os.path.exists(path + self.backup_ext):
                 logger.info('   adding backup ' + arc_path + self.backup_ext +
                             ' to tar and deleting file')
+                chksum_list.append(
+                    "%s  %s" % (create_sha256sum(path + self.backup_ext),
+                                arc_path))
                 tar.add(path + self.backup_ext, arcname=arc_path)
                 os.remove(path + self.backup_ext)
             elif path.endswith('.ldb') or path.endswith('.tdb'):
                 logger.info('   skipping ' + arc_path)
             elif os.path.isfile(path):
                 logger.info('   adding misc file ' + arc_path)
+                chksum_list.append("%s  %s" %
+                                   (create_sha256sum(path),
+                                    arc_path))
                 tar.add(path, arcname=arc_path)
 
+        chksum_filepath = os.path.join(temp_tar_dir, "SHA256SUM")
+        with open(chksum_filepath, "w") as f:
+            for c in chksum_list:
+                f.write(c + '\n')
+        tar.add(chksum_filepath, os.path.basename(chksum_filepath))
+        os.remove(chksum_filepath)
+
         tar.close()
         os.rename(temp_tar_name,
                   os.path.join(targetdir,