浏览代码

interleaved backup/expire

Wolfgang Ortmann 9 年之前
父节点
当前提交
58a3e294bb
共有 5 个文件被更改,包括 525 次插入504 次删除
  1. 23 7
      src/Image.cpp
  2. 3 0
      src/Image.h
  3. 31 35
      src/kind.ag
  4. 461 461
      src/kind.cpp
  5. 7 1
      src/stringtools.cpp

+ 23 - 7
src/Image.cpp

@@ -10,10 +10,11 @@ using namespace std;
 
 Image::Image(const string& dir): name(dir)
 {
-  FileName fn(dir);
-  string dummy;
+  FileName fn(name);
+  string dummy;// image "name"
   stringToDate(fn.getName(), time, dummy);
   string expireFileName = name + "/expires";
+
   valid = !fileExists(name + "/error") &&
           fileExists(expireFileName) &&
           dirExists(name + "/tree");
@@ -22,11 +23,26 @@ Image::Image(const string& dir): name(dir)
 
   if (valid)
     {
-      Strings ex;
-      file2Strings(expireFileName, ex);
-      if (ex.empty())
+      Strings expireText;
+      file2Strings(expireFileName, expireText);
+      if (expireText.empty())
         throw Exception("expireDate", "expire empty");
-      stringToDate(ex[0], expire, series);
-      expireRule = ex[1];
+      stringToDate(expireText[0], expire, series);
+      expireRule = expireText[1];
+    }
+}
+
+void Image::printInfo() const
+{
+  cout << name << endl;
+  if (valid)
+    {
+      if (series != "expire")
+	cout << "backup set: " << series << endl;
+      cout << "created: " << time.getString('h') << endl;
+      cout << "expires: " << expire.getString('h') << " -  " << timeString(expire - DateTime::now()) << endl;
     }
+  
+  else
+    cout << "invalid" << endl;
 }

+ 3 - 0
src/Image.h

@@ -18,6 +18,9 @@ public:
   bool valid;
   DateTime expire;
   std::string expireRule;
+
+  void printInfo() const;
+
   friend bool operator<(const Image& i1, const Image& i2)
   {
     return i1.time < i2.time;

+ 31 - 35
src/kind.ag

@@ -61,22 +61,31 @@ using namespace std;
 
 Strings banks;
 
+string findVault(const string& v);
+
 typedef pair<long int, long int> Sizes;
 map<string, Sizes> sizes;
 
 void readSizes(const string& logSizeFile)
 {
-  if (fileExists(logSizeFile))
+  if (!logSizeFile.empty() && fileExists(logSizeFile))
     {
       vector<string> ss;
       file2Strings(logSizeFile, ss);
       for (const string& s : ss)
         {
           unsigned int i = 0;
-          string v = getWord(s, i);
+          string vault = getWord(s, i);
           long int s1 = getLongInt(s, i);
           long int s2 = getLongInt(s, i);
-          sizes[v] = Sizes(s1, s2);
+	  try {
+	    findVault(vault);
+	    sizes[vault] = Sizes(s1, s2);
+	  }
+	  catch(...)
+	    {
+	      // ignore missing vaults
+	    }
         }
     }
 }
@@ -212,21 +221,13 @@ void listImageInfo(const string& vault,
   readVaultConfig(vault, conf);
   string vaultPath = findVault(vault);
   Images imageList = findImages(vaultPath, conf, true);
+  cout << "---"<<endl;
   for (auto img : imageList)
     {
       if (img.series == backupSet || backupSet.empty())
         {
-          cout << img.name << endl;
-          if (img.valid)
-            {
-              if (img.series != "expire")
-                cout << "backup set: " << img.series << endl;
-              cout << "created: " << img.time.getString() << endl;
-              cout << "expires: " << img.expire.getString() << " -  +" << timeString(img.expire - imageTime) << endl;
-            }
-
-          else
-            cout << "invalid" << endl;
+	  img.printInfo();
+	  cout << "---"<<endl;
         }
     }
 }
@@ -420,7 +421,7 @@ void doBackup(const string& vault,
     cout << "Not executing " << rsyncCmd << endl;
 }
 
-void backupVault(const string& vault,
+bool backupVault(const string& vault,
                  KindConfig conf /*Copy!*/ ,
                  const DateTime& imageTime,
                  bool fullImage,
@@ -525,7 +526,6 @@ void backupVault(const string& vault,
 
       if (backupNow)
         {
-
           // find reference image
           string referenceImage;
           if (!fullImage)
@@ -583,10 +583,12 @@ void backupVault(const string& vault,
               expireFile << rule << endl;
             }
         }
+      return backupNow;
     }
   catch (Exception ex)
     {
       cerr << "Exception in vault " << vault << ": " << ex.what() << endl;
+      return false;
     }
 }
 
@@ -610,7 +612,10 @@ void expireVault(const string& vault, KindConfig conf, DateTime now)
 
   for (Image image : imagelist)
     {
-      debugPrint(image.name);
+      if (debug)
+	{
+	  image.printInfo();
+	}
 
       DateTime imageTime = image.time;
 
@@ -636,13 +641,6 @@ void expireVault(const string& vault, KindConfig conf, DateTime now)
               expireRule = image.expireRule;
             }
 
-          if (debug)
-            {
-              cout << "    image: " << imageTime.getString('h') << endl;
-              cout << "      expire: " << expireTime.getString('h') << " " << expireRule << endl;
-              cout << "      now: " << now.getString('h') << endl;
-            }
-
           if (expireTime < now)
             {
               if (!quiet)
@@ -733,8 +731,7 @@ int main(int argc, char* argv[])
       createLock(lockFile);
 
       string logSizeFile = conf.getString("logSize");
-      if (!logSizeFile.empty())
-        readSizes(logSizeFile);
+      readSizes(logSizeFile);
 
       vector<string> vaults;
       string groupname = "group_" + vault;
@@ -743,16 +740,15 @@ int main(int argc, char* argv[])
       else
         vaults.push_back(vault);
 
-      if (doBackup)
-        for (string vault : vaults)
-          {
-            backupVault(vault, conf, imageTime, fullImage, forcedBackupSet);
-            writeSizes(logSizeFile);
-          }
 
-      if (doExpire)
-        for (unsigned int i = 0; i < vaults.size(); ++i)
-          expireVault(vaults[i], conf, imageTime);
+      for (string vault : vaults)
+	{
+	  if (doBackup)
+	    if (backupVault(vault, conf, imageTime, fullImage, forcedBackupSet))
+	      writeSizes(logSizeFile);
+	  if (doExpire)
+	    expireVault(vault, conf, imageTime);
+	}
 
       if (!quiet)
         cout << DateTime::now().getString('h') << ": finished" << endl;

文件差异内容过多而无法显示
+ 461 - 461
src/kind.cpp


+ 7 - 1
src/stringtools.cpp

@@ -197,6 +197,12 @@ time_t stot(const string& str)
 
 string timeString(time_t t)
 {
+  string res;
+  if (t<0)
+    {
+      res="- ";
+      t = -t;
+    }
   int sec = t % 60;
   t /= 60;
   int min = t % 60;
@@ -204,7 +210,6 @@ string timeString(time_t t)
   int hour = t % 24;
   t /= 24;
   int days = t;
-  string res;
   if (days > 0)
     res += to_string(days) + " days ";
   if (hour > 0)
@@ -234,6 +239,7 @@ long int getNumber(const string& l)
     {
       // we ignore overflow here because value is only informative
       cout << "stol failed on " << l << endl;
+      res = -1;
     }
   return res;
 }

部分文件因为文件数量过多而无法显示