Android Files Backup

Android Files Backup

Recently, I came into the possession of a used OnePlus 6. The owner had no more use for it and was already using a newer phone. Before giving the phone to someone else though, she ideally wanted to have a backup of all the files and photos. So before flasing LineageOS, I used a few linux commands that allowed me to quickly grab all the relevant files.

πŸ’‘
The Android Debug Bridge (adb) must be installed on your device for this guide to work, it is part of the Anroid SDK Platform Tools and can be downloaded here.

Once you have adb installed, USB debugging must be enabled on your phone. Follow the official guide here to enable it and connect to your phone via USB.

1. Get a list of all files πŸ“œ

We use adb to execute execute our commands inside a shell on the device. With find we lookup all files with their full path from the sdcard and it's subdirectories. These files are then filtered by a regex provided to grep, that will only allow files that have one of the extensions we are looking for. Lastly the result is saved in a text file.

adb shell "find /sdcard/**/* -type f | grep -E '.*\.(png|jpg|jpeg|mp4|mp3|gif)'" > files.txt    

πŸ’‘
When using find on Linux we can directly supply a regex filter, the version supplied by toybox on Android does not support this though, hence the grep workaround.

2. Filter files

After scanning the files.txt output, I noticed a bunch of files that were not relevant and should not be included in the backup. I repeatedly used sed to remove these from our files list. Check here if you want to learn more about sed. For example, the command to remove all camera thumbnails looked like this:

sed --in-place '\|^/sdcard/DCIM/.thumbnails/.*$|d' files.txt    

3. Pull files

We can leverage xargs to execute a command for every newline read from our files list. Before we execute adb pull to download the file, we create the directory locally with mkdir, this allows us to keep the files inside the same folder structure as they reside on the phone. Depending on the amount of data you pull from your phone, the command might take a while to complete. Once finished, you should have all the files available on your computer in the directory sdcard.

cat files.txt | xargs -I {} sh -c 'mkdir -p ".$(dirname "$1")"; adb pull "$1" ".$(dirname "$1")"' - {}   

4. Confirm (Optional)

Just to make sure we grabbed all the files, we can do a simple comparison. The first command gives us the number of files we pulled, whereas the second command gives us the number of files we wanted to retrieve. If everything went well, the numbers should match 🀞

find sdcard -type f | wc --lines
wc --lines files.txt

Last but not least we can use du on the sdcard directory, to tells us how much space the files occupy.

du --human-readable --summarize sdcard