我是靠谱客的博主 糟糕短靴,这篇文章主要介绍手把手带你使用Node.js和adb开发一个手机备份小工具,现在分享给大家,希望可以做个参考。

本篇文章给大家分享一个Node实战,介绍一下使用Node.js和adb怎么开发一个手机备份小工具,希望对大家有所帮助!

随着科技的发展我们日常中拍摄的图片和视频清晰度不断提升,但这也有一个较大的缺点那就是他们的体积也越来越大。还记得以前刚开始使用智能手机的时候那会一张照片只不过才2-5MB,而现在一张照片已经达到了15-20MB,甚至更大。

1.png

而我们手机上的存储空间是有限的,我们怎么把这些照片和视频备份起来,好让手机腾出空间来呢?

于是,在刚开始我是将这些数据都存放在了某相册云端上,虽然解决了存放这些数据的问题,但是也冒出了新的问题,例如上传大小约束、需要一直占后台导致耗电增加、广告。

后面我干脆不使用了,自己撸了一个脚本用于备份这些数据,于是就有了这一篇文章。

我使用了Node.jsadb制作了这一个脚本,并命名为MIB

原理

这个小工具是利用手机上的adb调试,通过shell命令读取手机中的文件信息和复制,移动手机中的文件实现的。

执行流程

我画了一个简易流程图,MIB首先会从读取配置文件(没有则创建配文件),根据配置文件读取需要备份的节点路径并进行文件备份操作。直到节点结束。

2.png

开发过程

安装所需环境

  • 下载adb包,用于执行各种设备操作

  • 下载Node.js,这个我相信兄弟们的电脑上都已经有了

  • 安装依赖库

    • fs-extra:基于fs模块二次封装的Node
    • prompts:命令行上交互的Node
    • winston:用于记录脚本日志的Node

读取配置文件

复制代码
1
2
3
4
5
6
7
export const getConfig = (): ConfigType => { if (existConf()) { return readJsonSync(CONFIG_PATH); } // 找不到配置文件 return createDefaultConfig(); };
登录后复制

在执行脚本时,选择需要备份的设备ID。并指定执行adb命令时的设备

复制代码
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
(async () => { const device: string | boolean = await selectDevice(); if (device) MIB(); })(); export const selectDevice = async ():Promise<string|false> => { // 获取设备 const list: devicesType[] = devices(); if (list.length === 0) { log("当前无设备连接,请连接后再执行该工具", "warn"); return false; } const result = list.map((i) => ({ title: i.name, value: i.name })); const { value } = await prompts({ type: "select", name: "value", message: "please select your device", choices: result, }); currentDeviceName = value; return currentDeviceName; };
登录后复制

遍历备份节点

选择设备之后,进入遍历节点信息,并执行拷贝文件到指定路径(配置文件中的output属性)

复制代码
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
const MIB = () => { // 获取配置文件 const { backups, output } = getConfig(); // 判断备份节点是否为空 if (backups.length === 0) { log("当前备份节点为空", "warn"); log("请在配置文件中添加备份节点", "warn"); } if (backups.length > 0) { isPath(output); // 解析备份路径最后一个文件夹 backups.forEach((item: SaveItemType) => { log(`当前执行备份任务:${item.comment}`); const arr = item.path.split("/").filter((i: string) => i !== ""); const folderName = arr.at(-1); const backupDir = pathRepair(item.path); // 备份目录 // 判断节点内是否有备份目录 // 拼接导出路径 const rootPath = pathRepair(pathRepair(output) + folderName); const outputDir = item.output ? item.output && pathRepair(item.output) : rootPath; // 判断备份路径是否存在 if (!isPathAdb(backupDir)) { log(`备份路径:${backupDir} 不存在已跳过`, "error"); } else { // 判断导出路径 isPath(outputDir); backup(backupDir, outputDir, item.full); } }); } log("程序结束"); }; // 细化需要备份的文件,进入备份队列中 const backup = (target: string, output: string, full: boolean = false) => { if (!full) { // 备份非备份的文件数据 // 获取手机中的文件信息,对比本地 const { backupQueue } = initData(target, output); // 计算体积和数量 computeBackupSize(backupQueue); // 执行备份程序 move(backupQueue, output); } else { // 不文件对比,直接备份 moveFolder(target, output); } }; // 移动待备份文件队列中的文件 const move = (backupQueue: FileNodeType[], outputDir: string): void => { if (backupQueue.length === 0) { log("无需备份"); return; } for (const fileN of backupQueue) { log(`正在备份${fileN.fileName}`); try { const out: string = execAdb( `pull "${fileN.filePath}" "${outputDir + fileN.fileName}"`, ); const speed: string | null = out.match(speedReg) !== null ? out.match(speedReg)![0] : "读取速度失败"; log(`平均传输速度${speed}`); } catch (e: any) { log(`备份${fileN.fileName}失败 error:${e.message}`, "error"); } } };
登录后复制

脚本功能

  • USB连接备份数据
  • 无线连接备份数据
  • 多设备备份选择
  • 单节点全量备份

使用

在终端中输入以下命令进行全局安装mib

复制代码
1
npm i @qc2168/mib -g
登录后复制

配置脚本文件

首次使用需要在用户目录下新建.mibrc文件,并设置对应的参数内容。

复制代码
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
{ "backups": [ { "path": "/sdcard/MIUI/sound_recorder/call_rec", "comment": "通话录音" }, { "path": "/sdcard/DCIM/Camera", "comment": "本地相册" }, { "path": "/sdcard/DCIM/Creative", "comment": "我的创作" }, { "path": "/sdcard/Pictures/weixin", "comment": "微信相册" }, { "path": "/sdcard/tencent/qq_images", "comment": "QQ相册" }, { "path": "/sdcard/Pictures/知乎", "comment": "知乎" }, { "path": "/sdcard/tieba", "comment": "贴吧" }, { "path": "/sdcard/DCIM/Screenshots", "comment": "屏幕截屏" }, { "path": "/sdcard/DCIM/screenrecorder", "comment": "屏幕录制" }, { "path": "/sdcard/MIUI/sound_recorder", "comment": "录音" }, { "path": "/sdcard/MIUI/sound_recorder/app_rec", "comment": "应用录音" } ], "output": "E:/backups/MI10PRO" }
登录后复制

执行备份

在控制台中,直接输入mib即可触发脚本,无需其他参数。

复制代码
1
mib
登录后复制

控制台会根据配置文件并输出对应的信息。

复制代码
1
2
3
4
5
6
7
8
9
10
11
2022-04-09 20:58:11 info 当前执行备份任务:屏幕录制 2022-04-09 20:58:11 info 备份数量1 2022-04-09 20:58:11 info 已获取数据24Mb 2022-04-09 20:58:11 info 备份体积24Mb 2022-04-09 20:58:11 info 正在备份Screenrecorder-2022-04-08-19-45-51-836.mp4 2022-04-09 20:58:12 info 平均传输速度27.7 MB/s 2022-04-09 20:58:12 info 当前执行备份任务:录音 2022-04-09 20:58:12 info 备份数量0 2022-04-09 20:58:12 info 备份体积0Mb 2022-04-09 20:58:12 info 无需备份 2022-04-09 20:58:13 info 程序结束
登录后复制

最后

以上就是糟糕短靴最近收集整理的关于手把手带你使用Node.js和adb开发一个手机备份小工具的全部内容,更多相关手把手带你使用Node内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(98)

评论列表共有 0 条评论

立即
投稿
返回
顶部