Skip to main content

Syncing Application Core and Binaries

Learning Focus

By the end of this module, you will understand how to isolate application code from stateful data during rsync operations, ensuring clean deployments and reliable disaster recovery across any tech stack.

The Anatomy of an Application

Most modern web applications follow a clear separation of concerns between code, configuration, and data. Understanding this structure is key to efficient syncing:

/var/www/my-app/
├── bin/ ← Binaries and executables (sync this)
├── src/ ← Application source code (sync this)
├── vendor/ ← Dependencies (usually managed by package manager)
├── public/ ← Entry points and static assets (sync this)
├── config/ ← Environment-specific configuration (EXCLUDE)
├── storage/ ← User-generated content and logs (EXCLUDE)
└── .env ← Secrets and credentials (EXCLUDE)
ComponentTypeSync StrategyWhy
System/CoreImmutable CodeSyncFiles are identical across environments of the same version.
BinariesExecutablesSyncScripts and compiled binaries needed for execution.
DependenciesExternal CodeExclude / SeparateUsually re-installed via npm install, composer install, etc.
ConfigurationEnvironmentExcludeContains DB credentials and secrets unique to the server.
User DataStateful DataExcludeHandled in separate data-focused sync operations.

Core Sync Commands

Syncing Immutable Application Files

The most common pattern is to sync the code while protecting the environment.

Deploying code to production
rsync -av \
--exclude='storage/' \
--exclude='vendor/' \
--exclude='.env' \
--exclude='.git/' \
/local/project/root/ user@production:/var/www/my-app/

Syncing Specific System Directories

When you only need to update specific application layers:

# Sync shared libraries or includes
rsync -av /src/libs/ user@remote:/app/libs/

# Sync administrative tools or CLI binaries
rsync -av /src/bin/ user@remote:/usr/local/bin/my-app-tools/

Clean State Restoration (Disaster Recovery)

If an application's core files are compromised or corrupted, use --delete to ensure the remote matches the local "clean" state exactly.

Restoring from a clean local reference
rsync -av --delete \
--exclude='config/' \
--exclude='storage/' \
/opt/my-app-clean/ /var/www/my-app/
important

The --delete flag removes any files on the destination that do not exist on the source. This is essential for removing malware injections or accidental file additions in core directories.


Application-Specific Patterns

While the principle of "Code vs. Data" is universal, different frameworks have different root-level files that should be excluded.

FrameworkCore Files to SyncExclude (Data/Config)
Node.js / Expressdist/, src/, package.jsonnode_modules/, .env, logs/
Laravel / PHPapp/, bootstrap/, public/storage/, vendor/, .env
Django / Pythonapps/, manage.py, templates/media/, venv/, settings.py
Gobin/, assets/logs/, config.yaml
WordPresswp-admin/, wp-includes/, root PHPwp-content/, wp-config.php, .htaccess

Strategy: The "Clean Reference" Directory

Sysadmins often keep a read-only "clean reference" of the application version currently running in production. This allows for instant integrity checks:

Verify production integrity
rsync -avnc /opt/clean-reference/ /var/www/my-app/ \
--exclude='storage/' \
--exclude='.env'

The -c flag forces a checksum comparison, and -n (dry-run) prevents any actual changes.


Best Practices

PracticeRationale
Exclude ConfigCredentials should never be synced between environments.
Exclude LogsLogs are unique to the instance and can be massive; use a separate tool for log aggregation.
Use Dry RunsAlways verify the file list before committing to a sync that involves deletions.
Fix PermissionsAfter syncing, ensure the web user (e.g., www-data or nginx) owns the new files.
Atomic SyncsFor zero-downtime, sync to a new directory and swap a symlink (current -> /app/v1.2).

Common Pitfalls

PitfallConsequencePrevention
Syncing .env filesOverwrites production secrets with dev or staging credentials.Always explicitly --exclude='.env'.
Syncing vendor/ or node_modules/Enormous transfer times; may introduce incompatible binary versions.Use package managers to rebuild dependencies.
Missing --delete in recoveryResidual corrupted files or malware remain in core directories.Use --delete when the source is a trusted "clean" copy.
Failing to exclude large dataSaturates bandwidth and delays code deployment.Move data/media sync to a separate, lower-priority job.

What's Next