Archiving and compression
Table of contents
- tar: create and extract
- gzip, bzip2, xz
- zip and unzip
- Backups with tar and rsync
- Backup strategies
- Hands-on exercises
1 - tar: create and extract
What is tar?
tar (Tape ARchive) groups several files into one, without compression.
Create an archive
# Archive simple (sans compression)
tar -cvf archive.tar dossier/
# Avec compression gzip (.tar.gz ou .tgz)
tar -czvf archive.tar.gz dossier/
# Avec compression bzip2 (.tar.bz2)
tar -cjvf archive.tar.bz2 dossier/
# Avec compression xz (.tar.xz)
tar -cJvf archive.tar.xz dossier/
Extract an archive
# Extraire dans le répertoire courant
tar -xvf archive.tar
# Extraire un .tar.gz
tar -xzvf archive.tar.gz
# Extraire dans un répertoire spécifique
tar -xzvf archive.tar.gz -C /destination/
# Extraire un seul fichier
tar -xzvf archive.tar.gz chemin/vers/fichier.txt
Main options
| Option | Description |
|---|---|
-c | Create |
-x | eXtract |
-t | lisT (list the content) |
-v | Verbose (display the files) |
-f | File (specify the archive file) |
-z | gzip |
-j | bzip2 |
-J | xz |
-C | Change directory |
--exclude | Exclude files |
List the content
# Sans extraction
tar -tvf archive.tar.gz
# Rechercher un fichier
tar -tvf archive.tar.gz | grep "config"
Exclude files
# Exclure un pattern
tar -czvf archive.tar.gz --exclude='*.log' --exclude='node_modules' dossier/
# Depuis un fichier d'exclusion
tar -czvf archive.tar.gz -X exclude.txt dossier/
🔝 Back to table of contents
2 - gzip, bzip2, xz
Comparison of the algorithms
| Algorithm | Extension | Speed | Compression |
|---|---|---|---|
| gzip | .gz | Fast | Medium |
| bzip2 | .bz2 | Medium | Good |
| xz | .xz | Slow | Excellent |
gzip - The most common
# Compresser (remplace l'original)
gzip fichier.txt
# Crée fichier.txt.gz
# Garder l'original
gzip -k fichier.txt
# Décompresser
gunzip fichier.txt.gz
# ou
gzip -d fichier.txt.gz
# Voir le contenu compressé
zcat fichier.txt.gz
zless fichier.txt.gz
zgrep "pattern" fichier.txt.gz
bzip2 - Better compression
# Compresser
bzip2 fichier.txt
# Crée fichier.txt.bz2
# Décompresser
bunzip2 fichier.txt.bz2
# ou
bzip2 -d fichier.txt.bz2
# Garder l'original
bzip2 -k fichier.txt
xz - Maximum compression
# Compresser
xz fichier.txt
# Crée fichier.txt.xz
# Décompresser
unxz fichier.txt.xz
# ou
xz -d fichier.txt.xz
# Garder l'original
xz -k fichier.txt
# Compression maximale
xz -9 fichier.txt
When to use what?
| Situation | Recommendation |
|---|---|
| Daily use | gzip |
| Long-term archives | xz |
| Speed/size balance | bzip2 |
| Maximum compatibility | gzip |
🔝 Back to table of contents
3 - zip and unzip
zip - Windows/Mac compatible
# Installer si nécessaire
sudo apt install zip unzip
# Créer une archive
zip archive.zip fichier1.txt fichier2.txt
# Archive récursive (dossier)
zip -r archive.zip dossier/
# Avec mot de passe
zip -e archive.zip fichier.txt
# Exclure des fichiers
zip -r archive.zip dossier/ -x "*.log" -x "*.tmp"
unzip - Extract
# Extraire
unzip archive.zip
# Dans un répertoire
unzip archive.zip -d /destination/
# Lister le contenu
unzip -l archive.zip
# Extraire un seul fichier
unzip archive.zip chemin/fichier.txt
# Tester l'intégrité
unzip -t archive.zip
7zip - Universal format
# Installer
sudo apt install p7zip-full
# Créer une archive
7z a archive.7z dossier/
# Extraire
7z x archive.7z
# Lister
7z l archive.7z
🔝 Back to table of contents
4 - Backups with tar and rsync
Backup with tar
#!/bin/bash
# backup.sh
DATE=$(date +%Y%m%d_%H%M%S)
SOURCE="/var/www"
DEST="/backup"
# Créer le backup
tar -czvf "$DEST/www_$DATE.tar.gz" "$SOURCE"
# Rotation : garder 7 jours
find "$DEST" -name "www_*.tar.gz" -mtime +7 -delete
echo "Backup terminé : www_$DATE.tar.gz"
Incremental backup with tar
# Premier backup (complet)
tar -czvf backup_full.tar.gz \
--listed-incremental=/var/backup/snapshot.snar \
/data/
# Backups suivants (incrémentaux)
tar -czvf backup_incr_$(date +%Y%m%d).tar.gz \
--listed-incremental=/var/backup/snapshot.snar \
/data/
rsync - Smart synchronization
# Synchronisation locale
rsync -avh source/ destination/
# Vers un serveur distant
rsync -avz /data/ user@server:/backup/
# Avec suppression des fichiers supprimés
rsync -avz --delete source/ destination/
# Dry run (simulation)
rsync -avzn source/ destination/
# Exclure des fichiers
rsync -avz --exclude='*.log' --exclude='cache/' source/ dest/
# Limiter la bande passante
rsync -avz --bwlimit=5000 source/ dest/ # 5 Mo/s
# Avec progression
rsync -avz --progress source/ destination/
Important rsync options
| Option | Description |
|---|---|
-a | Archive (preserve everything) |
-v | Verbose |
-z | Compression during transfer |
-h | Human readable |
-n | Dry run |
--delete | Delete files absent from the source |
--exclude | Exclude patterns |
--progress | Show progress |
--bwlimit | Limit bandwidth |
🔝 Back to table of contents
5 - Backup strategies
The 3-2-1 rule
| Rule | Example |
|---|---|
| 3 copies | Original + 2 backups |
| 2 media | Local disk + cloud |
| 1 off-site | Remote server, S3 |
Types of backup
| Type | Advantage | Disadvantage |
|---|---|---|
| Full | Simple restore | Slow, large |
| Incremental | Fast, small | Complex restore |
| Differential | Balanced | Larger than incremental |
Complete backup script
#!/bin/bash
# /usr/local/bin/backup_system.sh
# Configuration
BACKUP_DIR="/backup"
DATE=$(date +%Y%m%d)
RETENTION_DAYS=30
# Créer les dossiers
mkdir -p "$BACKUP_DIR"/{daily,weekly,monthly}
# Backup quotidien
tar -czf "$BACKUP_DIR/daily/backup_$DATE.tar.gz" \
--exclude='/backup' \
--exclude='/proc' \
--exclude='/sys' \
--exclude='/tmp' \
--exclude='/var/cache' \
/etc /home /var/www
# Rotation quotidienne (30 jours)
find "$BACKUP_DIR/daily" -name "*.tar.gz" -mtime +$RETENTION_DAYS -delete
# Copie hebdomadaire (dimanche)
if [ "$(date +%u)" -eq 7 ]; then
cp "$BACKUP_DIR/daily/backup_$DATE.tar.gz" "$BACKUP_DIR/weekly/"
find "$BACKUP_DIR/weekly" -name "*.tar.gz" -mtime +90 -delete
fi
# Copie mensuelle (1er du mois)
if [ "$(date +%d)" -eq 01 ]; then
cp "$BACKUP_DIR/daily/backup_$DATE.tar.gz" "$BACKUP_DIR/monthly/"
find "$BACKUP_DIR/monthly" -name "*.tar.gz" -mtime +365 -delete
fi
echo "Backup completed: $DATE"
🔝 Back to table of contents
6 - Hands-on exercises
Exercise 1: Create archives
# 1. Créez un dossier de test
mkdir -p ~/test_archive/{docs,images}
echo "Test 1" > ~/test_archive/docs/doc1.txt
echo "Test 2" > ~/test_archive/docs/doc2.txt
# 2. Créez une archive tar.gz
tar -czvf test_archive.tar.gz ~/test_archive/
# 3. Listez le contenu sans extraire
tar -tvf test_archive.tar.gz
# 4. Extrayez dans un nouveau dossier
mkdir ~/extracted
tar -xzvf test_archive.tar.gz -C ~/extracted/
# 5. Nettoyez
rm -rf ~/test_archive ~/extracted test_archive.tar.gz
Exercise 2: Compare the compressions
# 1. Créez un fichier de test volumineux
yes "Hello World" | head -c 10M > test_file.txt
# 2. Compressez avec différents algorithmes
cp test_file.txt test_gzip.txt && gzip test_gzip.txt
cp test_file.txt test_bzip2.txt && bzip2 test_bzip2.txt
cp test_file.txt test_xz.txt && xz test_xz.txt
# 3. Comparez les tailles
ls -lh test_*.* test_file.txt
# 4. Nettoyez
rm test_*.* test_file.txt
Quiz
Q1. Which tar option compresses with gzip?
Answer
-z → tar -czvf archive.tar.gz dossier/
Q2. How do you extract a tar.gz archive?
Answer
tar -xzvf archive.tar.gz
Q3. Which rsync command simulates without modifying?
Answer
rsync -avzn source/ dest/ (the -n option = dry run)
🔝 Back to table of contents
Key takeaways
- tar -czvf = create, tar -xzvf = extract
- gzip = fast, xz = better compression
- rsync > tar for regular backups
- The
-noption (dry run) to test rsync - The 3-2-1 rule for backups
- Rotation with
find -mtime +N -delete - Always test the restore!