bup-backup/bup-backup.hs

55 lines
1.4 KiB
Haskell
Raw Normal View History

2016-09-12 15:41:41 +02:00
#!/usr/bin/env nix-script
#!>haskell
#! shell | bup git
2019-08-08 22:25:57 +02:00
#! env | BUP_DIR BUP_SRV
2016-09-12 15:41:41 +02:00
#! haskell | shell-conduit filepath
2019-01-31 18:20:33 +01:00
import Data.List (intersperse)
2016-09-12 15:41:41 +02:00
import Data.Conduit.Shell
import Data.Conduit.Shell.Segments (strings)
import Data.Conduit.Shell.Variadic (variadicProcess)
import System.FilePath
2019-08-08 22:25:57 +02:00
import System.Environment (lookupEnv)
2016-09-12 15:41:41 +02:00
2019-01-31 18:20:33 +01:00
-- | regexs for excluded files
2016-09-12 15:41:41 +02:00
excluded :: [String]
2018-08-05 17:32:02 +02:00
excluded = intersperse "--exclude-rx"
2019-02-01 00:46:04 +01:00
[ "/rnhmjoj/game/"
, "/rnhmjoj/\\.cache/"
2019-08-08 23:40:59 +02:00
, "/rnhmjoj/\\.local/share/bup"
2018-08-05 17:32:02 +02:00
, "/unity3d/.+/.+/Unity/"
2016-09-12 15:41:41 +02:00
]
main :: IO ()
2019-08-08 22:25:57 +02:00
main = do
var <- lookupEnv "BUP_SRV"
case var of
Nothing -> run (echo "set the BUP_SRV variable to hostname:path")
Just server -> run $
mapM_ (backup server) =<< strings (bup "ls" "-r" server)
findPath :: String -> String -> Segment FilePath
findPath server path = do
files <- strings (bup "ls" "-A" "-r" server path)
if length files /= 1
then return ("/" </> final path)
else findPath server (path </> head files)
2016-09-12 15:41:41 +02:00
where final = concat . drop 2 . splitPath
2019-08-08 22:25:57 +02:00
backup :: String -> String -> Segment ()
backup server name = do
path <- findPath server (name </> "latest")
2019-08-09 00:25:11 +02:00
echo "indexing" name
2018-08-05 17:32:02 +02:00
proc "bup" ("index" : path : "--exclude-rx" : excluded)
2019-08-09 00:25:11 +02:00
echo "saving" name
2019-08-08 22:25:57 +02:00
bup "save" "-r" server "-n" name path
2016-09-12 15:41:41 +02:00
2019-01-31 18:20:33 +01:00
-- | Fix for missing execs on NixOS
2016-09-12 15:41:41 +02:00
bup :: ProcessType r => String -> r
bup = variadicProcess "bup"