aboutsummaryrefslogtreecommitdiff
path: root/systems/backup-2/databases/utils.nix
blob: 1b3190ff1035cfe885e1af8a3a2afa6f8e20dab3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
{ pkgs }:
{
  keepLastNDumps = ext: backupDir: n: ''
    #!${pkgs.stdenv.shell}

    cd ${backupDir}
    ${pkgs.coreutils}/bin/rm -f \
      $(${pkgs.coreutils}/bin/ls -1 *.${ext} \
        | ${pkgs.coreutils}/bin/sort -r \
        | ${pkgs.gnused}/bin/sed -e '1,${builtins.toString n}d')
    '';
  exponentialDumps = ext: backupDir: let
    log2rotateSrc = builtins.fetchGit {
      url = "https://github.com/avian2/pylog2rotate";
      ref = "master";
      rev = "061f0564757289d3bea553b16f8fd5c4a0319c5e";
    };
    log2rotate = pkgs.writeScript "log2rotate" ''
      #!${pkgs.python38}/bin/python

      ${builtins.readFile "${log2rotateSrc}/log2rotate.py"}
    '';
  in ''
    #!${pkgs.stdenv.shell}

    cd ${backupDir}
    ${pkgs.coreutils}/bin/rm -f $(ls -1 *.${ext} | grep -v 'T22:' | sort -r | sed -e '1,12d')
    ${pkgs.coreutils}/bin/rm -f $(ls -1 *T22*.${ext} | ${log2rotate} --skip 7 --fuzz 7 --delete --format='%Y-%m-%dT%H:%M+00:00.${ext}')
    '';
}