# Copies a RasterizedDB data volume into a .tar.gz file, consistently: the # container using the volume is stopped for the copy and started again after. # # usage: .\backup.ps1 [output directory] # .\backup.ps1 rasterizeddb rasterizeddb-data .\backups # .\backup.ps1 rasterizeddb-rasterizeddb-1 rasterizeddb_data .\backups # Compose # # The copy is made by `tar` inside the RasterizedDB image (-Image to use # another image with GNU tar), as root, so it can read the server's files. param( [Parameter(Mandatory = $true, Position = 0)] [string] $Container, [Parameter(Mandatory = $true, Position = 1)] [string] $Volume, [Parameter(Position = 2)] [string] $OutDir = '.', [string] $Image = 'milendenev/rasterizeddb:1.1.0' ) $ErrorActionPreference = 'Stop' docker volume inspect $Volume | Out-Null if ($LASTEXITCODE -ne 0) { throw "There is no volume named $Volume." } New-Item -ItemType Directory -Force -Path $OutDir | Out-Null $out = (Resolve-Path $OutDir).Path $file = "$Volume-$((Get-Date).ToUniversalTime().ToString('yyyyMMddTHHmmssZ')).tar.gz" $running = (docker container inspect -f '{{.State.Running}}' $Container) if ($LASTEXITCODE -ne 0) { throw "There is no container named $Container." } if ($running -eq 'true') { Write-Host "Stopping $Container for a consistent copy..." docker stop -t 60 $Container | Out-Null if ($LASTEXITCODE -ne 0) { throw "docker stop $Container failed" } } try { docker run --rm --user 0 --entrypoint tar ` -v "${Volume}:/data:ro" ` --mount "type=bind,source=$out,target=/backup" ` $Image --numeric-owner -czf "/backup/$file" -C /data . if ($LASTEXITCODE -ne 0) { throw 'tar failed; no complete backup was written' } docker run --rm --user 0 --entrypoint tar ` --mount "type=bind,source=$out,target=/backup" ` $Image -tzf "/backup/$file" | Out-Null if ($LASTEXITCODE -ne 0) { throw "the archive $file does not read back" } } finally { if ($running -eq 'true') { docker start $Container | Out-Null Write-Host "Started $Container again." } } Write-Host "Wrote $(Join-Path $out $file)."