-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathBackupInternalPartitions.ino
134 lines (90 loc) · 4.36 KB
/
BackupInternalPartitions.ino
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
/*
BackupInternalPartitions
This code demonstrates how the "Arduino_UnifiedStorage" can be used to access multiple partitions on the internal storage,
and transfer information to a USB Mass storage device.
In the setup function, the code initializes serial communication, and registers a callback for the insertion of the USB Drive.
If the device is successfully mounted, a folder for this instance of a backup will be created on the USB Drive.
Afterwards the sketch does the following:
- lists all partitions available on the InternalStorage
- mounts each partition
- creates a sub folder for each partition,
- copies everything on that partition to the corresponding subfolder.
The "addSomeFakeFiles" function generates random files in the specified folder, simulating real data.
The "move" function is responsible for transferring folders and files between storage locations.
INSTRUCTIONS
- Make sure you have "POSIXStorage" and "Arduino_UnifiedStorage" installed
- Insert a USB Drive whenever you want
- Wait for the sketch to finish, it will display the following "DONE, you can restart the board now" when successful
- Unplug the USB device and inspect its contents.
Created: 31th August 2023
By: Cristian Dragomir
Source: https://github.com/arduino-libraries/Arduino_UnifiedStorage/blob/main/examples/BackupInternalPartitions/BackupInternalPartitions.ino
*/
#include <Arduino_UnifiedStorage.h>
constexpr boolean createFakeFiles = true;
boolean done = false;
volatile boolean connected = false;
USBStorage thumbDrive;
void addSomeFakeFiles(Folder * folder){
Serial.println("Adding some fake files to: " + String(folder -> getPathAsString()));
for (int i = 0; i < random(0, 9); i++){
UFile thisFile = folder -> createFile("File_"+ String(random(999)), FileMode::WRITE);
Serial.println("\t * " + thisFile.getPathAsString());
thisFile.write("writing stuff to the file");
thisFile.close();
}
Folder subfolder = folder -> createSubfolder("ChildFolder_"+ String(random(999)));
for (int i = 0; i < random(0, 9); i++){
UFile thisFile = subfolder.createFile("File_"+ String(random(999)), FileMode::WRITE);
Serial.println("\t * " + thisFile.getPathAsString());
thisFile.write("writing stuff to the file");
thisFile.close();
}
}
void move(Folder * source, Folder * dest){
for(Folder f: source -> getFolders()){
Serial.println("Copying folder :" + String(f.getPathAsString()));
f.moveTo(*dest);
}
for(UFile f: source -> getFiles()){
Serial.println("Copying file :" + String(f.getPathAsString()));
f.moveTo(*dest);
}
}
void setup(){
randomSeed(analogRead(A0));
Serial.begin(115200);
while(!Serial);
// toggle this to enable debugging output
Arduino_UnifiedStorage::debuggingModeEnabled = false;
thumbDrive = USBStorage();
bool thumbMounted = thumbDrive.begin(FS_FAT);
if(thumbMounted){
Serial.println("USB Thumb Drive has been mounted");
Folder thumbRoot = thumbDrive.getRootFolder();
String folderName = "InternalBackup_" + String(millis());
Serial.println(folderName);
Folder backupFolder = thumbRoot.createSubfolder(folderName);
int partitionIndex = 0;
std::vector<Partition> partitions = InternalStorage::readPartitions();
Serial.println("Found " + String(partitions.size()) + " partitions on internalStorage \n");
for (auto part: partitions){
partitionIndex++;
const char * partitionName = createPartitionName(partitionIndex);
Folder thisPartitionBackupFolder = backupFolder.createSubfolder(partitionName);
InternalStorage thisPartition = InternalStorage(partitionIndex, partitionName, part.fileSystemType);
thisPartition.begin();
Folder partitionRootFolder = thisPartition.getRootFolder();
Serial.println(partitionRootFolder.getPathAsString());
if(createFakeFiles){
addSomeFakeFiles(&partitionRootFolder);
}
move(&partitionRootFolder, &thisPartitionBackupFolder);
thisPartition.unmount();
}
thumbDrive.unmount();
Serial.println("DONE, you can restart the board now");
}
}
void loop(){
}