├── README.md
├── adb-export.sh
└── LICENSE
/README.md:
--------------------------------------------------------------------------------
1 | # adb-export
2 | Bash script that **exports** content provider's data to raw and CSV format. It can be your own content provider or any other content provider that isn't blocked with permissions.
3 |
4 | ## Getting started
5 |
6 | First, clone the repository using git:
7 |
8 | ``` bash
9 | git clone https://github.com/snatik/adb-export/
10 | ```
11 |
12 | or download the script manually using this command:
13 |
14 | ``` bash
15 | curl "https://raw.githubusercontent.com/snatik/adb-export/master/adb-export.sh" -o adb-export.sh
16 | ```
17 |
18 | Then give the execution permission to the script and run it:
19 |
20 | ``` bash
21 | $ chmod +x adb-export.sh
22 | $ ./adb-export.sh
23 | ```
24 |
25 | ## Usage
26 |
27 | Enable `Developer options` on the device
28 |
29 | * **-e [uri]** - Set your provider URI and Export the data to CSV
30 |
31 | Example:
32 | ``` bash
33 | ./adb-export.sh -e content://com.your.app/images
34 | ```
35 |
36 | Example:
37 | ``` bash
38 | ./adb-export.sh -e content://com.android.calendar/calendars
39 | ```
40 |
41 | ## Output
42 | The exported data will be in a new created folders with timestamps in the same root folder this script is located. You will find two new created files:
43 |
44 | * `raw_query.txt` - this is what we exported from device
45 | * `data.csv` - this is what you need
46 |
47 | ## Permissions :unlock:
48 |
49 | There is a list of some android content providers that don't block the adb query. My expectation was that it wouldn't be possible because of lack of provider permissions. Just to be on the safe side, I opened a security vulnarability issue to **[Security Google Team](http://www.google.co.il/about/appsecurity/)** and got few replies where the last one says that this is not a security hole.
50 |
51 | Just an example:
52 |
53 | * This one won't work: `content://sms`
54 | The result will be: *requires android.permission.READ_SMS*
55 |
56 | * This will work: `content://com.android.contacts/contacts`
57 | The result: CSV with all contacts
58 | Which, btw, should also check for permissions, but it **doesn't**. :open_mouth:
59 |
60 | ## Android content providers URIs :unlock:
61 | If you discover some new that work, add them here
62 |
63 | #### Calendar
64 |
65 | - content://com.android.calendar/calendars
66 | - content://com.android.calendar/events
67 | - content://com.android.calendar/attendees
68 | - content://com.android.calendar/reminders
69 | - content://com.android.calendar/extendedproperties
70 |
71 | #### Contacts
72 |
73 | - content://com.android.contacts/contacts
74 | - content://com.android.contacts/directories
75 | - content://com.android.contacts/deleted_contacts
76 | - content://com.android.contacts/raw_contacts
77 | - content://com.android.contacts/data
78 | - content://com.android.contacts/data/phones
79 | - content://com.android.contacts/raw_contact_entities
80 | - content://com.android.contacts/status_updates
81 | - content://com.android.contacts/groups
82 | - content://com.android.contacts/aggregation_exceptions
83 | - content://com.android.contacts/photo_dimensions
84 |
85 | #### Media
86 |
87 | - content://media/external/images/media
88 | - content://media/external/images/thumbnails
89 | - content://media/external/audio/media
90 | - content://media/external/audio/genres
91 | - content://media/external/audio/playlists
92 | - content://media/external/audio/artists
93 | - content://media/external/audio/albums
94 | - content://media/external/video/media
95 | - content://media/external/video/thumbnails
96 |
97 | > the `external` in the uri, you can also change to `internal`
98 |
99 | #### Settings
100 |
101 | - content://settings/system
102 | - content://settings/secure
103 | - content://settings/global
104 | - content://settings/bookmarks
105 |
106 | #### Other
107 |
108 | - content://user_dictionary/words
109 |
110 | ## Tested Environments
111 |
112 | * MacOSX
113 | * Ubuntu 14.10 (Utopic). Run ```sudo apt-get install android-tools-adb``` first
114 |
115 | If you have successfully tested this script on others systems or platforms please let me know or update this list.
116 |
117 | ## Important things to know
118 |
119 | - Any commas in imported values are replaced to " " (space) char, since they don't play well in CSV format. You can configure it by changing `REPLACE_VALUE_COMMAS_TO` variable in the script.
120 | - The table schema of the same android content provider on different devices can be different, thus the parsing steps try to understand what is the column and what is the value.
121 | - This is the first version, and if you find bad parsing outputs, please open an issue.
122 | - Use it only for good purposes!
123 |
124 | ## License
125 | Apache License 2.0
126 |
127 | See [LICENSE](./LICENSE.md) for details.
128 |
129 | ## Follow us
130 |
131 | [](https://twitter.com/intent/tweet?text=https://github.com/snatik/adb-export)
132 | [](https://twitter.com/snatikteam)
133 |
--------------------------------------------------------------------------------
/adb-export.sh:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 | # author: Roman Kushnarenko @sromku
3 | # license: Apache License 2.0
4 |
5 | DEBUG=false
6 | REPLACE_VALUE_COMMAS_TO=" "
7 | SELECTED_URI=""
8 |
9 | # usage info
10 | usage() {
11 | cat <)
112 | if [ $i -eq 0 -o $i -eq 1 ]; then
113 | let "i+=1"
114 | continue
115 | fi
116 |
117 | # if last char is "," then we have valid key=value
118 | keyValue=$(printf "%s" "${keyValue} ${item}")
119 | lastChar="${item: -1}"
120 | if [ $lastChar = "," ]; then
121 | keyValue=$(tr "," "$REPLACE_VALUE_COMMAS_TO" <<< "${keyValue%?}")
122 | keyValue+=","
123 | next=true
124 | fi
125 |
126 | # if we have valid key value, then export to CSV
127 | if [ $next = true ]; then
128 |
129 | # fetch the column & value
130 | position=$(strindex "$keyValue" "=")
131 | # this is another assumtion, that column name can't be more than 30 lenght
132 | if [ $position -eq -1 -o $position -gt 30 ]; then
133 | # yes, it may happen and we will need to take this one and update the previous value
134 | lastPosition=${#values[@]}-1
135 | # add to previous one
136 | values[$lastPosition]=$(printf "%s" "${values[lastPosition]%?}${keyValue}")
137 | next=false
138 | keyValue=""
139 | continue
140 | else
141 | let "i+=1"
142 | fi
143 |
144 | column=${keyValue:0:position}
145 | value=${keyValue:position+1}
146 |
147 | # -- DO IT ONLY FOR CSV --
148 | # except last char, replace all commas to make the CSV to be valid
149 | value="${value%?}"
150 | value=$(tr "," "$REPLACE_VALUE_COMMAS_TO" <<< "$value")
151 | value="${value},"
152 | # --^-----------------^--
153 |
154 | # aggregate columns and values
155 | columns+=($column)
156 | values+=($value)
157 |
158 | # go for next field
159 | next=false
160 | keyValue=""
161 | fi
162 |
163 | done
164 |
165 | # add last field
166 | position=$(strindex "$keyValue" "=")
167 | column=${keyValue:0:position}
168 | value=${keyValue:(position+1)}
169 | columns+=($column)
170 | values+=($value)
171 |
172 | # check if we need to print columns
173 | if [ $2 == true ]; then
174 |
175 | # print columns with comma separated
176 | cols=${columns[*]}
177 |
178 | # print to file
179 | echo ${cols// /,} >> $OUTPUT_CSV
180 | fi
181 |
182 | # print values with comma separated
183 | vals=${values[*]}
184 |
185 | # print to file end escape new lines
186 | echo ${vals} | tr "\n\r" " " >> $OUTPUT_CSV
187 | echo -n $'\n' >> $OUTPUT_CSV
188 |
189 | }
190 |
191 | # give the percentage number
192 | # params:
193 | # - part number
194 | # - total number
195 | # return:
196 | # - the percentage
197 | percent() {
198 | echo $(printf '%i %i' $1 $2 | awk '{ pc=100*$1/$2; i=int(pc); print (pc-i<0.5)?i:i+1 }')
199 | }
200 |
201 | # ========== RUN ADB CONENT CMD ===========
202 |
203 | dbquery() {
204 | adb shell "
205 | content query --uri $SELECTED_URI
206 | " > $RAW_QUERY_FILE
207 | }
208 |
209 | # wait and show some spinner
210 | (dbquery) &
211 | spinner "Querying DB"
212 |
213 | # ============ EXPORT TO CSV ==============
214 |
215 | # prepare all rows in this array
216 | declare -a rows
217 |
218 | # number of lines
219 |
220 | numOfLines=$(wc -l < "$RAW_QUERY_FILE")
221 | readLines=0
222 |
223 | # let's read line be line and build rows
224 | count=-1
225 | while read -r line
226 | do
227 | # print
228 | let "readLines+=1"
229 | percentage=$(percent "$readLines" "$numOfLines")
230 | toPrint=$(printf " - Reading DB raw data: %d" "$percentage")
231 | echo -ne "$toPrint% \r"
232 |
233 | # it works as follow:
234 | # - we check if row starts with 'Row:', then we can assume that this is a new row (it's pretty NOT bad assumtion)
235 | # - the line that doesn't start with 'Row:' is something that contains fields and data whuch are belong to previous row
236 | # ref: https://android.googlesource.com/platform/frameworks/base/+/cd92588/cmds/content/src/com/android/commands/content/Content.java
237 |
238 | # the position of 'Row:' in the line
239 | index=$(strindex "$line" "Row:")
240 | if [ $index -eq 0 ]; then
241 | # this is a new row
242 | let "count+=1"
243 | rows[$count]=$(printf "%s" "$line")
244 | else
245 | if [ $count -eq -1 ]; then
246 | # it means that we got some exception
247 | echo $line
248 | continue
249 | fi
250 | # this is still the previous row
251 | rows[$count]+=$(printf "%s" "$line")
252 | fi
253 |
254 | done < "$RAW_QUERY_FILE"
255 |
256 | if [ $count -eq -1 ]; then
257 | echo ""
258 | exit 1
259 | fi
260 |
261 | # parse and write to csv
262 | numOfRows=${#rows[@]}
263 | readLines=0
264 | echo ""
265 | for i in "${!rows[@]}"
266 | do
267 |
268 | # print
269 | let "readLines+=1"
270 | percentage=$(percent "$readLines" "$numOfRows")
271 | toPrint=$(printf " - Parsing and writing to CSV ($readLines/$numOfRows): %d" "$percentage")
272 | echo -ne "$toPrint% \r"
273 |
274 | # for each row export to CSV
275 | toCsv "${rows[i]}" $(if [ $i -eq 0 ]; then echo "true"; else echo "false"; fi)
276 |
277 | done
278 | echo ""
279 | ENDTIME=$(date +"%s")
280 | executionTime=$(($ENDTIME-$STARTTIME))
281 | minutesPassed=$(($executionTime / 60))
282 | secondsPassed=$(($executionTime % 60))
283 | # export results
284 | echo "----------------------"
285 | echo "Result:"
286 | echo $(printf "Num of exported rows: %d" "$numOfRows")
287 | echo $(printf "Execution time: %s minutes and %s seconds" "$minutesPassed" "$secondsPassed")
288 | echo $(printf "Output folder with CSV: %s" "$DIR")
289 | echo ""
290 |
291 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "{}"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright 2015 Roman Kushnarenko
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
202 |
203 |
--------------------------------------------------------------------------------