summaryrefslogtreecommitdiff
path: root/config/borg-backup.nix
blob: 59c5db32c19dd37461403039b0b832c9aea77645 (plain)
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# To initialize repo
# borg init --encryption=keyfile /staging/backup
# Key file also stored in pass
{ config, lib, pkgs, ... }:
let
  repo = "/staging/backup";
in
{
  systemd.services.borg-backup = {
    description = "Run backups.";
    path = with pkgs; [ borgbackup rsync openssh ];
    startAt = "02:30";
    environment = {
      BORG_KEYS_DIR = "/opt/secret/borg-backup-keys/";
      REPOSITORY = repo;
    };
    # serviceConfig = {
    #   RemainAfterExit = true;
    # };
    script = ''
      echo "Database backup"
      ssh orbekk@raigh.orbekk.com sqlite3 /home/orbekk/linoquotes.sqlite \".backup /home/orbekk/linoquotes-backup.sqlite\"
      rsync -Hax orbekk@raigh.orbekk.com:linoquotes-backup.sqlite /storage/archive/linoquotes/

      echo "Creating backup"
      borg create -v --stats                     \
        --compression lzma,6                     \
        $REPOSITORY::'storage-{now:%Y-%m-%d}'    \
        /storage

      borg create -v --stats                     \
        --compression lzma,6                     \
        $REPOSITORY::'{hostname}-{now:%Y-%m-%d}' \
        /opt /home

      echo "Pruning old versions"
      borg prune -v --list $REPOSITORY --prefix 'storage-' \
        --keep-daily=7 --keep-weekly=4 --keep-monthly=6  
      borg prune -v --list $REPOSITORY --prefix '{hostname}-' \
        --keep-daily=7 --keep-weekly=4 --keep-monthly=6  

      echo "Synchronizing backup"
      rsync --delete -Hax /staging/backup root@orbekk.osl.trygveandre.net:/storage
      echo "Success."
    '';
  };
}