Last active
July 12, 2020 09:31
-
-
Save cvzi/30c2df3222470e3eb73d41222b957efb to your computer and use it in GitHub Desktop.
Backup all installed (third-party) .apk files from your Android phone to your computer. No root required.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
backupApks.py | |
Backup all installed (third-party) .apk files from your Android phone to your computer. | |
Requires adb/adb.exe in %path%. No root required. | |
You can download adb from here: https://developer.android.com/studio/releases/platform-tools.html | |
You should try to keep the phone screen on while downloading the apks, otherwise transfer speed might drop drastically. | |
""" | |
import os | |
import subprocess | |
outputDir = 'apks/' | |
tempfile = os.path.join(outputDir, '0temp.apk') | |
if not os.path.isdir(outputDir): | |
os.makedirs(outputDir) | |
packages = subprocess.run('adb shell pm list packages -3'.split(), stdout=subprocess.PIPE, shell=True) | |
packages = packages.stdout.decode('utf8').strip().split('\n') | |
errors = [] | |
skipped = [] | |
for line in packages: | |
package = line[8:].strip() | |
print(package) | |
cmd = 'adb shell pm path'.split() | |
cmd.append(package) | |
filenames = subprocess.run(cmd, stdout=subprocess.PIPE, shell=True).stdout.decode('utf8').strip().split('\n') | |
if len(filenames) > 0: | |
for file in filenames: | |
filepath = file[8:].strip() | |
filename = os.path.basename(filepath) | |
if filename == "base.apk" and len(filenames) == 1: | |
filename ='%s.apk' % package | |
else: | |
filename ='%s.%s' % (package, filename) | |
if os.path.isfile(os.path.join(outputDir, filename)): | |
print("Skipping: %s already exists" % filename) | |
skipped.append(filename) | |
continue | |
cmd = 'adb pull'.split() | |
cmd.append(filepath) | |
cmd.append(tempfile) | |
print("Press Ctrl-C to abort") | |
print(' '.join(cmd)) | |
try: | |
with subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) as p: | |
for line in p.stdout: | |
print(line.decode('utf8').strip(), end='\r') | |
os.rename(tempfile, os.path.join(outputDir, filename)) | |
except KeyboardInterrupt: | |
print("Aborted %s" % package) | |
errors.append(package) | |
print('\n') | |
print("") | |
if skipped: | |
print("Following packages were skipped:") | |
for ski in skipped: | |
print(ski) | |
print("") | |
if errors: | |
print("Following packages were aborted:") | |
for err in errors: | |
print(err) | |
else: | |
print("No errors") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment