#lang racket
(require (planet gh/aws/sdb)
(planet gh/aws/sns)
(planet gh/aws/glacier)
(planet gh/http/request))
(define path->archive-domain "examplesBackupPathToArchive")
(define archive->meta-domain "examplesBackupArchiveToMeta")
(define vault "examples.backup")
(define (ensure-assets)
(create-vault vault)
(create-domain path->archive-domain)
(create-domain archive->meta-domain))
(define/contract (archive-file path)
(path? . -> . void?)
(define path/string (path->string path))
(printf "~a\nUploading to Amazon Glacier ...\n" path/string)
(define archive-id (create-archive-from-file vault path))
(printf "Updating Amazon Simple Database with metadata ...\n")
(put-attributes path->archive-domain
path/string
`([ArchiveId ,archive-id]))
(put-attributes archive->meta-domain
archive-id
`([Size ,(number->string (file-size path))]
[Date ,(seconds->gmt-8601-string)]
[Path ,path/string]))
(void))
(define/contract (archive-directory path [sns-topic #f])
((path-string?) (string?) . ->* . void?)
(printf "Ensuring Amazon SDB and Glacier resources are created ...\n")
(ensure-assets)
(printf "Starting archive of all files under ~a ...\n" path)
(for ([x (in-directory path)])
(unless (or (directory-exists? x)
(equal? #\. (string-ref (path->string x) 0)))
(archive-file x)))
(when sns-topic
(publish sns-topic (format "Archive completed ~a." (seconds->gmt-string))))
(void))
(define root-dir
(path->string (simplify-path (path->complete-path (build-path 'up "tests")))))
(define sns-topic (match (list-topics) [(list x rest ...) x][else #f]))
(archive-directory root-dir sns-topic)
(select-hash (format "SELECT * FROM ~a" path->archive-domain))
(select-hash (format "SELECT * FROM ~a" archive->meta-domain))