Sync Two Apache Websites Using Rsync
In this article
- Why Use Rsync for Apache Website Sync
- Before You Start: Prerequisites
- Basic One-Way Sync Command
- Two-Way Sync vs. One-Way: What You Need
- Automating with Cron and Handling Conflicts
- Common Problems and How to Avoid Them
- Alternative Tools and When to Use Them
- Safety and Best Practices
- Frequently Asked Questions
Yes, you can sync two Apache websites using rsync, and it is one of the most reliable methods for keeping web content identical across servers. Rsync is a command-line tool that copies files and directories, transferring only the differences between source and destination, which makes it fast and efficient for ongoing synchronization.
Why Use Rsync for Apache Website Sync
Rsync is widely used by system administrators because it handles large directory trees well, preserves file permissions and ownership, and works over SSH for secure transfers. When you run Apache on two servers—for load balancing, failover, or staging—keeping the document root (often /var/www/html or /var/www) in sync is essential. Rsync does this without copying unchanged files, saving bandwidth and time. It also supports compression and can resume interrupted transfers, which is helpful for large sites.
Before You Start: Prerequisites
You need SSH access to both servers, and rsync must be installed on both. On Debian/Ubuntu, install with sudo apt install rsync; on CentOS/RHEL, use sudo yum install rsync. You also need write permissions to the destination directory. For security, using SSH keys is recommended over password authentication, so you can run rsync without interactive prompts—ideal for cron jobs.
Basic One-Way Sync Command
The core command to sync from a source to a destination is:
rsync -avz --delete /var/www/html/ user@dest-server:/var/www/html/
Here is what the flags mean:
-a(archive): preserves permissions, timestamps, symlinks, and recurses into directories.-v(verbose): shows what is being copied.-z(compress): compresses data during transfer to save bandwidth.--delete: removes files on the destination that no longer exist on the source, making the destination an exact mirror.
The trailing slash on the source path is critical: /var/www/html/ copies the contents of the directory, while /var/www/html copies the directory itself, which would create /var/www/html/html on the destination.
Two-Way Sync vs. One-Way: What You Need
Rsync is fundamentally one-way: it copies from source to destination. For true two-way sync, where changes on either server propagate to the other, you need a tool like unison or a distributed filesystem. However, for most Apache setups, one-way sync is sufficient—you push from a master server to a slave or staging server. If you need both servers to accept changes, consider using a shared storage solution or a version control system like Git for your web files, then deploy from the repository.
Automating with Cron and Handling Conflicts
To keep the sites continuously in sync, add a cron job. For example, run the rsync command every 5 minutes:
*/5 * * * * rsync -az --delete /var/www/html/ user@dest-server:/var/www/html/
Be careful with --delete in automated jobs: if the source directory becomes empty or corrupted, the destination will be wiped. To mitigate this, you can add --backup to keep backups of deleted files, or use a dry run (-n) first. Also, ensure that Apache on the destination is not writing to the same files (like logs or cache) that you are syncing, or you may get conflicts or corrupted files.
Common Problems and How to Avoid Them
Several issues commonly arise when syncing Apache sites:
- Permission mismatches: If the web server runs as
www-dataon one server andapacheon another, file ownership may break. Use--chown=www-data:www-dataor ensure user IDs match. - Symlinks: By default, rsync copies symlinks as symlinks. If your site uses absolute symlinks, they may point to wrong paths on the destination. Use
-Lto copy the referenced files instead. - Large files: For databases or large media, rsync may be slow. Consider using
--partialto keep partially transferred files and resume later. - SSH key passphrase: If your key has a passphrase, cron jobs will fail. Use
ssh-agentor create a key without a passphrase (with restricted permissions) for automation.
Alternative Tools and When to Use Them
Rsync is not the only option. For real-time sync, you might use lsyncd, which watches directories and triggers rsync on changes. For many servers, tools like Ansible or Puppet can deploy files, but they are more for configuration management than continuous sync. If you need to sync a database, rsync is not suitable because the database files may be inconsistent; use database replication tools like MySQL replication instead. For static sites, you could also use a content delivery network (CDN) or a version control system like Git to manage deployment.
Safety and Best Practices
Always test rsync with --dry-run (-n) before executing. Keep a backup of your destination before the first sync. Use --exclude to skip directories like .git, cache, or logs that should not be overwritten. For example:
rsync -avz --delete --exclude='.git' --exclude='cache/' /var/www/html/ user@dest-server:/var/www/html/
Also, ensure you are syncing the correct Apache document root—check your virtual host configuration with apachectl -S to see which directory is actually served.
Frequently Asked Questions
Can I sync two Apache websites in real time?
Rsync itself is not real-time; it runs on a schedule. For near-real-time, use lsyncd which monitors file changes and triggers rsync immediately. However, there is a slight delay and it may not catch every change if the server crashes. For true real-time, consider a distributed filesystem like GlusterFS or a database replication if the content is dynamic.
What is the difference between rsync and scp for syncing websites?
rsync is superior for syncing because it only transfers changed parts of files, supports compression, and can delete extraneous files. scp copies everything each time, which is slower and does not handle deletions. For large sites, rsync is the clear choice.
How do I sync a website from a remote server to my local machine?
Reverse the source and destination in the command. For example, to pull from a remote server to your local machine: rsync -avz user@source-server:/var/www/html/ /local/path/. This is useful for backup or local development.
Does rsync preserve Apache file permissions and ownership?
Yes, with the -a flag, rsync preserves permissions, ownership, timestamps, and symlinks. However, ownership preservation requires root privileges on the receiving side, or you may need to use --chown to set the correct owner.
What happens if the destination server is down during a sync?
Rsync will fail with a connection error, and nothing will be transferred. If you are using cron, the job will simply fail and you can set up email alerts. To avoid partial states, rsync uses a temporary file and renames it only after the transfer completes, so the destination is never left with a half-written file.
Can I sync only specific files or directories?
Yes, you can specify a subdirectory as the source, or use --include and --exclude patterns. For example, to sync only PHP files: rsync -avz --include='*.php' --exclude='*' /var/www/html/ user@dest:/var/www/html/. This is useful if you only want to push code changes and not static assets.