Make writing a habit together! This is the 8th day of my participation in the “Gold Digging Day New Plan · April More Text Challenge”. Click here for more details.
preface
With the development of technology, the sharpness of the pictures and videos we take in daily life is increasing, but there is also a big disadvantage that they are getting bigger and bigger. I remember when I first started using smart phones, a photo was only 2-5MB, but now a photo has reached 15-20MB or even bigger.
And with limited storage space on our phones, how are we going to back up all those photos and videos to make room for our phones?
Therefore, AT the beginning, I stored all these data on the cloud of a photo album. Although the problem of storing these data was solved, new problems emerged, such as uploading size constraint, power consumption increase caused by the need to occupy the background all the time, and advertising.
I stopped using it altogether and created a script to back up the data, hence this article.
I made this script using Node.js and ADB and named it MIB
The principle of
This small tool is to use adb debugging on mobile phones, through shell commands to read the file information in mobile phones and copy files in mobile phones.
Execute the process
I drew a simple flow chart. MIB first reads configuration files (if no configuration files are created), reads node paths to be backed up according to the configuration files, and performs file backup operations. Until the node ends.
The development process
Installation Environment
- download
adb
Package for performing various device operations - download
Node.js
I’m sure everyone has it on their computers - Installing dependent libraries
fs-extra
Based on:fs
Module secondary encapsulationNode
libraryprompts
: interactive on the command lineNode
librarywinston
: Used to record script logsNode
library
Because the project source code is a bit too much, I only put the main code here
Interested partners can go to Github to see the project source github.com/QC2168/mib
Reading configuration Files
export const getConfig = (): ConfigType= > {
if (existConf()) {
return readJsonSync(CONFIG_PATH);
}
// The configuration file cannot be found
return createDefaultConfig();
};
Copy the code
When executing the script, select the ID of the device that you want to back up. And specify the device on which the ADB command is executed
(async() = > {const device: string | boolean = await selectDevice();
if(device) MIB(); }) ();export const selectDevice = async() :Promise<string|false> = > {// Get the device
const list: devicesType[] = devices();
if (list.length === 0) {
log("No device is currently connected, please connect and then run the tool."."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;
};
Copy the code
Traverse the backup node
After selecting the device, go through the node information and copy the file to the specified path (output property in the configuration file)
const MIB = () = > {
// Get the configuration file
const { backups, output } = getConfig();
// Check whether the backup node is empty
if (backups.length === 0) {
log("Current backup node is empty"."warn");
log("Please add a backup node to the configuration file"."warn");
}
if (backups.length > 0) {
isPath(output);
// Parse the last folder in the backup path
backups.forEach((item: SaveItemType) = > {
log('Currently performing a backup job:${item.comment}`);
const arr = item.path.split("/").filter((i: string) = >i ! = ="");
const folderName = arr.at(-1);
const backupDir = pathRepair(item.path);
// Backup directory
// Check whether there is a backup directory on the node. // Join the export path
const rootPath = pathRepair(pathRepair(output) + folderName);
const outputDir = item.output
? item.output && pathRepair(item.output)
: rootPath;
// Check whether the backup path exists
if(! isPathAdb(backupDir)) { log('Backup path:${backupDir}There is no skipped '."error");
} else {
// Determine the export pathisPath(outputDir); backup(backupDir, outputDir, item.full); }}); } log("End of program");
};
// Specify the file to be backed up and enter the backup queue
const backup = (target: string, output: string, full: boolean = false) = > {
if(! full) {// Back up non-backup file data
// Get the file information from the mobile phone and compare it locally
const { backupQueue } = initData(target, output);
// Calculate the volume and quantity
computeBackupSize(backupQueue);
// Execute the backup program
move(backupQueue, output);
} else {
// No file comparison, direct backupmoveFolder(target, output); }};// Move files in the file queue to be backed up
const move = (backupQueue: FileNodeType[], outputDir: string) :void= > {
if (backupQueue.length === 0) {
log("No backup required");
return;
}
for (const fileN of backupQueue) {
log('Backing up${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] : "Read speed failed";
log('Average transmission speed${speed}`);
} catch (e: any) {
log(` backup${fileN.fileName}Failure error:${e.message}`."error"); }}};Copy the code
The script function
-
USB
Connecting backup Data - Wireless connection backup data
- Multiple device backup options
- Full backup of a single node
use
Run the following command on the terminal to install mib globally.
npm i @qc2168/mib -g
Copy the code
Configuration script File
You need to create an. Mibrc file in the user directory for the first time and set related parameters.
{
"backups": [{"path": "/sdcard/MIUI/sound_recorder/call_rec"."comment": "Call recording"
},
{
"path": "/sdcard/DCIM/Camera"."comment": "Local Album"
},
{
"path": "/sdcard/DCIM/Creative"."comment": "My Creation."
},
{
"path": "/sdcard/Pictures/weixin"."comment": "Wechat Album"
},
{
"path": "/sdcard/tencent/qq_images"."comment": "QQ photo album"
},
{
"path": "/ sdcard/Pictures/zhihu"."comment": "Zhihu"
},
{
"path": "/sdcard/tieba"."comment": "Post"
},
{
"path": "/sdcard/DCIM/Screenshots"."comment": "Screen shot"
},
{
"path": "/sdcard/DCIM/screenrecorder"."comment": "Screen recording"
},
{
"path": "/sdcard/MIUI/sound_recorder"."comment": "Recording"
},
{
"path": "/sdcard/MIUI/sound_recorder/app_rec"."comment": "Application recording"}]."output": "E:/backups/MI10PRO"
}
Copy the code
Perform a backup
In the console, you can directly enter mib to trigger the script, and no other parameters are required.
mib
Copy the code
The console will output the corresponding information based on the configuration file.
2022-04-09 20:58:11 info current backup task: screen recording. 2022-04-09 20:58:11 info backup quantity 1 2022-04-09 20:58:11 info obtained data. 24Mb 2022-04-09 20:58:11info backup volume 24Mb 2022-04-09 20:58:11info backing up Screenrecorder-2022-04-08-19-45-51-836.mp4 2022-04-09 20:58:12info Average Transmission speed 27.7 MB/s 2022-04-09 20:58:12 info Current backup task: Recording 2022-04-09 20:58:12 INFO Backup quantity 0 2022-04-09 20:58:12 INFO Backup volume 0 MB 2022-04-09 20:58:12 info No backup required. 2022-04-09 20:58:13 INFO The program endsCopy the code
If you want to learn more about this project, please go to the project code: github.com/QC2168/mib