Skip to content

Commit

Permalink
Add example script for restoring a backup
Browse files Browse the repository at this point in the history
This was originally contributed by @codygman and edited by me to adopt newer
`turtle` idioms
  • Loading branch information
Gabriella439 committed Dec 3, 2016
1 parent c1441e7 commit 43a0f23
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions examples/restoreBackup.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{-# LANGUAGE OverloadedStrings #-}

import Turtle

import qualified Control.Foldl as Fold

main = do
-- Backup the old database
inproc "mysqldump" ["-uroot", "myproject"] empty
& output "revert.sql"

let serverName = "lalala"
let backupDir = "/backups/db/myproject/"

-- Obtain newest filename from server
let query :: Shell Line
query = inproc "ssh"
[ serverName
, format ("ls -Art "%fp%" | tail -n 1") backupDir
]
empty
result <- fold query Fold.head
newestFileName <- case result of
Nothing -> die "Couldn't get backup path"
Just text -> return (fromText (lineToText text))

let backupFilePath = backupDir </> newestFileName
let localFilePath = "/tmp/" </> newestFileName

-- Download the backup
procs "rsync"
[ "-avcz"
, format (s%":"%fp) serverName backupFilePath
, format fp localFilePath
]
empty

let dbName = "myproject-copy"

-- Drop the old database
shells ("mysqladmin -uroot drop -f " <> dbName) empty

-- Restore the database from the downloaded backup
inproc "zcat" [format fp localFilePath] empty
& procs "mysql" ["-uroot", dbName]

echo "Backup restored"

0 comments on commit 43a0f23

Please sign in to comment.