#!/usr/bin/env nix-script #!>haskell #! shell | bup git #! env | BUP_DIR BUP_SRV #! haskell | shell-conduit filepath import Data.List (intersperse) import Data.Conduit.Shell import Data.Conduit.Shell.Segments (strings) import Data.Conduit.Shell.Variadic (variadicProcess) import System.FilePath import System.Environment (lookupEnv) -- | regexs for excluded files excluded :: [String] excluded = intersperse "--exclude-rx" [ "/rnhmjoj/game/" , "/rnhmjoj/\\.cache/" , "/rnhmjoj/\\.local/share/bup" , "/unity3d/.+/.+/Unity/" ] main :: IO () 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) where final = concat . drop 2 . splitPath backup :: String -> String -> Segment () backup server name = do path <- findPath server (name "latest") proc "bup" ("index" : path : "--exclude-rx" : excluded) bup "save" "-r" server "-n" name path -- | Fix for missing execs on NixOS bup :: ProcessType r => String -> r bup = variadicProcess "bup"