Why My Backup Directory Kept Disappearing

A lesson learned the hard way with rsync --delete and cron jobs.

Introduction

I was working on a simple backup script to back up my home directory and some /srv scripts to my NFS server. Everything seemed to be going fine—I created a directory called weekly_home_bkups on my NFS share, and my script was tarring and zipping files correctly. I could even restore them afterward.

At that point, I felt like things were in a good place, so I decided to clean up my work and start adding documentation.

I ran the script again to verify timestamps and formatting. Something was off—now it was failing and timing out like the NFS server wasn’t reachable. I tried to cd into my mount and noticed something weird: my backup directory was gone.

It was late, so I assumed it was just some weird glitch. I recreated weekly_home_bkups, ran the script again, and it worked fine. Everything looked good, so I called it a night.

The next day, I ran the script again and it failed in the same way. The directory was gone again. The mount was fine, I was in the correct path, but the directory itself just wasn’t there.

That’s when I started digging into what was actually happening.

What was actually happening

It turned out I had a cron job running every hour:

0 * * * * rsync -rlDv --delete /srv/www/nginx/ /mnt/bk_to_nfs1/

This job was syncing my lab web server directory to the same NFS mount I was using for backups.

The issue was the --delete option.

--delete makes the destination match the source exactly. Anything in the destination that does not exist in the source gets removed during the sync.

So when I kept creating weekly_home_bkups inside /mnt/bk_to_nfs1/, rsync saw it as “extra” because it didn’t exist in /srv/www/nginx/, and it removed it on the next cron run.

Lesson learned

I was treating /mnt/bk_to_nfs1/ like a general storage directory, but rsync was treating it like a strict mirror of /srv/www/nginx/. That combination is what caused the issue.

Older Posts