# Symmetric Synchronization

    library(syncdr)

    # Create .syncdrenv
    .syncdrenv = toy_dirs()
    #> ■■■■■■■■■■■■■■■                   47% | ETA:  5s
    #> ■■■■■■■■■■■■■■■■■■■               60% | ETA:  5s

    # Get left and right directories' paths
    left  <- .syncdrenv$left
    right <- .syncdrenv$right

This article covers functions designed for symmetric synchronization between two directories.

## What is symmetric synchronization?

This is a two-way synchronization: this means that you compare both directories and update each other to reflect the latest changes: If a file is added, modified, or deleted in one directory, the corresponding action is taken in the other directory. This approach is useful when you want both directories to be always up-to-date with the latest changes, regardless of where those changes originate.

To keep in mind:

When calling these synchronization functions, you can provide inputs in two alternative ways:

1. Specify paths of both left and right directories, as well as the `by_date` and `by_content` arguments as you wish the synchronization to be performed (if not specified, by default `by_date = TRUE` and `by_content` = FALSE) OR

2. First call the workhorse function `compare_directories()` to obtain the sync_status object. Then, provide it as input to the synchronization function. You do not need to specify the ‘by_date’ and ‘by_content’ arguments, as they will automatically be determined depending on the ‘sync_status’.

## Types of symmetric synchronization

Similar to its asymmetric counterpart, `syncdr` enables the execution of specific symmetric synchronizations with predefined options, allowing you to select the most suitable function based on your requirements.

Types of symmetric synchronization
Type of synchronization | Actions on common files | Actions on non-common files
--- | --- | ---
Full symmetric synchronization: `full_symmetric_sync()` | if `by_date = TRUE` only: If the file in one directory is newer than the corresponding file in the other directory it will be copied over to update the older version If modification dates are the same, no change is made if `by_date = TRUE` and `by_content = TRUE`: If the file in one directory is newer AND different than the corresponding file in the other directory, it will be copied over to update the older version. If modification dates/contents are the same, no change is made if `by_content = TRUE` only: no action | if a file exists in one but not in the other directory, it is copied over accordingly
| if `by_date = TRUE` only: If the file in one directory is newer than the corresponding file in the other directory, it will be copied over to update the older version. If modification dates are the same, no action is executed if `by_date = TRUE` and `by_content = TRUE`: If the file in one directory is newer AND different than the corresponding file in the other directory, it will be copied over to update the older version If modification dates/contents are the same, nothing is done if `by_content = TRUE` only: no action | No changes are made: keep in right files that are only in right keep in left those that are only in left

Let’s consider the following examples:

##### *️⃣ Note: `verbose = TRUE`

As with asymmetric synchronization, also here you have the option to enable verbose mode by setting `verbose = TRUE`. This will display the tree structure of both directories BEFORE and AFTER the synchronization.

1 - Full symmetric synchronization:

When comparing directories by date and content:

    sync_status <- compare_directories(left_path  = left,
                                       right_path = right,
                                       by_content = TRUE)

    # Providing left and right paths object
    # full_symmetric_sync(left, right)

    # Providing sync_status object
    full_symmetric_sync(sync_status = sync_status)
    #> These files will be COPIED (overwriting if present) to RIGHT
    #>
    #>
    #> |Files     |Action       |
    #> |:---------|:------------|
    #> |/A/A1.Rds |To be copied |
    #> |/A/A2.Rds |To be copied |
    #> |/A/A3.Rds |To be copied |
    #> |/B/B3.Rds |To be copied |
    #> |/D/D1.Rds |To be copied |
    #> |/D/D2.Rds |To be copied |
    #> These files will be COPIED (overwriting if present) to LEFT
    #>
    #>
    #> |Files               |Action       |
    #> |:-------------------|:------------|
    #> |/B/B1.Rds           |To be copied |
    #> |/B/B2.Rds           |To be copied |
    #> |/C/C1_duplicate.Rds |To be copied |
    #> |/C/C2.Rds           |To be copied |
    #> |/C/C3.Rds           |To be copied |
    #> |/D/D3.Rds           |To be copied |
    #> |/E/E1.Rds           |To be copied |
    #> |/E/E2.Rds           |To be copied |
    #> |/E/E3.Rds           |To be copied |
    #> Do you want to proceed? Type your answer (Yes/no/cancel)
    #> ✔ synchronized

When comparing directories by date only:

    sync_status <- compare_directories(left_path  = left,
                                       right_path = right)

    # Example with left and right paths
    full_symmetric_sync(left_path = left,
                        right_path = right)
    #> Do you want to proceed? Type your answer (Yes/no/cancel)
    #> ✔ synchronized

2 - Partial symmetric synchronization:

    sync_status <- compare_directories(left_path  = left,
                                       right_path = right)

    partial_symmetric_sync_common_files(sync_status = sync_status)
    #> Do you want to proceed? Type your answer (Yes/no/cancel)
    #> ✔ synchronized