Skip to content

Instantly share code, notes, and snippets.

@JeffreyVdb
Created August 6, 2018 11:11
Show Gist options
  • Save JeffreyVdb/1765a313a97f35807b876eecbb81d8b9 to your computer and use it in GitHub Desktop.
Save JeffreyVdb/1765a313a97f35807b876eecbb81d8b9 to your computer and use it in GitHub Desktop.
Backup github repositories to AWS S3
#!/usr/bin/pwsh
Param (
[Parameter(Mandatory=$True, Position=1)]
[string]$RepositoryName,
[Parameter(Mandatory=$True)]
[string]$BucketName,
[Parameter(Mandatory=$True)]
[string]$Organization = "",
[Parameter(Mandatory=$False)]
[string]$BucketPrefix = ""
)
function New-TemporaryDirectory {
$parent = [System.IO.Path]::GetTempPath()
[string] $name = [System.Guid]::NewGuid()
$fullDirectoryName = (Join-Path $parent $name)
New-Item -ItemType Directory -Path $fullDirectoryName | Out-Null
return $fullDirectoryName
}
$tempDir = ""
try {
Push-Location
$tempDir = New-TemporaryDirectory
Set-Location $tempDir
git clone "[email protected]:${Organization}/${RepositoryName}.git"
Set-Location $RepositoryName
$bundlePath = (Join-Path $tempDir "${RepositoryName}.bundle")
Write-Host "Creating bundle at $bundlePath"
git bundle create $bundlePath --all
if ($BucketPrefix -eq "") {
aws s3 cp $bundlePath "s3://${BucketName}"
}
else {
aws s3 cp $bundlePath "s3://${BucketName}/$($BucketPrefix -Replace "[/]+$", '')/"
}
}
finally {
Pop-Location
if ($tempDir -ne "") {
Remove-Item -Recurse -Confirm -Force "$tempDir"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment