-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathandroidbackup.php
430 lines (365 loc) · 17.5 KB
/
androidbackup.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
<?php
/*
Android バックアッププログラム
APKファイル(apkディレクトリ)、アプリケーションデータ(appbackupディレクトリ)、本体のデータ(dataディレクトリ)、SDカードデータ(sdcarddataディレクトリ内)をダウンロードできる
実行前に端末のUSBデバッグをオンにしてください。
端末接続されていなくても特にチェックせず動作するため、事前にadb devicesコマンドでチェックしてから!
複数端末接続非対応
*/
$playstore_use_flag = true; // アプリ名の取得にプレイストアを利用するか?
function pp($text){
$text = trim($text);
echo date("Y/m/d H:i:s") . " | " . $text . "\n";
}
/**
* Windowsでファイル名に使用できない文字列をアンダーバーに置き換える
* @param $str ファイル名
* @return 置き換えた後のファイル名
*/
function replaceFilenameillegalChar($str)
{
return str_replace(["\\", "/", "", "*", "?", "\"", "<", ">", "|", ":"], "_", $str);
}
/**
* 指定したディレクトリを削除する
*
* @param string $dir
* @return boolean
*/
function rmrf(string $dir)
{
if (!is_dir($dir))
{
return false;
}
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
if($file == "." || $file == ".."){
continue;
}
if(is_file($dir . DIRECTORY_SEPARATOR . $file)){
unlink($dir . DIRECTORY_SEPARATOR . $file);
}else if(is_dir($dir . DIRECTORY_SEPARATOR . $file)){
rmrf($dir . DIRECTORY_SEPARATOR . $file);
}
}
closedir($dh);
}
return rmdir($dir);
}
if (!file_exists(__DIR__ . DIRECTORY_SEPARATOR . "apk" . DIRECTORY_SEPARATOR)) {
mkdir(__DIR__ . DIRECTORY_SEPARATOR . "apk" . DIRECTORY_SEPARATOR);
}
if (!file_exists(__DIR__ . DIRECTORY_SEPARATOR . "appbackup" . DIRECTORY_SEPARATOR)) {
mkdir(__DIR__ . DIRECTORY_SEPARATOR . "appbackup" . DIRECTORY_SEPARATOR);
}
if (!file_exists(__DIR__ . DIRECTORY_SEPARATOR . "data" . DIRECTORY_SEPARATOR)) {
mkdir(__DIR__ . DIRECTORY_SEPARATOR . "data" . DIRECTORY_SEPARATOR);
}
if (!file_exists(__DIR__ . DIRECTORY_SEPARATOR . "sdcarddata" . DIRECTORY_SEPARATOR)) {
mkdir(__DIR__ . DIRECTORY_SEPARATOR . "sdcarddata" . DIRECTORY_SEPARATOR);
}
if (!file_exists(__DIR__ . DIRECTORY_SEPARATOR . "tmp" . DIRECTORY_SEPARATOR)) {
mkdir(__DIR__ . DIRECTORY_SEPARATOR . "tmp" . DIRECTORY_SEPARATOR);
}
if (!file_exists(__DIR__ . DIRECTORY_SEPARATOR . "appdata" . DIRECTORY_SEPARATOR)) {
mkdir(__DIR__ . DIRECTORY_SEPARATOR . "appdata" . DIRECTORY_SEPARATOR);
}
// モデル名取得
$model = shell_exec("adb shell getprop ro.product.model");
$model = trim($model);
if ($model == "") {
pp("Model Name is not found!");
exit;
}
pp("Model: " . $model);
$size = exec("adb shell wm size");
preg_match("/Physical size: ([0-9]+)x([0-9]+)/", $size, $m);
$win_x = $m[1];
$win_y = $m[2];
$x = $win_x - 300;
$y = $win_y - 100;
pp("Window Size: " . $win_x . " x " . $win_y);
exec("aapt > nul 2>&1", $_out, $return_var);
$aapt_enable = ($return_var != 1);
if($aapt_enable){
pp("aapt: Available");
}else{
pp("aapt: Unavailable");
}
// プレイストアアクセス時のエラー抑制
$context = stream_context_create(array(
"http" => array("ignore_errors" => true)
));
system("adb shell mkdir /storage/emulated/0/APK");
// 端末にインストールされているアプリのリストを取得
$adblist = shell_exec("adb shell pm list packages -f");
$adblist = str_replace("package:", "", $adblist); // 各行の最初のpackage:を消す
$adblists = explode("\n", $adblist); // 改行で分割
$backupList = array();
foreach ($adblists as $key => $adb) {
$pos = mb_strrpos($adb, "="); // 行の最後に出てくる=を探す
if ($pos === false) {
continue; // =がなかったら次へ
}
$id = mb_substr($adb, $pos + 1); // =以降がパッケージ名
$id = trim($id);
$apkFile = mb_substr($adb, 0, $pos); // =以前がapkファイルのパス
$apkFile = trim($apkFile);
pp("[" . ($key + 1) . "/" . count($adblists) . "] ---------- " . $id . " -----");
// プレイストアにアクセス
if($playstore_use_flag){
$html = file_get_contents("https://play.google.com/store/apps/details?hl=ja_JP&id=" . $id, false, $context);
preg_match('/HTTP\/1\.[0|1|x] ([0-9]{3})/', $http_response_header[0], $matches);
$status_code = $matches[1]; // ステータスコード取得
}else{
$status_code = -1;
}
unset($name);
if ($status_code == 200) { // ステータスコードが200だったら
pp("[" . ($key + 1) . "/" . count($adblists) . "] GooglePlay StatusCode: 200");
preg_match("/<h1.* itemprop=\"name\"><span >(.+)<\/span><\/h1>/", $html, $m);
if(isset($m[1])){
$name = html_entity_decode($m[1]); // アプリ名取得
$nameORId = replaceFilenameillegalChar(
html_entity_decode(
$m[1]
)
);
if (replaceFilenameillegalChar($m[1]) != $nameORId && file_exists(__DIR__ . DIRECTORY_SEPARATOR . "apk" . DIRECTORY_SEPARATOR . replaceFilenameillegalChar($m[1]))) {
rename(__DIR__ . DIRECTORY_SEPARATOR . "apk" . DIRECTORY_SEPARATOR . replaceFilenameillegalChar($m[1]), __DIR__ . DIRECTORY_SEPARATOR . "apk" . DIRECTORY_SEPARATOR . $nameORId);
pp("[" . ($key + 1) . "/" . count($adblists) . "] " . "RENAME FROM " . "apk" . DIRECTORY_SEPARATOR . replaceFilenameillegalChar($m[1]) . " TO " . "apk" . DIRECTORY_SEPARATOR . $nameORId . "!");
}
}
}
if(!isset($name) && $aapt_enable) {
// apkを引っ張ってきてaaptで調べる
pp("[" . ($key + 1) . "/" . count($adblists) . "] Try to get data from aapt...");
system("adb shell cp \"" . $apkFile . "\" /storage/emulated/0/APK");
$filename = substr($apkFile, strrpos($apkFile, "/") + 1);
$output = exec("adb pull -p \"/storage/emulated/0/APK/" . $filename . "\" -p \"" . __DIR__ . DIRECTORY_SEPARATOR . "tmp" . DIRECTORY_SEPARATOR . "tmp.apk\"");
pp($output);
if(file_exists(__DIR__ . DIRECTORY_SEPARATOR . "tmp" . DIRECTORY_SEPARATOR . "tmp.apk")){
// ok
$packageaaptData = shell_exec("aapt dump badging " . __DIR__ . DIRECTORY_SEPARATOR . "tmp" . DIRECTORY_SEPARATOR . "tmp.apk");
unset($m);
preg_match("/application-label-ja:'(.+)'/", $packageaaptData, $m);
if(isset($m[1])){
$name = html_entity_decode($m[1]); // アプリ名取得
$nameORId = replaceFilenameillegalChar(
html_entity_decode(
$m[1]
)
);
pp("[" . ($key + 1) . "/" . count($adblists) . "] Successful data acquisition from aapt! (ja)");
}else{
unset($m);
preg_match("/application-label:'(.+)'/", $packageaaptData, $m);
if(isset($m[1])){
$name = html_entity_decode($m[1]); // アプリ名取得
$nameORId = replaceFilenameillegalChar(
html_entity_decode(
$m[1]
)
);
pp("[" . ($key + 1) . "/" . count($adblists) . "] Successful data acquisition from aapt! (other lang)");
}
}
system("adb shell rm \"/storage/emulated/0/APK/" . $filename . "\"");
unlink(__DIR__ . DIRECTORY_SEPARATOR . "tmp" . DIRECTORY_SEPARATOR . "tmp.apk");
}
}
if(!isset($name) || $name == null){
pp("[" . ($key + 1) . "/" . count($adblists) . "] Failed to get application name. Use application ID.");
$name = "null"; // プレイストアもaaptからも取得できなかったらnull
$nameORId = $id;
}
// 端末からアプリのバージョンを取得する
$packageData = shell_exec("adb shell dumpsys package " . $id);
preg_match("/versionName=(.+)/", $packageData, $m);
$versionName = $m[1];
pp("[" . ($key + 1) . "/" . count($adblists) . "] Application Name: " . $name);
pp("[" . ($key + 1) . "/" . count($adblists) . "] Version: " . $versionName);
//echo __DIR__ . "/apk/" . $id . "_" . $versionName . ".apk";
$backupList[] = [
"name" => $name,
"id" => $id,
"path" => $apkFile,
"version" => $versionName
];
//break;
if (!file_exists(__DIR__ . DIRECTORY_SEPARATOR . "apk" . DIRECTORY_SEPARATOR . $nameORId)) {
mkdir(__DIR__ . DIRECTORY_SEPARATOR . "apk" . DIRECTORY_SEPARATOR . $nameORId);
}
$versionName = replaceFilenameillegalChar($versionName);
if (file_exists(__DIR__ . DIRECTORY_SEPARATOR . "apk" . DIRECTORY_SEPARATOR . $nameORId . DIRECTORY_SEPARATOR . $versionName . ".apk")) {
pp("[" . ($key + 1) . "/" . count($adblists) . "] APK File is found!");
continue;
}
// APKファイルダウンロード
pp("[" . ($key + 1) . "/" . count($adblists) . "] APK File Downloading...");
system("adb shell cp \"" . $apkFile . "\" /storage/emulated/0/APK");
$filename = substr($apkFile, strrpos($apkFile, "/") + 1);
$output = exec("adb pull -p \"/storage/emulated/0/APK/" . $filename . "\" -p \"" . __DIR__ . DIRECTORY_SEPARATOR . "apk" . DIRECTORY_SEPARATOR . $nameORId . DIRECTORY_SEPARATOR . $versionName . ".apk\"");
pp($output);
if(file_exists(__DIR__ . DIRECTORY_SEPARATOR . "apk" . DIRECTORY_SEPARATOR . $nameORId . DIRECTORY_SEPARATOR . $versionName . ".apk")){
pp("[" . ($key + 1) . "/" . count($adblists) . "] APK File Download Successful! (ApplicationName)");
}else{
// download error
if (!file_exists(__DIR__ . DIRECTORY_SEPARATOR . "apk" . DIRECTORY_SEPARATOR . $id)) {
mkdir(__DIR__ . DIRECTORY_SEPARATOR . "apk" . DIRECTORY_SEPARATOR . $id);
}
$output = exec("adb pull -p \"/storage/emulated/0/APK/" . $filename . "\" -p \"" . __DIR__ . DIRECTORY_SEPARATOR . "apk" . DIRECTORY_SEPARATOR . $id . DIRECTORY_SEPARATOR . $versionName . ".apk\"");
pp($output);
if(file_exists(__DIR__ . DIRECTORY_SEPARATOR . "apk" . DIRECTORY_SEPARATOR . $id . DIRECTORY_SEPARATOR . $versionName . ".apk")){
pp("[" . ($key + 1) . "/" . count($adblists) . "] APK File Download Successful! (ApplicationID)");
}
}
system("adb shell rm \"/storage/emulated/0/APK/" . $filename . "\"");
if (($key + 1) % 20 == 0) {
// 20アプリずつ画面をタッチして起動させておく
$fp = popen('start /b "" php ' . __DIR__ . '/touch.php 0 0', 'r');
pclose($fp);
}
}
pp(count($backupList) . " apps backupped!");
system("adb shell rm -rf /storage/emulated/0/APK");
$DATE = date("Ymd");
foreach ($backupList as $key => $one) {
$name = $one["name"];
$id = $one["id"];
$path = $one["path"];
$versionName = $one["version"];
if ($name != null) {
$nameORId = $name;
} else {
$nameORId = $id;
}
pp("[" . ($key + 1) . "/" . count($backupList) . "] " . $name . " " . $id . " " . $versionName);
if (!file_exists(__DIR__ . DIRECTORY_SEPARATOR . "appbackup" . DIRECTORY_SEPARATOR . $model)) {
mkdir(__DIR__ . DIRECTORY_SEPARATOR . "appbackup" . DIRECTORY_SEPARATOR . $model);
}
if (!file_exists(__DIR__ . DIRECTORY_SEPARATOR . "appbackup" . DIRECTORY_SEPARATOR . $model . DIRECTORY_SEPARATOR . $DATE)) {
mkdir(__DIR__ . DIRECTORY_SEPARATOR . "appbackup" . DIRECTORY_SEPARATOR . $model . DIRECTORY_SEPARATOR . $DATE);
}
$file = __DIR__ . DIRECTORY_SEPARATOR . "appbackup" . DIRECTORY_SEPARATOR . $model . DIRECTORY_SEPARATOR . $DATE . DIRECTORY_SEPARATOR . $nameORId . ".ab";
if (file_exists($file)) {
pp("App backup File is found!");
continue;
}
// アプリバックアップ
// 端末でバックアップの確認がされるので端末操作必要
//$fp = popen('start /b "" php ' . __DIR__ . '/touch.php ' . $x . ' ' . $y, 'r');
$fp = popen('start /b "" php ' . __DIR__ . '/touch.php', 'r');
pclose($fp);
exec("adb shell input keyevent 3");
pp(exec("adb backup -f \"" . $file . "\" -apk -obb " . $id));
}
if (!file_exists(__DIR__ . DIRECTORY_SEPARATOR . "appdata" . DIRECTORY_SEPARATOR . $model)) {
mkdir(__DIR__ . DIRECTORY_SEPARATOR . "appdata" . DIRECTORY_SEPARATOR . $model);
}
if(file_exists(__DIR__ . DIRECTORY_SEPARATOR . "appbackup" . DIRECTORY_SEPARATOR . $model . DIRECTORY_SEPARATOR . $DATE)){
system("7z a " . __DIR__ . DIRECTORY_SEPARATOR . "appbackup" . DIRECTORY_SEPARATOR . $model . DIRECTORY_SEPARATOR . $DATE . ".7z " . __DIR__ . DIRECTORY_SEPARATOR . "appbackup" . DIRECTORY_SEPARATOR . $model . DIRECTORY_SEPARATOR . $DATE, $ret);
}
if($ret == 0){
pp("ReturnCode: " . $ret . ". Delete dir.");
rmrf(__DIR__ . DIRECTORY_SEPARATOR . "appbackup" . DIRECTORY_SEPARATOR . $model . DIRECTORY_SEPARATOR . $DATE);
}else{
pp("ReturnCode: " . $ret . ".");
}
if(file_exists(__DIR__ . DIRECTORY_SEPARATOR . "appdata" . DIRECTORY_SEPARATOR . $model . DIRECTORY_SEPARATOR . $DATE)){
system("7z a " . __DIR__ . DIRECTORY_SEPARATOR . "appdata" . DIRECTORY_SEPARATOR . $model . DIRECTORY_SEPARATOR . $DATE . ".7z " . __DIR__ . DIRECTORY_SEPARATOR . "appdata" . DIRECTORY_SEPARATOR . $model . DIRECTORY_SEPARATOR . $DATE, $ret);
}
if($ret == 0){
pp("ReturnCode: " . $ret . ". Delete dir.");
rmrf(__DIR__ . DIRECTORY_SEPARATOR . "appdata" . DIRECTORY_SEPARATOR . $model . DIRECTORY_SEPARATOR . $DATE);
}else{
pp("ReturnCode: " . $ret . ".");
}
if (!file_exists(__DIR__ . DIRECTORY_SEPARATOR . "data" . DIRECTORY_SEPARATOR . $model)) {
mkdir(__DIR__ . DIRECTORY_SEPARATOR . "data" . DIRECTORY_SEPARATOR . $model);
}
if (!file_exists(__DIR__ . DIRECTORY_SEPARATOR . "data" . DIRECTORY_SEPARATOR . $model . DIRECTORY_SEPARATOR . $DATE)) {
mkdir(__DIR__ . DIRECTORY_SEPARATOR . "data" . DIRECTORY_SEPARATOR . $model . DIRECTORY_SEPARATOR . $DATE);
}
// 本体のデータをダウンロード
pp("start data backup...");
$nowcd = getcwd();
chdir(__DIR__ . DIRECTORY_SEPARATOR . "data" . DIRECTORY_SEPARATOR . $model . DIRECTORY_SEPARATOR . $DATE);
$files = shell_exec("adb shell find /sdcard/ -type file");
$files = explode("\n", $files);
$homedir = getcwd();
foreach ($files as $file) {
$file = trim($file);
$dirname = dirname(substr($file, strlen("/sdcard/")));
if(!file_exists($dirname)){
mkdir($dirname, 0777, true);
}
chdir($dirname);
system("adb pull \"" . $file . "\"");
chdir($homedir);
}
chdir($nowcd);
// system("adb pull /sdcard/ \"" . __DIR__ . DIRECTORY_SEPARATOR . "data" . DIRECTORY_SEPARATOR . $model . DIRECTORY_SEPARATOR . $DATE . "\"");
if(file_exists(__DIR__ . DIRECTORY_SEPARATOR . "data" . DIRECTORY_SEPARATOR . $model . DIRECTORY_SEPARATOR . $DATE)){
system("7z a " . __DIR__ . DIRECTORY_SEPARATOR . "data" . DIRECTORY_SEPARATOR . $model . DIRECTORY_SEPARATOR . $DATE . ".7z " . __DIR__ . DIRECTORY_SEPARATOR . "data" . DIRECTORY_SEPARATOR . $model . DIRECTORY_SEPARATOR . $DATE, $ret);
}
if($ret == 0){
pp("ReturnCode: " . $ret . ". Delete dir.");
rmrf(__DIR__ . DIRECTORY_SEPARATOR . "data" . DIRECTORY_SEPARATOR . $model . DIRECTORY_SEPARATOR . $DATE);
}else{
pp("ReturnCode: " . $ret . ".");
}
pp("end data backup.");
// SDカードがある場合それをバックアップ
pp("start SDCard data backup...");
exec("adb shell cat /proc/mounts", $output);
$sddrives = [];
foreach($output as $one){
$one = explode(" ", $one);
if(substr($one[1], 0, strlen("/storage/")) == "/storage/"){
if($one[1] == "/storage/emulated"){
continue;
}
$sddrives[] = $one[1];
}
}
if(count($sddrives) == 0){
pp("SDCard is not found.");
exit;
}
if (!file_exists(__DIR__ . DIRECTORY_SEPARATOR . "sdcarddata" . DIRECTORY_SEPARATOR . $model)) {
mkdir(__DIR__ . DIRECTORY_SEPARATOR . "sdcarddata" . DIRECTORY_SEPARATOR . $model);
}
if (!file_exists(__DIR__ . DIRECTORY_SEPARATOR . "sdcarddata" . DIRECTORY_SEPARATOR . $model . DIRECTORY_SEPARATOR . $DATE)) {
mkdir(__DIR__ . DIRECTORY_SEPARATOR . "sdcarddata" . DIRECTORY_SEPARATOR . $model . DIRECTORY_SEPARATOR . $DATE);
}
$nowcd = getcwd();
chdir(__DIR__ . DIRECTORY_SEPARATOR . "sdcarddata" . DIRECTORY_SEPARATOR . $model . DIRECTORY_SEPARATOR . $DATE);
foreach($sddrives as $sddrive){
$sdName = substr($sddrive, strrpos($sddrive, "/") + 1);
$files = shell_exec("adb shell find " . $sddrive . "/ -type file");
$files = explode("\n", $files);
$homedir = getcwd();
foreach ($files as $file) {
$file = trim($file);
$dirname = dirname(substr($file, strlen($sddrive . "/")));
if(!file_exists($dirname)){
mkdir($dirname, 0777, true);
}
chdir($dirname);
system("adb pull \"" . $file . "\"");
chdir($homedir);
}
}
chdir($nowcd);
if(file_exists(__DIR__ . DIRECTORY_SEPARATOR . "sdcarddata" . DIRECTORY_SEPARATOR . $model . DIRECTORY_SEPARATOR . $DATE)){
system("7z a " . __DIR__ . DIRECTORY_SEPARATOR . "sdcarddata" . DIRECTORY_SEPARATOR . $model . DIRECTORY_SEPARATOR . $DATE . ".7z " . __DIR__ . DIRECTORY_SEPARATOR . "sdcarddata" . DIRECTORY_SEPARATOR . $model . DIRECTORY_SEPARATOR . $DATE);
}
if($ret == 0){
pp("ReturnCode: " . $ret . ". Delete dir.");
rmrf(__DIR__ . DIRECTORY_SEPARATOR . "sdcarddata" . DIRECTORY_SEPARATOR . $model . DIRECTORY_SEPARATOR . $DATE);
}else{
pp("ReturnCode: " . $ret . ".");
}
pp("end SDCard data backup.");