Backup and Restore (OTG )

Discussions about creating CAPI applications to run on Android devices
Post Reply
devasri
Posts: 2
Joined: January 4th, 2023, 7:17 pm

Backup and Restore (OTG )

Post by devasri »

Dear All,

Backup and Restore (OTG)

I want write a Menu Driven Program, For data entry, reports, backup and restore . Here I need to back up my data files to an External Drive (External USB Pen Drive ) via OTG and restore when necessary from OTG device to Tablet Computer which is running android.

Target tables : Samsung, lenovo

Does any one give me sample code or clue to do those operations?


Thank you

Deva
aaronw
Posts: 564
Joined: June 9th, 2016, 9:38 am
Location: Washington, DC

Re: Backup and Restore (OTG )

Post by aaronw »

If you were synchronizing to a server, I would rather leverage synchronization to backup and restore data. However, I have written logic to backup and delete data files locally in the past. To restore the data you'd need more of less reverse the process.
// ------------------
// ------------------ Back up and Delete data (Windows & Android)
// ------------------

list string fileListing;
string dataPath = "..\Data\";
string assignmentsCsdb = "Assignments.csdb";
string staffCsdb = "Staff.csdb";
string householdCsdb = "Household.csdb";

function DataExists()

    DataExists = 0;
       
    if 0 < length(fileListing) then
       
DataExists = 1;
    endif;
   
end;

function string CreateUniqueZipPath()

    CreateUniqueZipPath = maketext("../DataBackup-%d.zip", timestamp());

end;

function CompressDataFiles()

        CompressDataFiles = 0;
       
        string zipPath = CreateUniqueZipPath();
        if compress(zipPath, fileListing) <> default then
           
CompressDataFiles = 1;
        endif;
       
end;

function DeleteDataFiles()

    // Displays error message if any file cannot be deleted
   
numeric success = 1;
   
    if success and fileexist(dataPath + assignmentsCsdb) and filedelete(dataPath + assignmentsCsdb) = default then
       
success = 0;
    endif;
       
    if success and fileexist(dataPath + staffCsdb) and filedelete(dataPath + staffCsdb) = default then
       
success = 0;
    endif;
   
    if success and fileexist(dataPath + householdCsdb) and filedelete(dataPath + householdCsdb) = default then
       
success = 0;
    endif;
       
    DeleteDataFiles = success;
           
end;

function BackupAndDeleteData()

    numeric input = errmsg("WARNING: This will delete your data directory. Only do this if you know what you are doing!")
        select("Confirm", continue, "Cancel", continue);
       
    if input = 1 then

        if not direxist
(dataPath) then
            errmsg
("There is no data to delete.");
            exit;
        endif;
   
        if not dirlist(fileListing, dataPath, recursive) then
            errmsg
("Data directory structure could not be read. Try restarting your device.");
            exit;
        endif;
   
        if not DataExists() then
            errmsg
("There is no data to delete.");
            exit;
        endif;
       
        if not CompressDataFiles() then
            errmsg
("Data files could not be backed up. Try restarting your device.");
            exit;
        endif;
       
        if not DeleteDataFiles() then
            errmsg
("Data files could not be deleted. Try restarting your device.");
            exit;
        endif;
       
    endif;

end;
devasri
Posts: 2
Joined: January 4th, 2023, 7:17 pm

Re: Backup and Restore (OTG )

Post by devasri »

Thank you aaronw for your code.

Code: Select all

function CompressDataFiles()
        CompressDataFiles = 0;
       
        string zipPath = CreateUniqueZipPath();
        if compress(zipPath, fileListing) <> default then
            CompressDataFiles = 1;
        endif;
end;
Function above create zip file thank you again, After that I need to copy the zip file to External Storage via OTG Adapter. (removable Storage / USB)


I stuck here with filecopy function and PathName Function (s = pathname(path_type ‖ dictionary_name ‖ file_handler)) with path_type "CSEntryExternal" as CSPro help

I am new to CSPro and seeks your more advice on coping file to removable device via OTG adapter.

Thank you

Deva
aaronw
Posts: 564
Joined: June 9th, 2016, 9:38 am
Location: Washington, DC

Re: Backup and Restore (OTG )

Post by aaronw »

Given what I wrote before you could make a change to the function CreateUniqueZipPath, so it returns just the file name. This will make constructing the destination path for the zip straightforward.
function string CreateUniqueZipName()

    CreateUniqueZipName = maketext("DataBackup-%d.zip", timestamp());

end;

function CompressDataFiles()

        CompressDataFiles = 0;
        string zipFilename = CreateUniqueZipName();
      
        string zipPath = "../" + zipFilename;
        if compress(zipPath, fileListing) <> default then
           
CompressDataFiles = 1;
        endif;
       
        // copy zip to SD card
       
string destPath = path.concat(CSEntryExternal, zipFilename);
        filecopy(zipPath, destPath);
       
end;
CSEntryExternal is going to work for storage like an SD card, but it's not going to return the path for a USB pen drive. If you have uniform devices and USB pen drives you may be able to hard code the path. However, we don't typically recommend hard coding anything, because it's going to fail as soon as something changes. Alternatively, the copying process may need to be manual.
Post Reply