Bläddra i källkod

Search for excludes reorganized
creation of image dir just before rsync

Wolfgang Ortmann 9 år sedan
förälder
incheckning
dffc26e8cd
8 ändrade filer med 751 tillägg och 782 borttagningar
  1. 1 1
      src/Makefile
  2. 65 0
      src/excludetools.cpp
  3. 9 0
      src/excludetools.h
  4. 12 3
      src/filetools.cpp
  5. 8 3
      src/filetools.h
  6. 30 67
      src/kind.ag
  7. 612 708
      src/kind.cpp
  8. 14 0
      src/kind.h

+ 1 - 1
src/Makefile

@@ -1,7 +1,7 @@
 # Makefile for kind, the backup tool
 # 
 
-OBJECTS = kind.o filetools.o FileName.o DateTime.o stringtools.o KindConfig.o Lexer.o rulecomp.o expiretools.o Image.o
+OBJECTS = kind.o filetools.o FileName.o DateTime.o stringtools.o KindConfig.o Lexer.o rulecomp.o expiretools.o Image.o excludetools.o
 
 LOPT := $(OPT) 
 #-static

+ 65 - 0
src/excludetools.cpp

@@ -0,0 +1,65 @@
+#include "kind.h"
+#include "Exception.h"
+#include "FileName.h"
+#include "filetools.h"
+#include "excludetools.h"
+
+using namespace std;
+
+Strings getExclusions(const KindConfig& conf, bool shellMode)
+{
+  Strings exclusions;
+  if (conf.hasKey("exclude"))
+    exclusions += conf.getStrings("exclude");
+
+  if (shellMode)
+    {
+      string path = conf.getString("path");
+      string remoteShell = conf.getString("remoteShell");
+      string userAtHost = conf.getString("user") + "@" + conf.getString("host");
+      string rshCommand = remoteShell;
+      if (remoteShell.empty())
+        rshCommand = "ssh";
+
+      rshCommand += " " + userAtHost;
+
+      string userExcludeCommand = conf.getString("userExcludeCommand");
+
+      if (!userExcludeCommand.empty())
+        {
+          replacePlaceHolder(userExcludeCommand, "%path", conf.getString("path"));
+
+          verbosePrint("searching for exclusions (" + userExcludeCommand + ")");
+
+          int rc;
+          Strings excludedFiles = remoteExec(rshCommand, userExcludeCommand, rc, debug);
+          if (rc > 0)
+            {
+              // return Strings should contain error messages
+              throw Exception("Find excludes", "Search for excludes failed");
+            }
+
+          for (unsigned int i = 0; i < excludedFiles.size(); ++i)
+            {
+              FileName fn(excludedFiles[i]);
+              exclusions.push_back('/' + fn.getPath());
+              debugPrint("Excluding: " + exclusions.back());
+            }
+        }
+
+      string userExcludeFile = conf.getString("userExcludeFile");
+      if (!userExcludeFile.empty())
+        {
+          userExcludeFile = path + userExcludeFile;
+          string getExcludeFileCommand = " \" if [ -f '" + userExcludeFile + "' ]; then ";
+          getExcludeFileCommand += " cat '" + userExcludeFile + "' ; fi \"";
+          // cout << getExcludeFileCommand << endl;
+          int rc;
+          Strings excludes2 = remoteExec(rshCommand, getExcludeFileCommand, rc, debug);
+          if (rc == 0)
+            exclusions += excludes2;
+        } // if (shellMode)
+    }
+  return exclusions;
+}
+

+ 9 - 0
src/excludetools.h

@@ -0,0 +1,9 @@
+#ifndef EXCLUDETOOLS_H
+#define EXCLUDETOOLS_H
+
+#include "KindConfig.h"
+#include "Strings.h"
+
+Strings getExclusions(const KindConfig& conf, bool shellMode);
+
+#endif

+ 12 - 3
src/filetools.cpp

