Skip to content

Instantly share code, notes, and snippets.

@LongboatAline
Created April 17, 2018 10:27
Show Gist options
  • Save LongboatAline/48e3c7de528a993648b9414883fc34cb to your computer and use it in GitHub Desktop.
Save LongboatAline/48e3c7de528a993648b9414883fc34cb to your computer and use it in GitHub Desktop.
Backup AAPS/ruffy/xDrip+ configuration and log files relevant for debugging and restoring a previous combo pairing.
#!/bin/sh
# Trying to backup AAPS related data into a directory per device for later perusal
# Install: Configure Path variable section before use, comment out those parts you
# do not want to archive (bugreport, wlan configuration,...
# To use, connect (only) your phone used to run AAPS via usb, and call this script
# Check we only have one Android Device connected, and are allowed to access it via adb.
adb start-server || echo $0: Could not start adb server!
device=$(adb devices |awk 'NR == 2 {print $1}')
if [ -z $device ]
then
echo $0: No device found, please make sure USB debugging is enabled and allowed for this host.
exit 0
fi
if [ `(adb devices | wc -l)` -eq "3" ]
then
echo $0: Device $device found
else
echo $0: Please make sure only the device to backup is connected.
exit 0
fi
#-----------------------------------------------------------------------------------------------
# Paths - please verify and configure where needed
#-----------------------------------------------------------------------------------------------
# Where should your backups go:
backup=/Volumes/export/Android/Backup/${device}
xDriparchive=${backup}/xDrip
AAPSarchive=${backup}/AAPS
# Path to your sdcard (where AAPS writes its preference file to)
AAPSprefpath=/storage/emulated/0
sdcard=/sdcard
ruffypath=/data/data/org.monkey.d.ruffy.ruffy
AAPSpref=AndroidAPSPreferences
AAPSlog=Android/data/info.nightscout.androidaps/files
# These might be different on your phone (or not apply at all)
Screenshots=Pictures/Screenshots
TiBackup=TitaniumBackup
#TiBackup=
# End of configurable section
#-----------------------------------------------------------------------------------------------
btservice=`adb shell dumpsys -l |grep bluet | awk '{$1=$1};1' `
for i in ${backup} ${AAPSarchive} ${xDriparchive} ${backup}/Bluetooth ${backup}/Properties ${backup}/Bugreports ${backup}/wifi
do
if [ ! -d ${i} ]
then
mkdir -p ${i} || { echo "$0: Could not create Archive directory ${i}, exiting" ; exit 1; }
echo "$0: Created archive directory ${i}".
fi
done
#############################################
# Date format
# with seconds:
# datefmt="+%Y-%m-%d|%T"
#datefmt="+%Y-%m-%d|%H:%M"
datefmt="+%Y-%m-%d_%H%M"
# One file per month
filefmt="+%Y-%m"
LOGM="`date ${filefmt}`.csv"
LOGD=${LOGM}-`date "+%d"`
YY=`date "+%Y"`
#############################################
#############################################
# Field 01: UTC - normalized time
# Field 02: Localtime (or device time)
yesterday=`TZ=CET+24 /bin/date '+%b %e'`
now=`/bin/date ${datefmt}`
utc=`TZ=UTC /bin/date ${datefmt}`
#############################################
# Get data, and archive it into one directory per device
adb shell getprop > $backup/Properties/properties-${now}
adb pull -a ${AAPSprefpath}/${AAPSpref} ${AAPSarchive}/${AAPSpref}-$now && echo "=== archived as ${AAPSarchive}/${AAPSpref}-${now}"
adb pull -a ${AAPSprefpath}/${AAPSlog} ${AAPSarchive}/ && echo "=== archived in ${AAPSarchive}/files"
adb pull -a ${AAPSprefpath}/${Screenshots} ${backup}/ && echo "=== archived in ${backup}/Screenshots"
#
# TODO: delete *older* AAPS logfiles and Screenshots on device after transfer
# adb shell rm /storage/emulated/0/Pictures/Screenshots/\*.png
#
if [ -z $TiBackup ]
then
echo "Archiving Titanium Backup storage directory as well - warning: this might grow to be rather big"
# adb pull -a ${AAPSprefpath}/${TiBackup} ${backup}/ && echo "=== archived in ${backup}/TitaniumBackup"
fi
mkdir ${xDriparchive}/${now}
# Dated archive files - stacking into one directory to avoid unnecessary duplication
adb pull -a ${sdcard}/xdrip ${backup}&& echo "Pulled ${sdcard}/xdrip"
adb pull -a ${sdcard}/Download/xDrip ${xDriparchive}/${now} && echo "=== archived in ${xDriparchive}-${now}"
rmdir ${xDriparchive}/${now} && echo "=== (nothing there - removing empty directory)"
adb root
# takes some time
# adb bugreport ${backup}/Bugreports/bugreport-${device}-${now}
# Brute force: Try to get one of the bluetooth config files we know of
echo "Trying to fetch bluetooth information from all over the place, expect some attempts to fail"
echo "Found Bluetooth information in the following directories:" > ${backup}/Bluetooth/location.txt
for i in \
/system/etc/bluetooth \
/sys/class/bluetooth \
/sys/kernel/debug/bluetooth \
/sys/module/bluetooth \
/storage/emulated/0/bluetooth \
/mnt/runtime/write/emulated/0/bluetooth \
/mnt/runtime/read/emulated/0/bluetooth \
/mnt/runtime/default/emulated/0/bluetooth \
/data/media/0/bluetooth \
/data/misc/bluetooth \
/data/misc/bluedroid
do
adb pull ${i} ${backup}/Bluetooth/ && ( echo ${i} >> ${backup}/Bluetooth/location.txt; echo "Bluetooth information discovered in ${i}, archiving." )
done
adb shell dumpsys ${btservice} > ${backup}/Bluetooth/${btservice}-${now}
if [ -d ${backup}/Bluetooth/bluedroid/ ]
then
adb pull /data/misc/bluedroid/bt_config.conf ${backup}/Bluetooth/bluedroid/bt_config.conf-${now} && echo "=== archived Bluetooth pairing information in ${backup}/Bluetooth/bluedroid/bt_config.conf-${now}"
fi
# /data/data/org.monkey.d.ruffy.ruffy
adb pull ${ruffypath} ${backup}/
adb pull ${ruffypath}/shared_prefs/pumpdata.xml ${backup}/org.monkey.d.ruffy.ruffy/shared_prefs/pumpdata.xml-${now} && echo "=== archived ruffy pairing information in ${backup}/org.monkey.d.ruffy.ruffy/shared_prefs/pumpdata.xml-${now}"
# Contains access configuration/passwords (unless using certificate-based access)
adb pull /data/misc/wifi/wpa_supplicant.conf ${backup}/wifi/wpa_supplicant.conf-${now} && echo "=== archived wlan configuration in ${backup}/wifi/wpa_supplicant.conf-${now}"
@LongboatAline
Copy link
Author

