Skip to content

Instantly share code, notes, and snippets.

@Ujjwal0501
Last active July 31, 2022 21:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ujjwal0501/a57156c55959ce6cfe8e993469ba315d to your computer and use it in GitHub Desktop.
Save Ujjwal0501/a57156c55959ce6cfe8e993469ba315d to your computer and use it in GitHub Desktop.
Bash script to make a backup of the android user data
#!/bin/bash
LOG_FILE=~/android_backup.log
DISOLVE_DIRECTORY=0
if [[ $BACKUP_SRC -eq "" ]]; then BACKUP_SRC=storage/self/primary; fi
if [[ $BACKUP_DST -eq "" ]]; then BACKUP_DST=$HOME/AndroidBackup; fi
IFS=$'\n' # set the Internal Field Separator to newline for iterating over file and directory list output of ls
usage() { echo "Usage: $0 [-s <android-source-folder>] [-d <backup-directory-path>] [-m]\n\t-x " 1>&2; exit 1; }
stop() { echo "Exiting..."; echo "No backup made!!"; exit 0; }
while getopts ":s:d:" o; do
case "${o}" in
d)
BACKUP_DST=${OPTARG}
;;
m)
DISOLVE_DIRECTORY=1
;;
s)
BACKUP_SRC=${OPTARG}
;;
*)
usage
;;
esac
done
shift $((OPTIND-1))
if [[ ! -d "$BACKUP_DST" ]]; then
echo -e "Backup directory does not exist\n";
read -p "Do you want to create the folder? (yes/[No])" choice
case "$choice" in
[Yy][Ee][sS]|[Yy])
mkdir "$BACKUP_DST" -p || exit 1
;;
*)
stop
;;
esac
fi
if [[ "$(which adb)" == "" ]]; then echo -e "adb not found!!\nadb installation is required to work"; stop; fi
echo -e "\nBackup started on `date`\n=====================================================" >> $LOG_FILE
directory_list=( "$(adb shell ls "$BACKUP_SRC" -R 2>&1 | grep :$ | cut -f -1 -d ':')" )
directory_count=$(echo "${directory_list[@]}" | wc -l 2>>$LOG_FILE)
count=0
echo -e "$count/$directory_count copying '$dir'";
for dir in ${directory_list[@]}; do
echo -ne "\r$count/$directory_count copying '$dir'";
mkdir -p "$BACKUP_DST/$dir" || exit
file_list=( "$(adb shell ls -p \"$dir\" | grep -v /$ 2>>$LOG_FILE)" );
for file in ${file_list[@]}; do
adb pull "$dir/$file" "$BACKUP_DST/$dir/$file" 2>>$LOG_FILE 1>>$LOG_FILE
done
echo -ne "\033[1K";
(( count++ ));
echo -ne "\r$count/$directory_count copying '$dir'";
done
echo -e "\ncompleted"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment