write log file and print tar's stderr afterwards
[sitarba.git] / shbackup
index 351aef95d877e3f0d095289ab582e7619ef21811..239dfa3520faefef2acfb55904621f3eb32ebbe9 100755 (executable)
--- a/shbackup
+++ b/shbackup
@@ -249,7 +249,7 @@ class BackupManager:
 
 
 
-    def backupFileSet(self, fileset, targetdir, since=None):
+    def backupFileSet(self, fileset, targetdir, log, since=None):
         """Create an archive for given fileset at given target directory."""
 
         print("Running file set: " + fileset.name)
@@ -266,11 +266,15 @@ class BackupManager:
 
         tarargs = [tarpath] + taropts + ["-f", fsfn] + fileset.dirs
         #print("tarargs: ", tarargs)
-        tarp = subprocess.Popen( tarargs )
+        print("tar call: " + " ".join(tarargs), file=log)
+        tarp = subprocess.Popen( tarargs, stderr=subprocess.PIPE )
 
         rett = tarp.wait()
         if rett != 0:
-            print(tarpath + " returned with exit status " + str(rett) + ":")
+            sys.stderr.write( tarp.stderr.read() )
+            msg = tarpath + " returned with exit status " + str(rett) + "."
+            print(msg)
+            print(msg, log)
 
 
     def backup(self, epoch=None, mode=None):
@@ -305,6 +309,21 @@ class BackupManager:
             if mode != "full":
                 print("** Warning: full backup recommended!")
 
+
+        # If we have a full backup, we backup everything
+        since = None
+        if mode == "diff":
+            since = sorted(oldfullbackups, key=lambda b: b.date)[-1].date
+        elif mode == "incr":
+            since = sorted(oldbackups, key=lambda b: b.date)[-1].date
+
+        if since != None:
+            print("Making backup relative to ", since.ctime())
+
+        yesno = self.ask_user_yesno("Proceed? [Y, n] ")
+        if yesno == "n":
+            return
+
         # Create new target directory
         basedir = self.conf.directory
         dirname = Backup.getDirName(now, epoch, mode)
@@ -312,19 +331,16 @@ class BackupManager:
         targetdir = os.path.join(basedir, tmpdirname)
         os.mkdir( targetdir )
 
-        # If we have a full backup, we backup everything
-        since = None
 
-        # Get latest full backup time
-        if mode == "diff":
-            since = sorted(oldfullbackups, key=lambda b: b.date)[-1].date
-        # Get latest backup time
-        elif mode == "incr":
-            since = sorted(oldbackups, key=lambda b: b.date)[-1].date
+        log = open(os.path.join(targetdir, "log.log"), 'w')
+        print("Started: " + now.ctime(), file=log)
 
         # Backup all file sets
         for s in self.conf.sets:
-            self.backupFileSet(s, targetdir, since)
+            self.backupFileSet(s, targetdir, log, since)
+
+        print("Stopped: " + datetime.datetime.now().ctime(), file=log)
+        log.close()
 
         # Rename backup directory to final name
         os.rename( targetdir, os.path.join(basedir, dirname) )
@@ -336,6 +352,7 @@ class BackupManager:
             f.close()
 
 
+
     def prune(self):
         """Prune old backup files"""
 
@@ -375,7 +392,7 @@ class BackupManager:
             return
 
         basedir = self.conf.directory
-        yesno = self.ask_user_yesno("Remove entries marked by '*'?")
+        yesno = self.ask_user_yesno("Remove entries marked by '*'? [y, N] ")
         if yesno == "y":
             for d in removeDirs:
                 shutil.rmtree(os.path.join(basedir, d))
@@ -385,7 +402,7 @@ class BackupManager:
             print(question + " y")
             return "y"
         else:
-            return input(question + " [y, N] ")
+            return input(question)
 
 
 def printUsage():