Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

50
LINES

< > BotCompany Repo | #1022822 // thinArbitraryDatedBackups - keeps all hourly backups in last 2 days, one per day in last 31 days, one per week after

JavaX fragment (include)

// file names need to include the pattern "-YYYYMMDD-HH"
static void thinArbitraryDatedBackups(File dir, bool doIt) {
  new L<File> files;
  new Map<File, Double> ageMap;
  
  Pattern pat = regexp("-(20\\d\\d)(\\d\\d)(\\d\\d)-(\\d\\d)");
  for (File f : listFilesNotDirs(dir)) {
    S s = f.getName();
    Matcher matcher = pat.matcher(s);
    continue unless matcher.find();
    //print("Found backup: " + sfu(matcherGroups(matcher)));
    int year = matcherInt(matcher, 1);
    int month = matcherInt(matcher, 2);
    int day = matcherInt(matcher, 3);
    int hour = matcherInt(matcher, 4);
    long time = timestampFromYMDH(year, month, day, hour);
    double age = ((now()-time)/1000.0/60/60/24;
    //print("Age: " + age + " days");
    ageMap.put(f, age);
    files.add(f);
  }
  
  int numDeleted = 0;
  sortByMap_inPlace(files, ageMap);
  double lastAge = -1;
  for (File f : files) {
    double age = ageMap.get(f);
    if (!thinArbitraryDatedBackups_shouldKeep(age, lastAge)) {
      //print("Deleting: " + f);
      ++numDeleted;
      if (doIt) {
        print("Deleting: " + f);
        f.delete();
      }
    } else {
      print("Keeping: " + f);
      lastAge = age;
    }
  }
  if (numDeleted != 0)
    print((doIt ? "Deleted: " : "Would delete: ") + n(numDeleted, "file"));
}

// age = age in days
sbool thinArbitraryDatedBackups_shouldKeep(double age, double lastAge) {
  if (age <= 2) true;
  if (age <= 31 && age >= lastAge+1) true;
  if (age >= lastAge+7) true;
  false;
}

Author comment

Began life as a copy of #1011269

download  show line numbers  debug dex  old transpilations   

Travelled to 7 computer(s): bhatertpkbcr, cfunsshuasjs, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1022822
Snippet name: thinArbitraryDatedBackups - keeps all hourly backups in last 2 days, one per day in last 31 days, one per week after
Eternal ID of this version: #1022822/1
Text MD5: 27905c9385e6a22b6f825efbb11c9cb1
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-04-09 11:41:50
Source code size: 1544 bytes / 50 lines
Pitched / IR pitched: No / No
Views / Downloads: 280 / 304
Referenced in: [show references]