From f70e58b707a93f2977fbba6b98d89441e0ea3b31 Mon Sep 17 00:00:00 2001 From: Félix Sipma Date: Sat, 23 Apr 2016 20:43:17 +0200 Subject: Attic: do not run prune if no KeepPolicy is specified --- src/Propellor/Property/Attic.hs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'src/Propellor') diff --git a/src/Propellor/Property/Attic.hs b/src/Propellor/Property/Attic.hs index 0fadc113..9fc79abd 100644 --- a/src/Propellor/Property/Attic.hs +++ b/src/Propellor/Property/Attic.hs @@ -52,10 +52,9 @@ backup dirs backupdir crontimes extraargs kp = propertyList (backupdir ++ " atti & Cron.niceJob ("attic_backup" ++ backupdir) crontimes (User "root") "/" backupcmd `requires` installed where - backupcmd = intercalate ";" - [ createCommand - , pruneCommand - ] + backupcmd = intercalate ";" $ + createCommand + : if null kp then [] else [pruneCommand] createCommand = unwords $ [ "attic" , "create" -- cgit v1.2.3 From ed52eec11b61d59b7a139b8f55c59961b0806d96 Mon Sep 17 00:00:00 2001 From: Félix Sipma Date: Thu, 28 Apr 2016 21:32:28 +0200 Subject: Attic: 'restored' and 'backup' only take one directory (API change) --- src/Propellor/Property/Attic.hs | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) (limited to 'src/Propellor') diff --git a/src/Propellor/Property/Attic.hs b/src/Propellor/Property/Attic.hs index 9fc79abd..504e3cfb 100644 --- a/src/Propellor/Property/Attic.hs +++ b/src/Propellor/Property/Attic.hs @@ -34,8 +34,9 @@ init backupdir = check (not <$> repoExists backupdir) (cmdProperty "attic" inita , backupdir ] -restored :: [FilePath] -> AtticRepo -> Property DebianLike -restored dirs backupdir = cmdProperty "attic" restoreargs +-- TODO: use restored from Obnam +restored :: FilePath -> AtticRepo -> Property DebianLike +restored dir backupdir = cmdProperty "attic" restoreargs `assume` MadeChange `describe` ("attic restore from " ++ backupdir) `requires` installed @@ -43,15 +44,22 @@ restored dirs backupdir = cmdProperty "attic" restoreargs restoreargs = [ "extract" , backupdir + , dir ] - ++ 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 +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] @@ -60,14 +68,14 @@ backup dirs backupdir crontimes extraargs kp = propertyList (backupdir ++ " atti , "create" , "--stats" ] - ++ extraargs ++ - [ backupdir ++ "::" ++ "$(date --iso-8601=ns --utc)" - , unwords dirs + ++ map shellEscape extraargs ++ + [ shellEscape backupdir ++ "::" ++ "$(date --iso-8601=ns --utc)" + , shellEscape dir ] pruneCommand = unwords $ [ "attic" , "prune" - , backupdir + , shellEscape backupdir ] ++ map keepParam kp -- cgit v1.2.3 From 67cb5eaecccddf65ecbd949aad6e82f6694ad4e5 Mon Sep 17 00:00:00 2001 From: Félix Sipma Date: Fri, 29 Apr 2016 11:21:02 +0200 Subject: Attic: use/adapt restored from Obnam --- src/Propellor/Property/Attic.hs | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) (limited to 'src/Propellor') diff --git a/src/Propellor/Property/Attic.hs b/src/Propellor/Property/Attic.hs index 504e3cfb..ef74afbd 100644 --- a/src/Propellor/Property/Attic.hs +++ b/src/Propellor/Property/Attic.hs @@ -34,18 +34,33 @@ init backupdir = check (not <$> repoExists backupdir) (cmdProperty "attic" inita , backupdir ] --- TODO: use restored from Obnam restored :: FilePath -> AtticRepo -> Property DebianLike -restored dir backupdir = cmdProperty "attic" restoreargs - `assume` MadeChange - `describe` ("attic restore from " ++ backupdir) - `requires` installed +restored dir backupdir = go `requires` installed where - restoreargs = - [ "extract" - , backupdir - , dir - ] + 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 -- cgit v1.2.3 From 9adfb7560fcd1186153bd743f885c12753abc9e5 Mon Sep 17 00:00:00 2001 From: Félix Sipma Date: Fri, 29 Apr 2016 11:33:23 +0200 Subject: Attic: add comments --- src/Propellor/Property/Attic.hs | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) (limited to 'src/Propellor') diff --git a/src/Propellor/Property/Attic.hs b/src/Propellor/Property/Attic.hs index ef74afbd..26f23500 100644 --- a/src/Propellor/Property/Attic.hs +++ b/src/Propellor/Property/Attic.hs @@ -25,6 +25,7 @@ installed = Apt.installed ["attic"] repoExists :: AtticRepo -> IO Bool repoExists repo = boolSystem "attic" [Param "list", File repo] +-- | Inits a new attic repository init :: AtticRepo -> Property DebianLike init backupdir = check (not <$> repoExists backupdir) (cmdProperty "attic" initargs) `requires` installed @@ -34,6 +35,13 @@ init backupdir = check (not <$> repoExists backupdir) (cmdProperty "attic" inita , backupdir ] +-- | Restores a directory from an attic backup. +-- +-- Only does anything if the directory does not exist, or exists, +-- but is completely empty. +-- +-- The restore is performed atomically; restoring to a temp directory +-- and then moving it to the directory. restored :: FilePath -> AtticRepo -> Property DebianLike restored dir backupdir = go `requires` installed where @@ -62,10 +70,31 @@ restored dir backupdir = go `requires` installed , return FailedChange ) +-- | Installs a cron job that causes a given directory to be backed +-- up, by running attic with some parameters. +-- +-- If the directory does not exist, or exists but is completely empty, +-- this Property will immediately restore it from an existing backup. +-- +-- So, this property can be used to deploy a directory of content +-- to a host, while also ensuring any changes made to it get backed up. +-- For example: +-- +-- > & Attic.backup "/srv/git" "root@myserver:/mnt/backup/git.attic" Cron.Daily +-- > ["--exclude=/srv/git/tobeignored"] +-- > [Attic.KeepDays 7, Attic.KeepWeeks 4, Attic.KeepMonths 6, Attic.KeepYears 1] +-- +-- Note that this property does not make attic encrypt the backup +-- repository. +-- +-- Since attic uses a fair amount of system resources, only one attic +-- backup job will be run at a time. Other jobs will wait their turns to +-- run. 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 +-- | Does a backup, but does not automatically restore. backup' :: FilePath -> AtticRepo -> Cron.Times -> [AtticParam] -> [KeepPolicy] -> Property DebianLike backup' dir backupdir crontimes extraargs kp = cronjob `describe` desc @@ -95,6 +124,10 @@ backup' dir backupdir crontimes extraargs kp = cronjob ++ map keepParam kp +-- | Constructs an AtticParam 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 attic prune to clean out +-- generations not specified here. keepParam :: KeepPolicy -> AtticParam keepParam (KeepHours n) = "--keep-hourly=" ++ show n keepParam (KeepDays n) = "--keep-daily=" ++ show n @@ -102,6 +135,10 @@ keepParam (KeepWeeks n) = "--keep-daily=" ++ show n keepParam (KeepMonths n) = "--keep-monthly=" ++ show n keepParam (KeepYears n) = "--keep-yearly=" ++ show n +-- | Policy for backup generations to keep. For example, KeepDays 30 will +-- keep the latest backup for each day when a backup was made, and keep the +-- last 30 such backups. When multiple KeepPolicies are combined together, +-- backups meeting any policy are kept. See attic's man page for details. data KeepPolicy = KeepHours Int | KeepDays Int -- cgit v1.2.3