@@ -208,9 +208,9 @@ int removeDir(const std::string& path)
 }
 
 #define POPEN_BUFFER_SIZE 2000
-Strings myPopen(const std::string& cmd,
-                int& rc, bool debug,
-                const std::string& logfn)
+Strings localExec(const std::string& cmd,
+                  int& rc, bool debug,
+                  const std::string& logfn)
 {
   if (debug)
     std::cout << "Executing " << cmd << std::endl;
@@ -260,3 +260,12 @@ Strings myPopen(const std::string& cmd,
     }
   return res;
 }
+
+Strings remoteExec(const std::string& rshCommand,
+                   const std::string& cmd,
+                   int& rc, bool debug,
+                   const std::string& logfn)
+{
+
+  return localExec(rshCommand + " " + cmd, rc, debug, logfn);
+}

+ 8 - 3
src/filetools.h

@@ -27,9 +27,14 @@ void file2Strings(const std::string& fn, std::vector<std::string>& s);
 bool dirExists(const std::string& name);
 bool fileExists(const std::string& name);
 
-Strings myPopen(const std::string& cmd,
-                int& rc, bool debug,
-                const std::string& logfn = "");
+Strings localExec(const std::string& cmd,
+                  int& rc, bool debug,
+                  const std::string& logfn = "");
+
+Strings remoteExec(const std::string& rshCommand,
+                   const std::string& cmd,
+                   int& rc, bool debug,
+                   const std::string& logfn = "");
 
 void removeLock(const std::string& lockfilename);
 void createLock(const std::string& lockfilename);

+ 30 - 67
src/kind.ag

@@ -25,7 +25,10 @@
 #include "Lexer.h"
 #include "rulecomp.h"
 
+#include "kind.h"
+
 #include "expiretools.h"
