This article will share with you aNodepractical experience and introduce how to develop a mobile phone backup gadget using Node.js and adb. I hope it will be helpful to everyone!
With the development of technology, the definition of the pictures and videos we take in our daily life continues to improve, but this also has a major disadvantage, that is, their size is also getting larger and larger. big. I still remember that when I first started using smartphones, a photo was only2-5MB
, but now a photo has reached15-20MB
, or even larger.
#The storage space on our mobile phones is limited. How do we back up these photos and videos to free up space on our mobile phones?
So, at the beginning, I stored all these data in a photo album cloud. Although the problem of storing these data was solved, new problems also emerged, such as upload size constraints and the need to Occupying the background leads to increased power consumption and advertising.
I simply stopped using it later and wrote a script myself to back up the data, so I came up with this article.
I usedNode.js
andadb
to make this script and named itMIB
This gadget is debugged usingadb
on the mobile phone and reads the file information in the mobile phone through theshell
command And copy, move the files in the mobile phone.
I drew a simple flow chart,MIB
will first read the configuration file (if not, create the configuration file), Read the node path that needs to be backed up according to the configuration file and perform file backup operations. until the end of the node.
Installing the required environment
Downloadadb
package, used to perform various device operations
DownloadNode.js
, I believe this Brothers already have
installed dependency libraries on their computers
fs-extra
: based onfs
Module secondary encapsulationNode
libraryprompts
:Node
library for interaction on the command linewinston
:Node
library for recording script logsSince the project source code is a bit too much, I only put the main ones here Code part
Interested friends can go to
github
to see the project source codegithub.com/QC2168/mib
Read configuration file
export const getConfig = (): ConfigType => { if (existConf()) { return readJsonSync(CONFIG_PATH); } // 找不到配置文件 return createDefaultConfig(); };
When executing the script, select the deviceID
that needs to be backed up. And specify the device when executing theadb
command
(async () => { const device: string | boolean = await selectDevice(); if (device) MIB(); })(); export const selectDevice = async ():Promise=> { // 获取设备 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; };
Traverse the backup node
After selecting the device, enter the traversal node information , and execute the copy file to the specified path (output
attribute in the configuration file)
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
Connection backup dataEnter the following command in the terminal to install globallymib
.
npm i @qc2168/mib -g
Configuration script file
For first time use, you need to create a new.mibrc
file in the user directory and set the corresponding parameter content.
{ "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" }
Perform backup
In the console, directly entermib
to trigger the script without other parameters.
mib
The console will output corresponding information based on the configuration file.
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 程序结束
Original address: https://juejin.cn/post/7084889987631710221
Author: _island
For more node-related knowledge, please visit :nodejs tutorial!
The above is the detailed content of Take you step by step to develop a mobile phone backup gadget using Node.js and adb. For more information, please follow other related articles on the PHP Chinese website!