LongboatAline commented Apr 17, 2018

output

(Titanium Backup and Bugreport disabled to save some time)

aaps-backup.sh: Device TA348000UM found
/storage/emulated/0/AndroidAPSPreferences: 1 file pulled. 5.1 MB/s (20079 bytes in 0.004s)
=== archived as /Volumes/export/Android/Backup/TA348000UM/AAPS/AndroidAPSPreferences-2018-04-17_1220
/storage/emulated/0/Android/data/info.nightscout.androidaps/files/: 10 files pulled. 17.0 MB/s (4432528 bytes in 0.248s)
=== archived in /Volumes/export/Android/Backup/TA348000UM/AAPS/files
/storage/emulated/0/Pictures/Screenshots/: 1 file pulled. 10.7 MB/s (195265 bytes in 0.017s)
=== archived in /Volumes/export/Android/Backup/TA348000UM/Screenshots
/sdcard/xdrip/: 46 files pulled. 16.3 MB/s (23830953 bytes in 1.392s)
Pulled /sdcard/xdrip
adb: error: remote object '/sdcard/Download/xDrip' does not exist
=== (nothing there - removing empty directory)
adbd is already running as root
Trying to fetch bluetooth information from all over the place, expect some attempts to fail
/system/etc/bluetooth/: 2 files pulled. 0.3 MB/s (4872 bytes in 0.014s)
Bluetooth information discovered in /system/etc/bluetooth, archiving.
/sys/class/bluetooth/: 0 files pulled.
Bluetooth information discovered in /sys/class/bluetooth, archiving.
/sys/kernel/debug/bluetooth/: 4 files pulled. 0.0 MB/s (65 bytes in 0.018s)
Bluetooth information discovered in /sys/kernel/debug/bluetooth, archiving.
/sys/module/bluetooth/: 7 files pulled. 0.0 MB/s (17 bytes in 0.035s)
Bluetooth information discovered in /sys/module/bluetooth, archiving.
adb: error: remote object '/storage/emulated/0/bluetooth' does not exist
adb: error: remote object '/mnt/runtime/write/emulated/0/bluetooth' does not exist
adb: error: remote object '/mnt/runtime/read/emulated/0/bluetooth' does not exist
adb: error: remote object '/mnt/runtime/default/emulated/0/bluetooth' does not exist
adb: error: remote object '/data/media/0/bluetooth' does not exist
/data/misc/bluetooth/: 0 files pulled.
Bluetooth information discovered in /data/misc/bluetooth, archiving.
/data/misc/bluedroid/: 2 files pulled. 0.1 MB/s (1246 bytes in 0.018s)
Bluetooth information discovered in /data/misc/bluedroid, archiving.
/data/misc/bluedroid/bt_config.conf: 1 file pulled. 0.3 MB/s (623 bytes in 0.002s)
=== archived Bluetooth pairing information in /Volumes/export/Android/Backup/TA348000UM/Bluetooth/bluedroid/bt_config.conf-2018-04-17_1220
/data/data/org.monkey.d.ruffy.ruffy/: 1 file pulled. 0.0 MB/s (440 bytes in 0.017s)
/data/data/org.monkey.d.ruffy.ruffy/shared_prefs/pumpdata.xml: 1 file pulled. 0.1 MB/s (440 bytes in 0.006s)
=== archived ruffy pairing information in /Volumes/export/Android/Backup/TA348000UM/org.monkey.d.ruffy.ruffy/shared_prefs/pumpdata.xml-2018-04-17_1220
/data/misc/wifi/wpa_supplicant.conf: 1 file pulled. 0.7 MB/s (1312 bytes in 0.002s)
=== archived wlan configuration in /Volumes/export/Android/Backup/TA348000UM/wifi/wpa_supplicant.conf-2018-04-17_1220