+#include "excludetools.h"
 
 /*AppGen
   %%  Beschreibung des Programmes:
@@ -239,20 +242,8 @@ void doBackup(const string& vault,
               const KindConfig& conf)
 {
   // create image path
-  if (!dryRun)
-    if (mkdir(imageFullName.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) != 0)
-      throw Exception("Create image", "failed to create " + imageFullName);
 
-  // error message
-  // we write an generic error message to mark backup as unsuccessful
-  // will be deleted at successful end of rsync
-  string errorfile = imageFullName + "/error";
-  if (!dryRun)
-    {
-      ofstream error(errorfile);
-      error << "failed" << endl;
-      error.close();
-    }
+  bool shellMode = true;
 
   // create source descriptor
   string host;
@@ -261,7 +252,10 @@ void doBackup(const string& vault,
 
   string server;
   if (conf.hasKey("server"))
-    server = conf.getString("server");
+    {
+      server = conf.getString("server");
+      shellMode = false;
+    }
 
   if (!host.empty() && !server.empty())
     throw Exception("backupVault", "Cannot have host and server");
@@ -280,12 +274,9 @@ void doBackup(const string& vault,
       else
         replacePlaceHolder(pingCommand, "%host", server);
       int rc = 0;
-      Strings pingResult = myPopen(pingCommand, rc, debug);
+      Strings pingResult = localExec(pingCommand, rc, debug);
       if (rc != 0)
-        {
-          strings2File(pingResult, errorfile);
-          throw Exception("Host not available", pingCommand);
-        }
+        throw Exception("Host not available", pingCommand);
     }
 
   string path = conf.getString("path");
@@ -302,63 +293,35 @@ void doBackup(const string& vault,
     rsyncCmd += opt + " ";
 
   // excludes
-  Strings excluded;
+  Strings excluded = getExclusions(conf, shellMode);
 
-  if (conf.hasKey("exclude"))
-    excluded += conf.getStrings("exclude");
+  // create image path
+  if (!dryRun)
+    if (mkdir(imageFullName.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) != 0)
+      throw Exception("Create image", "failed to create " + imageFullName);
 
-  if (!host.empty())  // shell mode
+  // error message
+  // we write an generic error message to mark backup as unsuccessful
+  // will be deleted at successful end of rsync
+  string errorfile = imageFullName + "/error";
+  if (!dryRun)
+    {
+      ofstream error(errorfile);
+      error << "failed" << endl;
+      error.close();
+    }
+
+  if (shellMode)  // shell mode
     {
       // cout << "USING SHELLMODE '" << host << "'" << endl;
       string remoteShell = conf.getString("remoteShell");
-      string userAtHost = conf.getString("user") + "@" +
-                          conf.getString("host");
+      string userAtHost = conf.getString("user") + "@" + conf.getString("host");
       string rshCommand = remoteShell;
       if (remoteShell.empty())
         rshCommand = "ssh";
 
       rshCommand += " " + userAtHost;
 
-      string userExcludeCommand = conf.getString("userExcludeCommand");
-
-      if (!userExcludeCommand.empty())
-        {
-          replacePlaceHolder(userExcludeCommand, "%path", path);
-          string excludeCommand = rshCommand + " " + userExcludeCommand;
-
-          verbosePrint("searching for exclusions (" + excludeCommand + ")");
-
-          int rc;
-          Strings excludedFiles = myPopen(excludeCommand, rc, debug);
-          if (rc > 0)
-            {
-              // return Strings should contain error messages
-              strings2File(excludedFiles, errorfile);
-              throw Exception("Find exludes", "Search for excludes failed");
-            }
-
-          for (unsigned int i = 0; i < excludedFiles.size(); ++i)
-            {
-              FileName fn(excludedFiles[i]);
-              excluded.push_back('/' + fn.getPath());
-              debugPrint("Excluding: " + excluded.back());
-            }
-        }
-
-      string userExcludeFile = conf.getString("userExcludeFile");
-      if (!userExcludeFile.empty())
-        {
-          userExcludeFile = path + userExcludeFile;
-          string getExcludeFileCommand = rshCommand;
-          getExcludeFileCommand += " \" if [ -f '" + userExcludeFile + "' ]; then ";
-          getExcludeFileCommand += " cat '" + userExcludeFile + "' ; fi \"";
-          // cout << getExcludeFileCommand << endl;
-          int rc;
-          Strings excludes2 = myPopen(getExcludeFileCommand, rc, debug);
-          if (rc == 0)
-            excluded += excludes2;
-        }
-
       if (!dryRun)
         strings2File(excluded, imageFullName + "/exclude");
 
@@ -396,7 +359,7 @@ void doBackup(const string& vault,
     {
       verbosePrint("syncing (" + rsyncCmd + ")");
       int rc;
-      backupResult = myPopen(rsyncCmd, rc, debug, imageFullName + "/rsync-log");
+      backupResult = localExec(rsyncCmd, rc, debug, imageFullName + "/rsync-log");
       if (rc == 0 ||
           rc == 24 || // "no error" or "vanished source files" (ignored)
           rc == 6144) // workaround for wrong exit code ??!!
@@ -558,7 +521,7 @@ bool backupVault(const string& vault,
                 {
                   int rc;
                   string hardLinkCommand = "cp -al " + imageFullName + " " + lastPath;
-                  Strings res = myPopen(hardLinkCommand, rc, debug);
+                  Strings res = localExec(hardLinkCommand, rc, debug);
                 }
               else if (linkType == "symLink")
                 {

Filskillnaden har hållts tillbaka eftersom den är för stor
+ 612 - 708
src/kind.cpp


+ 14 - 0
src/kind.h

@@ -0,0 +1,14 @@
+#ifndef _KIND_H_
+#define _KIND_H_
+
+#include <string>
+
+extern bool verbose;
+extern bool debug;
+extern bool quiet;
+
+void verbosePrint(const std::string& text);
+
+void debugPrint(const std::string& text);
+
+#endif

Vissa filer visades inte eftersom för många filer har ändrats