Geavanceerd
Zoeken inSamenvattingen
Zo zoek je
  • Woorden vinden lestitels, tekst en code. Filter op soort (gidsen, cursussen, programma's, boeken) of blijf in dit boek.
  • book:rhcsa en ch:8 maken de zoektocht kleiner; #code vindt een woord alleen in commando's van de lessen.
  • "twee woorden" vindt precies die zin.
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken
Probeer:

Hoofdstuk 18Naam in het boek: 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 Lessen
  • 16 Commando's
  • 1 Labs

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

$ als gewone gebruiker# als root (of met sudo)

Vaardigheden in dit hoofdstuk

  • Comprimeren, archiveren en back-ups maken met tar en 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

Let op

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.

Commando's in deze les

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

Onthoud

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

Let op

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).

Op het werk

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?

Oefenen

Alle commando's in dit hoofdstuk