Skip to content

Instantly share code, notes, and snippets.

Created November 1, 2010 03:03
What would you like to do?
Powershell script that uses the cmd line tool to download emails from gmail in chunks of X days and store them in separate folders
## usage
## Download gmail-backup from and put it under "tools" folder relative to the script
## The idea is to download emails in small batches to avoid network issues and make a re-start easier. It also helps to avoid huge folders
## INPUT: the storage folder, amount of days to make the cut, start date, end date, credentials
## new-backup "d:\mail" 30 (get-date -year 2004 -month 01 -day 01) (get-date -y 2006 -month 01 -day 01) "youremail" "yourpwd"
function log($msg) {
$msg = (get-date).ToString("yyyyMMdd hh:mm:ss") + " $msg"
add-content log.txt $msg
function new-backup($rootFolder, $daysBatch, $start, $end, $mail, $pwd) {
$from = $start
do {
$to = $from.AddDays($daysBatch)
$fromFormatted = $from.ToString("yyyyMMdd")
$toFormatted = $to.ToString("yyyyMMdd")
$path = [System.IO.Path]::Combine($rootFolder, "$fromFormatted-$toFormatted")
if (-not (test-path $path)) {
$newPath = new-item -path $path -type directory
write-host "Backing up $fromFormatted to $toFormatted"
write-host "NOTE: if you want to see the progress, open log.txt"
log "Starting backup of $fromFormatted to $toFormatted"
$execPath = [System.IO.Path]::Combine((Get-Location), "tool\gmail-backup.exe")
$params = "backup $path $mail $pwd $fromFormatted $toFormatted"
start-process -FilePath $execPath -ArgumentList $params -RedirectStandardOutput temp.txt -Wait
add-content log.txt (get-content temp.txt)
log "Finished backup of $fromFormatted to $toFormatted"
remove-item temp.txt
$from = $to
} while ($from -lt $end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment