Skip to content

Instantly share code, notes, and snippets.

@StoneLabs
Last active April 13, 2023 20:05
Show Gist options
  • Save StoneLabs/219bcd2ca3acc56585918feb196de1f1 to your computer and use it in GitHub Desktop.
Save StoneLabs/219bcd2ca3acc56585918feb196de1f1 to your computer and use it in GitHub Desktop.
Android storage backup script
echo ""
echo ""
echo "====== FILE PULLER ======"
echo ""
echo ""
$index = 0
if (Test-Path ./status) {
$confirmation = $Host.UI.PromptForChoice("", "It appears the last operation was interrupted. Continue?", @("&Continue", "&Restart", "&Exit"), 0)
if ($confirmation -eq 0) {
[int]$index = Get-Content ./status
echo ""
echo "Loaded index at $index"
}
if ($confirmation -eq 2) {
exit
}
}
if ($index -lt 1) {
echo ""
echo "Current index is 0. Reloading file list to start new operation..."
echo 0 > ./status
adb shell "find /storage/emulated -type f" > files.txt
} else {
echo "No file pull performed to continue last operation!"
}
$files = [System.Collections.Generic.List[string]]::new()
$invalid = 0
foreach($line in Get-Content .\files.txt) {
if($line -match "^/.*") {
$files.Add($line)
} else {
$invalid += 1
}
}
echo "Found $($files.Count) valid and $invalid invalid files."
echo ""
$confirmation = $Host.UI.PromptForChoice("", "Start pulling files?", @("&Yes", "&No"), 0)
if ($confirmation -ne 0) {
exit
}
echo ""
echo "Pulling $valid files..."
echo ""
$stopwatch = [System.Diagnostics.Stopwatch]::StartNew()
for (; $index -lt $files.Count; $index++) {
$file = $files[$index]
echo $index > ./status
if ($stopwatch.Elapsed.TotalMilliseconds -ge 100) {
Write-Progress -Activity "Pulling files" -Status "[$index / $($files.Count)] Pulling $file" -PercentComplete ($index / $files.Count * 100)
$stopwatch.Restart()
}
$parentFolder = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($(Split-Path -Path ".$file"))
if (!(Test-Path $parentFolder))
{
New-Item -Path $parentFolder -ItemType Directory *>$null
}
echo -n "$file "
adb pull -a "$file" ".$file" *>$null
if($LASTEXITCODE -ne 0) {
echo ""
Write-Host "Error on last pull operation! Retrying..." -ForegroundColor Yellow
echo -n "$file "
echo adb pull -a "$file" ".$file"
adb pull -a "$file" ".$file"
if($LASTEXITCODE -ne 0) {
echo ""
Write-Host "Failed again! Please review." -ForegroundColor Red
echo ""
$confirmation = $Host.UI.PromptForChoice("", "Continue or Exit (Progress is saved)?", @("&Continue", "&Exit"), 0)
if ($confirmation -ne 0) {
exit
}
}
}
}
Write-Progress -Activity "Pulling files" -Completed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment