diff options
| author | Joey Hess <joeyh@joeyh.name> | 2017-09-25 14:37:47 -0400 |
|---|---|---|
| committer | Joey Hess <joeyh@joeyh.name> | 2017-09-25 14:37:47 -0400 |
| commit | 0732930a105a29809affd42900b69025a892f003 (patch) | |
| tree | b528da1e04470a2a9f3cccecfe3aa6ad35708cf6 /src/Propellor/Property/Borg.hs | |
| parent | 49f477bc84842e9f70d6acea98953d4f1db6cad7 (diff) | |
| parent | fd4dd67f54f93a79bc9ec5977408f7821e1b0b56 (diff) | |
Merge branch 'joeyconfig'
Diffstat (limited to 'src/Propellor/Property/Borg.hs')
| -rw-r--r-- | src/Propellor/Property/Borg.hs | 84 |
1 files changed, 63 insertions, 21 deletions
diff --git a/src/Propellor/Property/Borg.hs b/src/Propellor/Property/Borg.hs index ace7a48b..74d71e3d 100644 --- a/src/Propellor/Property/Borg.hs +++ b/src/Propellor/Property/Borg.hs @@ -3,7 +3,10 @@ -- Support for the Borg backup tool <https://github.com/borgbackup> module Propellor.Property.Borg - ( installed + ( BorgParam + , BorgRepo(..) + , BorgRepoOpt(..) + , installed , repoExists , init , restored @@ -17,9 +20,39 @@ import qualified Propellor.Property.Apt as Apt import qualified Propellor.Property.Cron as Cron import Data.List (intercalate) +-- | Parameter to pass to a borg command. type BorgParam = String -type BorgRepo = FilePath +-- | A borg repository. +data BorgRepo + -- | Location of the repository, eg + -- `BorgRepo "root@myserver:/mnt/backup/git.borg"` + = BorgRepo String + -- | Location of the repository, and additional options to use + -- when accessing the repository. + | BorgRepoUsing [BorgRepoOpt] String + +data BorgRepoOpt + -- | Use to specify a ssh private key to use when accessing a + -- BorgRepo. + = UseSshKey FilePath + +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", k) installed :: Property DebianLike installed = withOS desc $ \w o -> case o of @@ -31,19 +64,20 @@ installed = withOS desc $ \w o -> case o of desc = "installed borgbackup" repoExists :: BorgRepo -> IO Bool -repoExists repo = boolSystem "borg" [Param "list", File repo] +repoExists repo = runBorg repo [Param "list", Param (repoLoc repo)] -- | Inits a new borg repository init :: BorgRepo -> Property DebianLike -init backupdir = check (not <$> repoExists backupdir) (cmdProperty "borg" initargs) - `requires` installed +init repo = check (not <$> repoExists repo) + (cmdPropertyEnv "borg" initargs (runBorgEnv repo)) + `requires` installed where initargs = [ "init" - , backupdir + , repoLoc repo ] --- | Restores a directory from an borg backup. +-- | Restores a directory from a borg backup. -- -- Only does anything if the directory does not exist, or exists, -- but is completely empty. @@ -51,7 +85,7 @@ init backupdir = check (not <$> repoExists backupdir) (cmdProperty "borg" initar -- The restore is performed atomically; restoring to a temp directory -- and then moving it to the directory. restored :: FilePath -> BorgRepo -> Property DebianLike -restored dir backupdir = go `requires` installed +restored dir repo = go `requires` installed where go :: Property DebianLike go = property (dir ++ " restored by borg") $ ifM (liftIO needsRestore) @@ -64,9 +98,9 @@ restored dir backupdir = go `requires` installed needsRestore = null <$> catchDefaultIO [] (dirContents dir) restore = withTmpDirIn (takeDirectory dir) "borg-restore" $ \tmpdir -> do - ok <- boolSystem "borg" $ + ok <- runBorg repo $ [ Param "extract" - , Param backupdir + , Param (repoLoc repo) , Param tmpdir ] let restoreddir = tmpdir ++ "/" ++ dir @@ -88,7 +122,9 @@ restored dir backupdir = go `requires` installed -- to a host, while also ensuring any changes made to it get backed up. -- For example: -- --- > & Borg.backup "/srv/git" "root@myserver:/mnt/backup/git.borg" Cron.Daily +-- > & Borg.backup "/srv/git" +-- > (BorgRepo "root@myserver:/mnt/backup/git.borg") +-- > Cron.Daily -- > ["--exclude=/srv/git/tobeignored"] -- > [Borg.KeepDays 7, Borg.KeepWeeks 4, Borg.KeepMonths 6, Borg.KeepYears 1] -- @@ -99,42 +135,48 @@ restored dir backupdir = go `requires` installed -- backup job will be run at a time. Other jobs will wait their turns to -- run. backup :: FilePath -> BorgRepo -> Cron.Times -> [BorgParam] -> [KeepPolicy] -> Property DebianLike -backup dir backupdir crontimes extraargs kp = backup' dir backupdir crontimes extraargs kp - `requires` restored dir backupdir +backup dir repo crontimes extraargs kp = backup' dir repo crontimes extraargs kp + `requires` restored dir repo -- | Does a backup, but does not automatically restore. backup' :: FilePath -> BorgRepo -> Cron.Times -> [BorgParam] -> [KeepPolicy] -> Property DebianLike -backup' dir backupdir crontimes extraargs kp = cronjob +backup' dir repo crontimes extraargs kp = cronjob `describe` desc `requires` installed where - desc = backupdir ++ " borg backup" + 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 ";" $ - createCommand - : if null kp then [] else [pruneCommand] + 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 backupdir ++ "::" ++ "$(date --iso-8601=ns --utc)" + [ shellEscape (repoLoc repo) ++ "::" ++ "$(date --iso-8601=ns --utc)" , shellEscape dir ] pruneCommand = unwords $ [ "borg" , "prune" - , shellEscape backupdir + , shellEscape (repoLoc repo) ] ++ map keepParam kp -- | Constructs an BorgParam that specifies which old backup generations to -- keep. By default, all generations are kept. However, when this parameter is --- passed to the `backup` property, they will run borg prune to clean out +-- passed to the `backup` property, it will run borg prune to clean out -- generations not specified here. keepParam :: KeepPolicy -> BorgParam keepParam (KeepHours n) = "--keep-hourly=" ++ val n |
