متقدم
البحث فيالملخصات
طريقة البحث
  • الكلمات تجد عناوين الدروس والنص والكود. صفِّ حسب النوع (أدلة، دورات، برامج، كتب) أو ابقَ في هذا الكتاب.
  • book:rhcsa و ch:8 يضيّقان البحث؛ ‎#code يجد الكلمة داخل أوامر الدروس فقط.
  • "كلمتان" تجد العبارة بالضبط.
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث
جرّب:

الفصل 18الاسم في الكتاب: Archiving and Backup

Archive, compress and back up

Backups are one of the most important jobs of an admin. Three ideas are needed: compression makes data smaller, archiving puts many files into one, and synchronizing keeps two copies the same. The tools are gzip, bzip2, xz, tar, zip and rsync.

  • 9 الدروس
  • 16 الأوامر
  • 1 المعامل

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

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

المهارات في هذا الفصل

  • الضغط والأرشفة والنسخ الاحتياطي بـ tar و rsync

18.1

Compress files with gzip

gzip compresses a file and replaces it with file.gz. gunzip restores it. Compression is lossless: you get back every byte.

ls -l /etc > foo.txt
ls -l foo.*
-rw-r--r--. 1 me me 15738 Nov  8 foo.txt
gzip foo.txt
ls -l foo.*
-rw-r--r--. 1 me me 3230 Nov  8 foo.txt.gz
gunzip foo.txt
gzip -c foo.txt > foo.txt.gz
zcat foo.txt.gz | less
OptionWhat it does
-cWrite to stdout, keep the original
-dDecompress (like gunzip)
-lShow the compression ratio
-rCompress all files in a directory tree
-tTest the compressed file
-vShow messages
-1 … -9Speed against size: -1 fastest, -9 smallest (default 6)

18.2

Use bzip2 and xz

bzip2 and xz compress better than gzip, but are slower. They have almost the same options.

ToolEndingUnpackRead
gzip.gzgunzipzcat
bzip2.bz2bunzip2bzcat
xz.xzunxzxzcat
bzip2 foo.txt
bunzip2 foo.txt.bz2
xz -T0 big.log

انتبه

Do not compress files that are already compressed (JPEG, MP4, .gz). They do not get smaller; sometimes they even grow.

18.3

Make archives with tar

tar ("tape archive") packs many files and directories into one file, a tarball. It keeps names, owners, rights and times.

ModeMeaning
cCreate an archive
xExtract an archive
rAdd files to the end of an archive
tList the contents
tar cf playground.tar playground
tar tf playground.tar
tar tvf playground.tar
mkdir foo; cd foo
tar xf ../playground.tar
tar xf ../playground.tar --wildcards 'home/me/playground/dir-*/file-A'
  • f file names the archive file; v lists the files while working.
  • Paths are stored relative. When you extract, the files go below the current directory. A leading / is removed, so an archive never overwrites system files by accident.
  • When root extracts, owners and rights are restored. A normal user becomes the owner of the files.

18.4

Compress tar archives

Add z (gzip), j (bzip2) or J (xz). The usual endings are .tgz / .tar.gz, .tar.bz2 and .tar.xz.

tar czf playground.tgz playground
tar cjf playground.tbz playground
tar cJf playground.tar.xz playground
tar xzf playground.tgz
tar czf /backup/etc-$(date +%F).tgz /etc
find playground -name 'file-A' | tar czf playground.tgz -T -

18.5

Use tar over the network

tar can write to stdout (f -) and read from stdin. With ssh you copy a whole directory tree between computers in one line.

ssh remote-sys 'tar cf - Documents' | tar xf -
tar czf - /var/www | ssh backup@nas 'cat > www.tgz'

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

18.6

Use zip for Windows users

zip archives and compresses in one step, in the format Windows and macOS open directly.

zip -r playground.zip playground
unzip -l playground.zip
unzip playground.zip
unzip playground.zip playground/dir-087/file-Z
ls -l /etc/ | zip ls-etc.zip -
  • Without -r, zip only stores the directory, not its contents.
  • unzip -l lists the contents; unzip -p writes a file to stdout.

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

18.7

Synchronize with rsync

rsync makes a target the same as the source, and copies only the differences. It is the standard tool for backups.

rsync -av playground foo
rsync -av playground foo
sending incremental file list
 
sent 22,635 bytes  received 20 bytes  45,310.00 bytes/sec
touch playground/dir-099/file-Z
rsync -av playground foo
rsync -av source/ destination
rsync -av --delete /etc /home /usr/local /media/BigDisk/backup
OptionWhat it does
-aArchive mode: recursive, keep rights, owners, times, links
-vList what is copied
--deleteDelete files in the target that are gone in the source
-n / --dry-runOnly show what would happen
-zCompress during the transfer
--progressShow the progress

تذكّر

The slash matters. rsync -av source dest copies the directory source into dest. rsync -av source/ dest copies only the contents of source.

انتبه

Try --delete with -n first. A wrong source with --delete empties your backup.

18.8

Use rsync over the network

With a host:path name, rsync uses SSH and sends only the changes over the network.

sudo rsync -av --delete --rsh=ssh /etc /home /usr/local remote-sys:/backup
rsync -avz --delete ./site/ deploy@web1:/var/www/site/
rsync -av rsync://mirror.example.org/fedora/ fedora-mirror/
  • --rsh=ssh (or -e ssh) chooses SSH; it is the default today.
  • An rsync:// address talks to an rsync server (often used by mirrors).

في العمل

Put your rsync backup command in a small script and run it every night with a timer or cron. Test a restore sometimes: a backup is only good if you can get the data back.

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

18.9

Summary of the chapter

gzip/bzip2/xz compress, tar archives (with z/j/J), zip for Windows, rsync keeps copies in sync.

1Which command makes a gzip-compressed archive etc.tgz of /etc?
2What is the difference between rsync -a src dst and rsync -a src/ dst?

Without the slash the directory src is copied into dst; with the slash only its contents are copied.

3How do you read a compressed log file without unpacking it?

تدرّب

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