-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add example script for restoring a backup
This was originally contributed by @codygman and edited by me to adopt newer `turtle` idioms
- Loading branch information
1 parent
c1441e7
commit 43a0f23
Showing
1 changed file
with
47 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |