Help us measure site usage?

With your permission, we use self-hosted analytics to understand page usage. It is optional and does not affect the Service. Learn more.

BashbeginnercliAI generated

Directory Sync & Conflict Logger

Build a Bash script that keeps two folders in sync with rsync, for example your documents and a backup drive, and notices when it would be unsafe to copy. If the same file was changed on both sides since the last sync, copying either version would lose the other's changes, so the script flags it as a conflict (using sha256sum to compare contents) and leaves both copies alone. Every action and conflict is written to a log file in a consistent format, and a summary mode prints a short digest, handy for a nightly cron job. You'll learn rsync options and dry runs, file checksums, comparing file dates, writing structured logs, and scheduling scripts with cron.

Estimate
~7.5h
Steps
6
Completed by
0
Proposed by
codeseed.app

rsync · sha256sum · cron

Project roadmap

  1. 01

    Create test folders

    ~1h

    Write a setup-test.sh script that creates a source and a destination folder with sample files: some identical, some only on one side, some changed only in the source, and some changed differently on both sides. Running it again resets the folders. Done when: the script builds the same test folders every time.

  2. 02

    Wrap rsync

    ~1h

    Write sync.sh with set -euo pipefail that takes a source and destination (checking both exist), and calls rsync -a to copy the source to the destination, with an option --dry-run that passes -n to rsync so nothing is changed. Print what rsync did using --itemize-changes. Done when: running sync.sh makes the destination match the source, and --dry-run changes nothing.

  3. 03

    Detect conflicts

    ~2h

    After each sync, save a manifest file with the sha256sum of every file. Before the next sync, compare each file on both sides with the manifest: a file changed on both sides since the last sync, with different contents, is a conflict. Exclude conflicted files from rsync (with --exclude-from) so neither copy is overwritten. Done when: on the test folders, files changed on both sides are flagged and left untouched, and the rest sync normally.

  4. 04

    Write structured logs

    ~1h

    Write a log function that appends lines in one format: ISO date, action (COPY, DELETE, SKIP or CONFLICT), file path and result, separated by tabs, to a log file (default sync.log). Log every rsync change and every conflict. Done when: the log has one well-formed line per action, easy to filter with grep.

  5. 05

    Add a summary mode for cron

    ~1h

    Add --summary, which prints only a short digest: the number of files copied and deleted, and the list of conflicts with instructions to resolve them. Exit with code 2 when there are conflicts, so cron can email you. Add an example crontab line to README.md, with full paths, since cron runs with a minimal environment. Done when: --summary prints a short digest, and the exit code reflects conflicts.

  6. 06

    Test it

    ~1.5h

    Write a test script (or use bats-core) that runs setup-test.sh, then sync.sh, and checks: synced files match, conflicts are detected and untouched, the log has the right lines, and a second run with no changes does nothing. Check the script with shellcheck. Done when: the tests pass, and shellcheck reports no warnings.

Ready to build this?

Get a GitHub repo and start building. Your AI reviewer checks each step as you go.

~7.5h · 6 steps

Tech stack

rsyncsha256sumcron