2014-04-13 02:36:36 +00:00
|
|
|
module Propellor.Property.Obnam where
|
|
|
|
|
|
|
|
import Propellor
|
|
|
|
import qualified Propellor.Property.Apt as Apt
|
|
|
|
import qualified Propellor.Property.Cron as Cron
|
|
|
|
import Utility.SafeCommand
|
|
|
|
|
2014-04-13 06:28:40 +00:00
|
|
|
import Data.List
|
|
|
|
|
2014-04-13 02:36:36 +00:00
|
|
|
type ObnamParam = String
|
|
|
|
|
2014-04-13 06:28:40 +00:00
|
|
|
-- | An obnam repository can be used by multiple clients. Obnam uses
|
|
|
|
-- locking to allow only one client to write at a time. Since stale lock
|
|
|
|
-- files can prevent backups from happening, it's more robust, if you know
|
|
|
|
-- a repository has only one client, to force the lock before starting a
|
|
|
|
-- backup. Using OnlyClient allows propellor to do so when running obnam.
|
|
|
|
data NumClients = OnlyClient | MultipleClients
|
|
|
|
deriving (Eq)
|
|
|
|
|
2014-04-13 02:36:36 +00:00
|
|
|
-- | Installs a cron job that causes a given directory to be backed
|
|
|
|
-- up, by running obnam 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.
|
|
|
|
-- And since Obnam encrypts, just make this property depend on a gpg
|
|
|
|
-- key, and tell obnam to use the key, and your data will be backed
|
|
|
|
-- up securely. For example:
|
|
|
|
--
|
|
|
|
-- > & Obnam.backup "/srv/git" "33 3 * * *"
|
2014-04-13 06:28:40 +00:00
|
|
|
-- > [ "--repository=sftp://2318@usw-s002.rsync.net/~/mygitrepos.obnam"
|
2014-04-13 02:36:36 +00:00
|
|
|
-- > , "--encrypt-with=1B169BE1"
|
2014-04-13 06:28:40 +00:00
|
|
|
-- > ] Obnam.OnlyClient
|
2014-04-13 02:36:36 +00:00
|
|
|
-- > `requires` Gpg.keyImported "1B169BE1" "root"
|
|
|
|
-- > `requires` Ssh.keyImported SshRsa "root"
|
|
|
|
--
|
|
|
|
-- How awesome is that?
|
2014-04-13 06:28:40 +00:00
|
|
|
backup :: FilePath -> Cron.CronTimes -> [ObnamParam] -> NumClients -> Property
|
|
|
|
backup dir crontimes params numclients = cronjob `describe` desc
|
2014-04-13 02:36:36 +00:00
|
|
|
`requires` restored dir params
|
|
|
|
where
|
|
|
|
desc = dir ++ " backed up by obnam"
|
|
|
|
cronjob = Cron.niceJob ("obnam_backup" ++ dir) crontimes "root" "/" $
|
2014-04-17 23:32:34 +00:00
|
|
|
intercalate ";" $ catMaybes
|
2014-04-13 06:28:40 +00:00
|
|
|
[ if numclients == OnlyClient
|
|
|
|
then Just $ unwords $
|
|
|
|
[ "obnam"
|
|
|
|
, "force-lock"
|
|
|
|
] ++ map shellEscape params
|
|
|
|
else Nothing
|
|
|
|
, Just $ unwords $
|
|
|
|
[ "obnam"
|
|
|
|
, "backup"
|
|
|
|
, shellEscape dir
|
|
|
|
] ++ map shellEscape params
|
|
|
|
]
|
2014-04-13 02:36:36 +00:00
|
|
|
|
|
|
|
-- | Restores a directory from an obnam 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 -> [ObnamParam] -> Property
|
2014-04-18 07:59:06 +00:00
|
|
|
restored dir params = property (dir ++ " restored by obnam") go
|
2014-04-13 02:36:36 +00:00
|
|
|
`requires` installed
|
|
|
|
where
|
|
|
|
go = ifM (liftIO needsRestore)
|
2014-04-13 15:14:43 +00:00
|
|
|
( do
|
|
|
|
warningMessage $ dir ++ " is empty/missing; restoring from backup ..."
|
|
|
|
liftIO restore
|
2014-04-13 02:36:36 +00:00
|
|
|
, noChange
|
|
|
|
)
|
|
|
|
|
|
|
|
needsRestore = null <$> catchDefaultIO [] (dirContents dir)
|
|
|
|
|
|
|
|
restore = withTmpDirIn (takeDirectory dir) "obnam-restore" $ \tmpdir -> do
|
|
|
|
ok <- boolSystem "obnam" $
|
|
|
|
[ Param "restore"
|
|
|
|
, Param "--to"
|
|
|
|
, Param tmpdir
|
|
|
|
] ++ map Param params
|
|
|
|
let restoreddir = tmpdir ++ "/" ++ dir
|
|
|
|
ifM (pure ok <&&> doesDirectoryExist restoreddir)
|
|
|
|
( do
|
|
|
|
void $ tryIO $ removeDirectory dir
|
|
|
|
renameDirectory restoreddir dir
|
|
|
|
return MadeChange
|
|
|
|
, return FailedChange
|
|
|
|
)
|
2014-04-18 02:09:29 +00:00
|
|
|
|
2014-04-18 02:25:09 +00:00
|
|
|
installed :: Property
|
|
|
|
installed = Apt.installed ["obnam"]
|
2014-04-18 02:09:29 +00:00
|
|
|
|
2014-04-18 02:25:09 +00:00
|
|
|
-- | Ensures that a recent version of obnam gets installed.
|
|
|
|
--
|
2014-04-18 03:32:42 +00:00
|
|
|
-- Only does anything for Debian Stable.
|
2014-04-18 02:25:09 +00:00
|
|
|
latestVersion :: Property
|
2014-04-18 03:32:42 +00:00
|
|
|
latestVersion = withOS "obnam latest version" $ \o -> case o of
|
|
|
|
(Just (System (Debian suite) _)) | isStable suite -> ensureProperty $
|
|
|
|
Apt.setSourcesListD (sources suite) "obnam"
|
|
|
|
`requires` toProp (Apt.trustsKey key)
|
|
|
|
_ -> noChange
|
2014-04-18 02:25:09 +00:00
|
|
|
where
|
2014-04-18 03:32:42 +00:00
|
|
|
sources suite =
|
|
|
|
[ "deb http://code.liw.fi/debian " ++ Apt.showSuite suite ++ " main"
|
|
|
|
]
|
2014-04-18 02:25:09 +00:00
|
|
|
-- gpg key used by the code.liw.fi repository.
|
|
|
|
key = Apt.AptKey "obnam" $ unlines
|
|
|
|
[ "-----BEGIN PGP PUBLIC KEY BLOCK-----"
|
|
|
|
, "Version: GnuPG v1.4.9 (GNU/Linux)"
|
|
|
|
, ""
|
|
|
|
, "mQGiBEfzuTgRBACcVNG/H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