6.5 Archives and Compression: tar, gzip, zip
tar -czvf site-backup.tar.gz /var/www/html # create gzip archive (c=create z=gzip v=verbose f=file)
tar -tzvf site-backup.tar.gz # list contents
mkdir -p /tmp/restore
tar -xzvf site-backup.tar.gz -C /tmp/restore # extract into a folder
tar -cJvf logs.tar.xz /var/log/nginx # xz compression (smaller, slower)
gzip big.log # -> big.log.gz gunzip big.log.gz
zip -r site.zip mysite/ # install package 'zip' if missing
unzip site.zip -d /var/www/html/ # install package 'unzip' if missing
Why this matters for security
Backups are your last defence against ransomware and mistakes – but a site-backup.tar.gz left inside the web root can be downloaded by anyone who guesses the name. Directory brute-forcing tools (Gobuster, Part 10) look for exactly such files.
Ravindra Bagale's Tip
Before running tar -xzvf, many students don't check what's inside with -tzvf, and files get scattered in the wrong place. List the contents first, then extract with -C and a target folder. And never keep backups in the web root.
Ravindra Bagale's Tip – मराठी
tar -xzvf करण्यापूर्वी बरेच students -tzvf ने आत काय आहे ते बघत नाहीत आणि files चुकीच्या जागी पसरतात. आधी list करा, मग -C ने folder देऊन extract करा. आणि backup कधी web root मध्ये ठेवू नका.
Ravindra Bagale's Tip – हिंदी
tar -xzvf करने से पहले बहुत से students -tzvf से नहीं देखते कि अंदर क्या है, और files गलत जगह फैल जाती हैं. पहले list करो, फिर -C से folder देकर extract करो. और backup कभी web root में मत रखो.
Practice task
Archive ~/labs into labs.tar.gz, list its contents, and extract it into /tmp/restore.