summaryrefslogtreecommitdiff
path: root/src/Propellor/Property/Attic.hs
blob: 9fc79abd4c2b3976dda4834d91108492f0c9232f (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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
-- | Maintainer: FĂ©lix Sipma <felix+propellor@gueux.org>

module Propellor.Property.Attic
	( installed
	, repoExists
	, init
	, restored
	, backup
	, KeepPolicy (..)
	) where

import Propellor.Base hiding (init)
import Prelude hiding (init)
import qualified Propellor.Property.Apt as Apt
import qualified Propellor.Property.Cron as Cron
import Data.List (intercalate)

type AtticParam = String

type AtticRepo = FilePath

installed :: Property DebianLike
installed = Apt.installed ["attic"]

repoExists :: AtticRepo -> IO Bool
repoExists repo = boolSystem "attic" [Param "list", File repo]

init :: AtticRepo -> Property DebianLike
init backupdir = check (not <$> repoExists backupdir) (cmdProperty "attic" initargs)
	`requires` installed
  where
	initargs =
		[ "init"
		, backupdir
		]

restored :: [FilePath] -> AtticRepo -> Property DebianLike
restored dirs backupdir = cmdProperty "attic" restoreargs
	`assume` MadeChange
	`describe` ("attic restore from " ++ backupdir)
	`requires` installed
  where
	restoreargs =
		[ "extract"
		, backupdir
		]
		++ dirs

backup :: [FilePath] -> AtticRepo -> Cron.Times -> [AtticParam] -> [KeepPolicy] -> Property DebianLike
backup dirs backupdir crontimes extraargs kp = propertyList (backupdir ++ " attic backup") $ props
	& check (not <$> repoExists backupdir) (restored dirs backupdir)
	& Cron.niceJob ("attic_backup" ++ backupdir) crontimes (User "root") "/" backupcmd
	`requires` installed
  where
	backupcmd = intercalate ";" $
		createCommand
		: if null kp then [] else [pruneCommand]
	createCommand = unwords $
		[ "attic"
		, "create"
		, "--stats"
		]
		++ extraargs ++
		[ backupdir ++ "::" ++ "$(date --iso-8601=ns --utc)"
		, unwords dirs
		]
	pruneCommand = unwords $
		[ "attic"
		, "prune"
		, backupdir
		]
		++
		map keepParam kp

keepParam :: KeepPolicy -> AtticParam
keepParam (KeepHours n) = "--keep-hourly=" ++ show n
keepParam (KeepDays n) = "--keep-daily=" ++ show n
keepParam (KeepWeeks n) = "--keep-daily=" ++ show n
keepParam (KeepMonths n) = "--keep-monthly=" ++ show n
keepParam (KeepYears n) = "--keep-yearly=" ++ show n

data KeepPolicy
	= KeepHours Int
	| KeepDays Int
	| KeepWeeks Int
	| KeepMonths Int
	| KeepYears Int