0

I am attempting to backing up a user profile (or more accurately copying specific directories e.g. username\documents, username\desktop, etc.) to a network shared drive. I'm using C++ builder (10.1 starter), thus limited to boost 1.55.

I've found this copyDir method by nijansen but it has an issue: it "breaks" when attempting to copy to a directory that already exists because of this code:

if(fs::exists(destination))
{
    std::cerr << "Destination directory " << destination.string()
        << " already exists." << '\n';
    return false;
}

Obviously I could just change it to delete directory if exists, then rather than returning instead let it proceed with copying, but I'm looking for a bit more.

What I would like to do is mirror the directories from the original to the backup (like robocopy's /MIR flag (including subdirectories)). If I backup a user today, then they delete a few things, create a few more, edit a document, etc., I want to copy just those changes into tomorrow's backup (and not re-backup everything that didn't change). Some users have 50+ GB of data which would take entirely too long if I were to just delete their "yesterday's" backup folder and create an entirely new backup.

Summary: Is there a way to mirror (not just copy!) directory trees between a source and destination using BOOST?

Any help and/or examples would be greatly appreciated! Thanks in advance!

Community
  • 1
  • 1
RisaAudr
  • 53
  • 1
  • 5
  • 10

1 Answers1

0

To answer my own question, the copy_file method has a 3rd argument that can be passed to it - fs::copy_option::overwrite_if_exists. If you take nijansen's copyDir method and add this option to the copy_file call, it will overwrite a file if it already exists (updating a file in a backup from live).

The function call then becomes:

fs::copy_file(current,
    destination / current.filename(), 
    fs::copy_option::overwrite_if_exists);

Also, for anyone who stumbles upon this question, I also figured out a way to exclude files/directories from the copy_file (add the if statements to the "else" that does the copy within the copyDir method.

// Need to exclude My Music, My Pictures, My Videos (from Documents folder)
std::string stem = current.filename().string();
//If not equal to excludes, then copy away; else skips them
if (!(stem == "My Music" || stem == "My Pictures" || stem == "My Videos"))
{
    fs::copy_file(current,destination / current.filename(), 
        fs::copy_option::overwrite_if_exists);
}

Anyhow, thanks!

Community
  • 1
  • 1
RisaAudr
  • 53
  • 1
  • 5
  • 10