module Propellor.Property.Borg
( BorgParam
, BorgRepo(..)
, BorgRepoOpt(..)
, 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 BorgParam = String
data BorgRepo
= BorgRepo String
| BorgRepoUsing [BorgRepoOpt] String
data BorgRepoOpt
= UseSshKey FilePath
| UsesEnvVar (String, String)
repoLoc :: BorgRepo -> String
repoLoc (BorgRepo s) = s
repoLoc (BorgRepoUsing _ s) = s
runBorg :: BorgRepo -> [CommandParam] -> IO Bool
runBorg repo ps = case runBorgEnv repo of
[] -> boolSystem "borg" ps
environ -> do
environ' <- addEntries environ <$> getEnvironment
boolSystemEnv "borg" ps (Just environ')
runBorgEnv :: BorgRepo -> [(String, String)]
runBorgEnv (BorgRepo _) = []
runBorgEnv (BorgRepoUsing os _) = map go os
where
go (UseSshKey k) = ("BORG_RSH", "ssh -i " ++ k)
go (UsesEnvVar (k, v)) = (k, v)
installed :: Property DebianLike
installed = withOS desc $ \w o -> case o of
(Just (System (Debian _ (Stable "jessie")) _)) -> ensureProperty w $
Apt.backportInstalled ["borgbackup", "python3-msgpack"]
_ -> ensureProperty w $
Apt.installed ["borgbackup"]
where
desc = "installed borgbackup"
repoExists :: BorgRepo -> IO Bool
repoExists repo = runBorg repo [Param "list", Param (repoLoc repo)]
init :: BorgRepo -> Property DebianLike
init repo = check (not <$> repoExists repo)
(cmdPropertyEnv "borg" initargs (runBorgEnv repo))
`requires` installed
where
initargs =
[ "init"
, repoLoc repo
]
restored :: FilePath -> BorgRepo -> Property DebianLike
restored dir repo = go `requires` installed
where
go :: Property DebianLike
go = property (dir ++ " restored by borg") $ ifM (liftIO needsRestore)
( do
warningMessage $ dir ++ " is empty/missing; restoring from backup ..."
liftIO restore
, noChange
)
needsRestore = isUnpopulated dir
restore = withTmpDirIn (takeDirectory dir) "borg-restore" $ \tmpdir -> do
ok <- runBorg repo $
[ Param "extract"
, Param (repoLoc repo)
, 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 -> BorgRepo -> Cron.Times -> [BorgParam] -> [KeepPolicy] -> Property DebianLike
backup dir repo crontimes extraargs kp = backup' dir repo crontimes extraargs kp
`requires` restored dir repo
backup' :: FilePath -> BorgRepo -> Cron.Times -> [BorgParam] -> [KeepPolicy] -> Property DebianLike
backup' dir repo crontimes extraargs kp = cronjob
`describe` desc
`requires` installed
where
desc = repoLoc repo ++ " borg backup"
cronjob = Cron.niceJob ("borg_backup" ++ dir) crontimes (User "root") "/" $
"flock " ++ shellEscape lockfile ++ " sh -c " ++ shellEscape backupcmd
lockfile = "/var/lock/propellor-borg.lock"
backupcmd = intercalate "&&" $ concat
[ concatMap exportenv (runBorgEnv repo)
, [createCommand]
, if null kp then [] else [pruneCommand]
]
exportenv (k, v) =
[ k ++ "=" ++ shellEscape v
, "export " ++ k
]
createCommand = unwords $
[ "borg"
, "create"
, "--stats"
]
++ map shellEscape extraargs ++
[ shellEscape (repoLoc repo) ++ "::" ++ "$(date --iso-8601=ns --utc)"
, shellEscape dir
]
pruneCommand = unwords $
[ "borg"
, "prune"
, shellEscape (repoLoc repo)
]
++
map keepParam kp
keepParam :: KeepPolicy -> BorgParam
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