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 dir backupdir = go `requires` installed
where
go :: Property DebianLike
go = property (dir ++ " restored by attic") $ ifM (liftIO needsRestore)
( do
warningMessage $ dir ++ " is empty/missing; restoring from backup ..."
liftIO restore
, noChange
)
needsRestore = null <$> catchDefaultIO [] (dirContents dir)
restore = withTmpDirIn (takeDirectory dir) "attic-restore" $ \tmpdir -> do
ok <- boolSystem "attic" $
[ Param "extract"
, Param backupdir
, Param tmpdir
]
let restoreddir = tmpdir ++ "/" ++ dir
ifM (pure ok <&&> doesDirectoryExist restoreddir)
( do
void $ tryIO $ removeDirectory dir
renameDirectory restoreddir dir
return MadeChange
, return FailedChange
)
backup :: FilePath -> AtticRepo -> Cron.Times -> [AtticParam] -> [KeepPolicy] -> Property DebianLike
backup dir backupdir crontimes extraargs kp = backup' dir backupdir crontimes extraargs kp
`requires` restored dir backupdir
backup' :: FilePath -> AtticRepo -> Cron.Times -> [AtticParam] -> [KeepPolicy] -> Property DebianLike
backup' dir backupdir crontimes extraargs kp = cronjob
`describe` desc
`requires` installed
where
desc = backupdir ++ " attic backup"
cronjob = Cron.niceJob ("attic_backup" ++ dir) crontimes (User "root") "/" $
"flock " ++ shellEscape lockfile ++ " sh -c " ++ backupcmd
lockfile = "/var/lock/propellor-attic.lock"
backupcmd = intercalate ";" $
createCommand
: if null kp then [] else [pruneCommand]
createCommand = unwords $
[ "attic"
, "create"
, "--stats"
]
++ map shellEscape extraargs ++
[ shellEscape backupdir ++ "::" ++ "$(date --iso-8601=ns --utc)"
, shellEscape dir
]
pruneCommand = unwords $
[ "attic"
, "prune"
, shellEscape backupdir
]
++
map keepParam kp
keepParam :: KeepPolicy -> AtticParam
keepParam (KeepHours n) = "--keep-hourly=" ++ val n
keepParam (KeepDays n) = "--keep-daily=" ++ val n
keepParam (KeepWeeks n) = "--keep-daily=" ++ val n
keepParam (KeepMonths n) = "--keep-monthly=" ++ val n
keepParam (KeepYears n) = "--keep-yearly=" ++ val n
data KeepPolicy
= KeepHours Int
| KeepDays Int
| KeepWeeks Int
| KeepMonths Int
| KeepYears Int