A ransomware-resistant third copy with rclone
If every backup sits in one object store, a single delete takes them all. A pull-model 3-2-1 copy with rclone, a read-only user and --backup-dir.
Backups were taken regularly, and a restore drill had been done. But they all lived in one object store, at one site. If that disk or site is lost, or someone with write access (or a stolen key) deletes everything, every backup goes at once. You think you have backups, but really you have a single copy.
Why does it happen?
Backups are often set up so that "a copy of the data is somewhere else", and left there. The 3-2-1 rule says that isn't enough:
- 3 copies of the data,
- on 2 different media,
- 1 of them at another site.
The real risk is that the side doing the copying can also write to the source. In a push model, the source server holds write access to the target. If the source is compromised, the target can be wiped too.
Fix: the pull model
The target, at a separate site, pulls the copy itself. The source doesn't even need to know the target exists.
- A read-only user on the source. It gets only
ListBucket,GetBucketLocationandGetObject. Even if the target is compromised, it can't delete anything at the source.
{
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Action": ["s3:ListBucket", "s3:GetBucketLocation", "s3:GetObject"],
"Resource": ["arn:aws:s3:::*"]
}]
}
- A nightly
rclone syncper bucket on the target. The key parameter here is--backup-dir. Files deleted or changed at the source don't vanish on the target; they are moved into a dated folder. A deletion that spreads from the source doesn't empty the target. It can be undone within N days.
#!/bin/bash
# /usr/local/bin/ucuncu-kopya.sh
set -euo pipefail
BUCKETS="yedek-db yedek-dosya"
HEDEF=/srv/kopya
TARIH=$(date +%F)
SAKLA_GUN=7
for b in $BUCKETS; do
rclone sync "kaynak:$b" "$HEDEF/guncel/$b" \
--backup-dir "$HEDEF/deleted/$b/$TARIH" \
--bwlimit "08:00,4M 19:00,off" \
--transfers 4 --checkers 8 \
--exclude "snapshots/**" \
--log-file /var/log/ucuncu-kopya.log --log-level INFO
done
# Clean up deleted folders past their retention
find "$HEDEF/deleted" -mindepth 2 -maxdepth 2 -type d -mtime +"$SAKLA_GUN" -exec rm -rf {} +
# Success stamp: monitoring checks its age
date +%s > /var/lib/ucuncu-kopya/son-basari
--bwlimit "08:00,4M 19:00,off": bandwidth is capped during business hours and free in the evening.-
--exclude: leave out large, re-creatable data (e.g. search engine snapshots). -
Run it with a systemd timer.
flockprevents overlapping runs, andniceandionicekeep it from crushing other work.
# /etc/systemd/system/ucuncu-kopya.service
[Unit]
Description=Üçüncü kopya (rclone sync)
OnFailure=yedek-hata-mail@%n.service
[Service]
Type=oneshot
ExecStart=/usr/bin/flock -n /run/ucuncu-kopya.lock \
/usr/bin/nice -n 10 /usr/bin/ionice -c2 -n7 /usr/local/bin/ucuncu-kopya.sh
# /etc/systemd/system/ucuncu-kopya.timer
[Unit]
Description=Üçüncü kopya, her gece
[Timer]
OnCalendar=*-*-* 21:00
RandomizedDelaySec=15m
Persistent=true
[Install]
WantedBy=timers.target
systemctl daemon-reload
systemctl enable --now ucuncu-kopya.timer
systemctl list-timers ucuncu-kopya.timer
Monitoring
A backup that silently stops is more dangerous than no backup at all, because everyone assumes it exists.
- On success, write a timestamp file (
son-basariabove). - A monitoring item checks the file's age. If it is older than 26 hours, it raises an alarm. That is one day for a nightly job plus some tolerance.
- On failure, send an email via
OnFailure. The timestamp check also catches the case where the email never goes out.
Watch out
- The first full copy takes a long time. With terabytes of data it takes hours. Start it in the evening so it doesn't spill into business hours.
- Retention competes with disk space. The
deletedfolder grows. In one setup, retention started at 30 days and was cut to 7 when space got tight. Tune it to capacity, and monitor disk usage too. - Don't copy re-creatable data. Exclude large data that can be rebuilt, such as search engine snapshots.
- Protect secrets.
rclone.confand other credential files should have600permissions, readable only by the service user. - Write down the restore procedure and test it. Which folder, which command, in what order: put it in the documentation. Actually restore once.
# Example: put yesterday's deleted files back at the source (with a separate account that can write to the source)
rclone copy /srv/kopya/deleted/yedek-db/<tarih> geri-yukleme:yedek-db --dry-run
Lesson
A backup's value is measured by whether it survives the worst day. A separate site, a read-only pull and a copy that keeps deletions for a while stop a single mistake, or a single attacker, from wiping the whole history.
Checklist
- [ ] Is at least one copy of the backups at a separate site?
- [ ] Is the target's user on the source read-only?
- [ ] Does
rclone syncrun with--backup-dir? - [ ] Is bandwidth capped during business hours?
- [ ] Is there an alarm if the success stamp is older than 26 hours?
- [ ] Do credential files have
600permissions? - [ ] Is the restore documented and tested?