متقدم
البحث فيأدلة «كيف»
طريقة البحث
  • اكتب المهمة بكلماتك (find port، open firewall، disk full) أو الصق رسالة الخطأ بين علامتي تنصيص.
  • اختر «الإجابة السريعة» لترى الأوامر فقط، أو مجالًا لتبقى في مجال واحد.
  • "كلمتان" تجد العبارة بالضبط.
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث
جرّب:

كيف 88System · Backup

How to back up a server with restic

A real backup has versions (yesterday, last week, last month), is encrypted, lives somewhere else, and is tested. restic gives all of that in one small program: deduplicated, encrypted snapshots to S3, MinIO, SFTP or a disk. This how-to sets up a repository, backs up the important folders and a database dump, keeps a retention policy, restores a file, and runs it every night.

  • 5 الدروس
  • 9 الأوامر

هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.

$ مستخدم عادي# بصلاحية root (أو مع sudo)

88.1

The quick answer

/etc/restic/env
RESTIC_REPOSITORY=s3:https://s3.example.com/backups/web1
RESTIC_PASSWORD_FILE=/etc/restic/password
AWS_ACCESS_KEY_ID=<backup user key>
AWS_SECRET_ACCESS_KEY=<backup user secret>
set -a; . /etc/restic/env; set +a
restic init
restic backup /etc /srv /var/www --one-file-system --tag files
restic snapshots
restic forget --keep-daily 7 --keep-weekly 4 --keep-monthly 12 --prune

الأوامر في هذا الدرس

88.2

Step by step

  1. Install (dnf install restic from EPEL, or apt-get install restic) and create the secret files:
install -d -m 700 /etc/restic
(umask 077; openssl rand -base64 32 > /etc/restic/password)
chmod 600 /etc/restic/env

Store a copy of the repository password outside the server (password manager). Without it, nobody can ever read the backup — also not you.

  1. Initialise the repository once (quick answer). Use a backup user with access only to its own bucket/path.
  2. What to back up: configs (/etc), app data (/srv, /var/www), and database dumps (not the live database files):
sudo -u postgres pg_dump -Fc shop | restic backup --stdin --stdin-filename shop.dump --tag postgres
  1. Retention — keep 7 daily, 4 weekly, 12 monthly snapshots, free the space:
restic forget --keep-daily 7 --keep-weekly 4 --keep-monthly 12 --prune
  1. Restore — one folder into a test path, or one file:
restic snapshots --tag files
restic restore latest --tag files --target /tmp/restore --include /etc/nginx
restic dump latest --tag postgres shop.dump > /tmp/shop.dump
  1. Run it every night — a systemd service + timer:
/etc/systemd/system/restic-backup.service
[Unit]
Description=restic backup
 
[Service]
Type=oneshot
EnvironmentFile=/etc/restic/env
ExecStart=/usr/bin/restic backup /etc /srv /var/www --one-file-system --tag files
ExecStartPost=/usr/bin/restic forget --keep-daily 7 --keep-weekly 4 --keep-monthly 12 --prune
Nice=10
IOSchedulingClass=idle

Add a restic-backup.timer with OnCalendar=*-*-* 02:15:00 and Persistent=true, then systemctl enable --now restic-backup.timer. Alert when it fails (a push monitor in Uptime Kuma, or a Prometheus textfile metric).

  1. Check the repository now and then:
restic check
restic check --read-data-subset=5%

88.3

Check that it worked

restic snapshots shows a new snapshot every day, restic check has no errors, and a test restore (monthly) gives you the files and a database dump that restores.

88.4

Common problems

ProblemCauseFix
Fatal: unable to open config file: … Is there a repository at the following location?Not initialised, or wrong RESTIC_REPOSITORY.restic init once; check the URL.
wrong password or no key foundWrong password file.Use the original password — there is no reset.
unable to create lock … repository is already lockedAnother run, or a crashed run left a lock.Wait; then restic unlock if no run is active.
Backup is huge / slowIt walks caches, mounts or /proc.--one-file-system, --exclude-caches, an exclude file.
Database restore failsThe live DB files were copied while running.Back up a dump (pg_dump, mysqldump) through --stdin.
Nobody noticed backups stoppedNo alert.Alert on failure and on "no snapshot in 26 hours".

الأوامر في هذا الدرس

88.5

Go further

1What happens if you lose the restic repository password?

The backup can never be read again — keep a copy of the password outside the server.

2Why back up a database dump instead of the database's data files?

Files copied while the database runs can be inconsistent; a dump is a consistent copy.

كل الأوامر في هذا الفصل