@LongboatAline
Copy link
Author

LongboatAline commented Apr 17, 2018

ToDo:

  • if root fails, default to adb backup for archiving ruffy
  • test with different (non LineageOS) phones (Bluetooth paths!
  • check if ruffy is present at all before trying to archive it
  • Cleanup
    • remove logfiles older than yesterday from phone after archiving
    • remove screenshots from phone after archiving

@LongboatAline
Copy link
Author

current version, needs some tidying:

#!/bin/sh
# Trying to backup AAPS related data into a directory per device for later perusal
# Install: Configure Path variable section before use
# To use, connect (only) your phone used to run AAPS via usb, and call this script

#  Check we only have one Android Device connected, and are allowed to access it via $adb.
adb=$HOME/Library/Android/sdk/platform-tools/adb
$adb start-server || echo $0: Could not start adb server!

device=$($adb devices |awk 'NR == 2 {print $1}')
if [ -z $device ]
then
	echo $0: No device found, please make sure USB debugging is enabled and allowed for this host.
	exit 0
fi


if [ `($adb devices | wc -l)` -eq "3" ]
then
	echo $0: Device $device found
else
	echo $0: Please make sure only the device to backup is connected.
	exit 0
fi

#-----------------------------------------------------------------------------------------------
# Paths - please verify and configure where needed
#-----------------------------------------------------------------------------------------------
# Where should your backups go:
backup=/Volumes/export/Android/Backup/${device}
xDriparchive=${backup}/xDrip
AAPSarchive=${backup}/AAPS
# Path to your sdcard (where AAPS writes its preference file to)
AAPSprefpath=/storage/emulated/0
AAPSdb=/data/data/info.nightscout.androidaps/databases/AndroidAPSDb
# Tablet Medion 
# AAPSprefpath=/storage/sdcard0
sdcard=/sdcard
ruffypath=/data/data/org.monkey.d.ruffy.ruffy 
AAPSpref=AndroidAPSPreferences
AAPSlog=Android/data/info.nightscout.androidaps/files
bugdir=/data/user_de/0/com.android.shell/files/bugreports/
# These might be different on your phone (or not apply at all)
Screenshots=Pictures/Screenshots
TiBackup=TitaniumBackup
Photos=/sdcard/DCIM/Camera
Download=/storage/self/primary/Download
#Photos=/storage/emulated/0/DCIM/Camera
#Photos=/storage/emulated/0/OLYMPUS
#Photos=/storage/emulated/0/Pictures
Photos=/storage/7C04-39FD/DCIM/Camera/
#TiBackup=
# End of configurable section
#-----------------------------------------------------------------------------------------------

btservice=`$adb shell dumpsys -l |grep bluet | awk '{$1=$1};1' `

for i in ${backup} ${AAPSarchive} ${xDriparchive} ${backup}/Bluetooth ${backup}/Properties ${backup}/Bugreports ${backup}/wifi ${backup}/org.monkey.d.ruffy.ruffy/shared_prefs ${AAPSarchive}/db
do
   if [ ! -d ${i} ] 
   then
	mkdir -p ${i} || { echo  "$0: Could not create Archive directory ${i}, exiting" ; exit  1; }
	echo "$0: Created archive directory ${i}".
   fi
done

#############################################
# Date format
# with seconds:
# datefmt="+%Y-%m-%d|%T" 
#datefmt="+%Y-%m-%d|%H:%M"
datefmt="+%Y-%m-%d_%H%M"
# One file per month
filefmt="+%Y-%m"
LOGM="`date ${filefmt}`.csv"
LOGD=${LOGM}-`date "+%d"`
YY=`date "+%Y"`
#############################################

#############################################
# Field 01: UTC - normalized time
# Field 02: Localtime (or device time)
yesterday=`TZ=CET+24 /bin/date '+%b %e'`
now=`/bin/date ${datefmt}`
utc=`TZ=UTC /bin/date ${datefmt}`
#############################################

# Get data, and archive it into one directory per device
$adb shell getprop > $backup/Properties/properties-${now}
$adb pull -a ${AAPSprefpath}/${AAPSpref} ${AAPSarchive}/${AAPSpref}-$now && echo "=== archived as ${AAPSarchive}/${AAPSpref}-${now}"
$adb pull -a ${AAPSprefpath}/${AAPSlog} ${AAPSarchive}/ && echo "=== archived in ${AAPSarchive}/files"
$adb pull -a ${AAPSprefpath}/${Screenshots} ${backup}/ && echo "=== archived in ${backup}/Screenshots"
$adb pull -a ${Download} ${backup}/ && echo "=== archived in ${backup}/DCIM" && echo archived Downloads
# for i in `echo *`; do adb shell rm /storage/7C04-39FD/DCIM/Camera/$i; done
#
# TODO: delete *older* AAPS logfiles and Screenshots on device after transfer
$adb shell rm /storage/emulated/0/Pictures/Screenshots/\*.png
$adb pull -a ${Photos} ${backup}/ && echo "=== archived in ${backup}/DCIM" && echo archived Photos
#

if [ -z $TiBackup ]
then
	echo "Archiving Titanium Backup storage directory as well - warning: this might grow to be rather big"
#	$adb pull -a ${AAPSprefpath}/${TiBackup} ${backup}/ && echo "=== archived in ${backup}/TitaniumBackup"
fi

mkdir ${xDriparchive}/${now}
# Dated archive files - stacking into one directory to avoid unnecessary duplication
$adb pull -a ${sdcard}/xdrip ${backup}&& echo "Pulled ${sdcard}/xdrip"
$adb pull -a ${sdcard}/Download/xDrip ${xDriparchive}/${now} && echo "=== archived in ${xDriparchive}-${now}"
$adb pull -a ${sdcard}/Download/xDrip-export ${xDriparchive}/${now} && echo "=== archived in ${xDriparchive}-${now}"
rmdir ${xDriparchive}/${now} && echo "=== (nothing there - removing empty directory)"

$adb root
$adb root
$adb pull -a ${AAPSdb} ${AAPSarchive}/db/${now} 
$adb pull -a ${AAPSdb}-journal ${AAPSarchive}/db/${now}-journal

# takes some time
# $adb bugreport ${backup}/Bugreports/bugreport-${device}-${now}
$adb pull -a ${bugdir} ${backup}/Bugreports/ && $adb shell rm ${bugdir}/\*.zip
# falls newer bugreport, prin success

# Brute force: Try to get one of the bluetooth config files we know of
echo "Trying to fetch bluetooth information from all over the place, expect some attempts to fail"
echo "Found Bluetooth information in the following directories:" > ${backup}/Bluetooth/location.txt
for i in \
	/system/etc/bluetooth \
	/sys/class/bluetooth \
	/sys/kernel/debug/bluetooth \
	/sys/module/bluetooth \
	/storage/emulated/0/bluetooth \
	/mnt/runtime/write/emulated/0/bluetooth \
	/mnt/runtime/read/emulated/0/bluetooth \
	/mnt/runtime/default/emulated/0/bluetooth \
	/data/media/0/bluetooth \
	/data/misc/bluetooth \
	/data/misc/bluedroid 
do
	$adb pull ${i} ${backup}/Bluetooth/ && ( echo ${i} >> ${backup}/Bluetooth/location.txt; echo "Bluetooth information discovered in ${i}, archiving." )
done
$adb shell dumpsys ${btservice} > ${backup}/Bluetooth/${btservice}-${now}
if [ -d ${backup}/Bluetooth/bluedroid/ ]
then
	$adb pull /data/misc/bluedroid/bt_config.conf ${backup}/Bluetooth/bluedroid/bt_config.conf-${now} && echo "=== archived Bluetooth pairing information in ${backup}/Bluetooth/bluedroid/bt_config.conf-${now}"
fi

# /data/data/org.monkey.d.ruffy.ruffy
$adb pull ${ruffypath}  ${backup}/
$adb pull ${ruffypath}/shared_prefs/pumpdata.xml  ${backup}/org.monkey.d.ruffy.ruffy/shared_prefs/pumpdata.xml-${now} && echo "=== archived ruffy pairing information in ${backup}/org.monkey.d.ruffy.ruffy/shared_prefs/pumpdata.xml-${now}"

# Contains access configuration/passwords (unless using certificate-based access) 
$adb pull /data/misc/wifi/wpa_supplicant.conf  ${backup}/wifi/wpa_supplicant.conf-${now} && echo "=== archived wlan configuration in ${backup}/wifi/wpa_supplicant.conf-${now}"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment