[Erp5-report] r46034 leonardo - /erp5/trunk/utils/Products.TIDStorage/Products/TIDStorage/r...

nobody at svn.erp5.org nobody at svn.erp5.org
Mon Nov 21 22:31:33 CET 2011


Author: leonardo
Date: Mon Nov 21 22:31:33 2011
New Revision: 46034

URL: http://svn.erp5.org?rev=46034&view=rev
Log:
Fix restore_tidstorage to actually work

Fix undefined variables.

Truncate the original FileStorage file after making a backup copy, instead
of slowly copying the desired transactions from the back-up on top of
the original.

Give more information on what is going on.

Modified:
    erp5/trunk/utils/Products.TIDStorage/Products/TIDStorage/repozo/restore_tidstorage.py

Modified: erp5/trunk/utils/Products.TIDStorage/Products/TIDStorage/repozo/restore_tidstorage.py
URL: http://svn.erp5.org/erp5/trunk/utils/Products.TIDStorage/Products/TIDStorage/repozo/restore_tidstorage.py?rev=46034&r1=46033&r2=46034&view=diff
==============================================================================
--- erp5/trunk/utils/Products.TIDStorage/Products/TIDStorage/repozo/restore_tidstorage.py [utf8] (original)
+++ erp5/trunk/utils/Products.TIDStorage/Products/TIDStorage/repozo/restore_tidstorage.py [utf8] Mon Nov 21 22:31:33 2011
@@ -27,15 +27,13 @@ Usage: %(program)s [-h|--help] [-c|--con
   --config configuration_file
     Use given file as configuration file.
     It must be a python file.
-    Recquired if neither -h nor --help are given.
+    Required if neither -h nor --help are given.
 """
 
 import imp
 import getopt
 import sys
 import os
-# urllib2 does not support (?) urls containing credentials
-# (http://login:password@...) but it's fine with urllib.
 from struct import pack
 import shutil
 from ZODB.FileStorage import FileStorage
@@ -98,6 +96,7 @@ def recover(data_fs_backup_path_dict, st
     # Derived from repozo (function=do_full_backup)
     # TODO: optimise to read backup only once.
     can_restore = False
+    print "Considering:", storage_id
     if os.path.exists(backup_path):
       if os.path.exists(file_path):
         print 'Both original and backup files exist for %r. If previous restoration was successful, you should delete the backup for this restoration to take place. Original: %r Backup: %r' % (storage_id, file_path, backup_path)
@@ -107,9 +106,15 @@ def recover(data_fs_backup_path_dict, st
     else:
       if os.path.exists(file_path):
         sys.stdout.write('Copying %r to %r... ' % (file_path, backup_path))
-        shutil.copy(file_path, backup_path)
-        initial_size = stat(file_path).st_size
-        final_size = stat(backup_path).st_size
+        sys.stdout.flush()
+        fsrc = file(file_path, 'rb')
+        fdst = file(backup_path, 'wb')
+        # shutil.copy uses too small a buffer size, which is slow:
+        shutil.copyfileobj(fsrc, fdst, READCHUNK)
+        fsrc.close()
+        fdst.close()
+        initial_size = os.stat(file_path).st_size
+        final_size = os.stat(backup_path).st_size
         if initial_size == final_size:
           can_restore = True
           print 'Done.'
@@ -118,20 +123,15 @@ def recover(data_fs_backup_path_dict, st
       else:
         print 'Cannot find any file for %r: %r and %r do not exist.' % (storage_id, file_path, backup_path)
     if can_restore:
+      tid = last_tid_dict[storage_id]
+      sys.stdout.write('Scanning %r for tid %r... ' % (backup_path, tid))
+      sys.stdout.flush()
       pos = get_tid_position(backup_path,last_tid_dict[storage_id])
-      print 'Restoring backup: %s bytes (transaction %r) from %s to %s' % (pos, tid, backup_path, file_path)
-      source_file = open(backup_path, 'rb')
-      destination_file = open(file_path, 'wb')
-      while pos:
-        todo = min(READCHUNK, pos)
-        data = source_file.read(todo)
-        if not data:
-          print 'Unexpected end of data stream (should contain %i more bytes)' % (pos, )
-          break
-        destination_file.write(data)
-        pos -= len(data)
-      destination_file.close()
-      source_file.close()
+      print 'Done. Position:', pos
+      original_file = open(file_path, 'rb+')
+      original_file.truncate(pos)
+      original_file.close()
+      print 'Truncated %s to: %s bytes (transaction %r)' % (file_path, pos, tid)
     else:
       print 'Skipping restoration of %r (%r).' % (file_path, storage_id)
 



More information about the Erp5-report mailing list