هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.
$ مستخدم عادي# بصلاحية root (أو مع sudo)
المهارات في هذا الفصل
- الضغط والأرشفة والنسخ الاحتياطي بـ tar و rsync
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| Option | What it does |
|---|---|
-c | Write to stdout, keep the original |
-d | Decompress (like gunzip) |
-l | Show the compression ratio |
-r | Compress all files in a directory tree |
-t | Test the compressed file |
-v | Show messages |
-1 … -9 | Speed against size: -1 fastest, -9 smallest (default 6) |
Use bzip2 and xz
bzip2 and xz compress better than gzip, but are slower. They have almost the same options.
Make archives with tar
tar ("tape archive") packs many files and directories into one file, a tarball. It keeps names, owners, rights and times.
| Mode | Meaning |
|---|---|
c | Create an archive |
x | Extract an archive |
r | Add files to the end of an archive |
t | List 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 filenames the archive file;vlists 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.
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 -- Modern GNU tar finds the compression itself when it extracts:
tar xf file.tar.xzworks. -T -reads the list of files from stdin, sofindcan choose which files go in.-C dirextracts into another directory:tar xzf backup.tgz -C /restore.
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.
Use zip for Windows users
zip archives and compresses in one step, in the format Windows and macOS open directly.
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| Option | What it does |
|---|---|
-a | Archive mode: recursive, keep rights, owners, times, links |
-v | List what is copied |
--delete | Delete files in the target that are gone in the source |
-n / --dry-run | Only show what would happen |
-z | Compress during the transfer |
--progress | Show 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.
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.
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.