john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

tar compress extract gpg encrypt a file decompress bzip2 zip unzip

[TOC]

gnu tar creates volumes from a group of files (or a directory), it can also use gzip or bzip to compress the resulting archive

https://www.gnu.org/software/tar/manual/tar.html

Tar basics: extract, compress

tar -xvvf myfile.tar

-x means extract from tar file

tar --list --file=collection.tar

shows you what's in your tar

tar --exclude='/tmp/foo' --exclude='/tmp/bar' -czvf archive.tar.gz /tmp
  • exclude the /tmp/foo directory and exclude the /tmp/bar directory
  • flags (does require the dash): to Create with gZip compression Verbose output into a File
  • ouptut file will be archive.tar.gz
  • source is directory /tmp, could be space delimited file(s) or a directories

the - is optional, and sometimes people use the shorter archive.tgz

tar txvf archive.tgz

list the archives contents (uncompress first?)

tar xzf archive.tar.gz

unzip and extract the archive to the current directory

tar xvzf archive.tgz -C /destination  > restore.log

restore to a target directory and redirect output to logfile

Bzip2 basics

sudo apt-get install bzip2

or dpkg -i libz... bzip2

tar -xjf broadcom-wl-4.8...tar.bz2

tar jxf /usr/src/linux-source-2.6.18.tar.bz2

tar -xzvf filename.tar.gz
tar xf filename.tgz

bzip2 -d filename

GPG Basics Encryption and Decryption

gpg -c archive.tar.gz   # gpg encrypts a file

Prompted: [enter a password]

scp arcjove.tar.gz.gpg user@192.168.0.2:/home/user

login to the new system: gpg archive.tar.gz.gpg Prompted: [enter the password to decrypt]

tar -xzvf archive.tar.gz
> decompress after decrypting

NON INTERACTIVE GPG ENCRYPT

gpg --yes --passphrase=password -c sourcefile.txt

echo "password" | gpg --yes --no-tty --batch --passphrase-fd 0 --output plain.txt.gpg  --symmetric --cipher-algo AES256 plain.txt
> AES 256 symmetric cipher

NON INTERACTIVE DECRYPT

gpg --yes --passphrase=password sourcefile.txt.gpg

NOTE IT NEEDS THE FILE TO END WITH .gpg TO AUTO DECRYPT

echo "password" | gpg --yes --no-tty --batch --passphrase-fd 0 --output decrypted.plain.txt --decrypt plain.txt.gpg

FULL SYSTEM BACKUP, GNU Tar to backup file permissions, datestamps, etc

tar --atime-preserve=system --dereference -cpvzf  `/bin/date +%Y-%m-%d--%H-%M`.tgz /projects > `/bin/date +%Y-%m-%d--%H-%M`

-c --create -p --preserve-permissions -v --verbose -z --gzip -f --file -h --dereference //get the files the symbolic link points to --atime-preserve=system

you must be in a "superior" directory to tar a sub directory (e.g. you must be at / to tar /example, you cannot be in /example)

mv 2008-10-27--13-00 /home/su/Desktop

you must restore a file from within the folder where you want the extracted files to appear (e.g. cd /testrestore, then tar -x)

--delay-directory-restore       //try to ensure directory timestamp occurs after file extraction
-x                              //extract

tar --atime-preserve=system --delay-directory-restore --dereference -xpzf  filename

Alternatives to do a full backup (the ‘\’ means continued on next line without CarriageReturn) apt-get clean //first remove unnecessary?

tar -cvpzf /mnt/linux-backups/22may09Backup.tgz --exclude=/cdrom --exclude=/dev --exclude=/lost+found --exclude=/mnt --exclude=/opt --exclude=/proc --exclude=/sys --exclude=/tmp / > /mnt/linux-backups/22may09backup-log.txt

'tar' is the program used to do a backup
c - create a new backup archive
v - verbose mode, tar will print what it's doing to the screen
p - permission
z - compress the backup file with 'gzip' to make it smaller
f <filename>

--exclude directories (special system directories that won’t backup well)

NOTE: the last ‘/’ is not a mistake, we have to give it a starting directory, in this case ROOT

filename (outputs the screentext to a text file so that it can be reviewed as a log)

2.2 GB on a quad core 3GB ram becomes 1.5 GB in 4 minutes, how could this be better done with differential?

RESTORE

first you can use partimage to restore a full hard disk or you can install a basic installation of linux from an install cd

either way remember that the Kernel can be installed hardware dependent (so moving from AMD to intel can cause problems)

Ubuntu "rescue mode" worked well for fixing an ubuntu installation (and re-installing GRUB)

Restoring is as easy as:

tar xvpzf backup.tgz -C / > restore-log.txt

Once the extraction is complete, re-create the directories which were excluded.

mkdir /proc /lost+found /mnt /sys

CONFLICTS

A “test machine” clone restore should avoid conflicts on the network:

hostname            //use the hostname command
nano /etc/hosts         //change from the old to new for 127.0.0.1

/etc/network            (static ip address?)

linux-zip-and-unzip

apt-get install zip unzip
zip -r directoryname.zip directoryname
unzip directoryname.zip

-r = recursive -k = DOS compatible file/dir names

Copyright (c) 1990-2006 Info-ZIP - Type 'zip "-L"' for software license.
Zip 2.32 (June 19th 2006). Usage:
zip [-options] [-b path] [-t mmddyyyy] [-n suffixes] [zipfile list] [-xi list]
  The default action is to add or replace zipfile entries from list, which
  can include the special name - to compress standard input.
  If zipfile and list are omitted, zip compresses stdin to stdout.
  -f   freshen: only changed files  -u   update: only changed or new files
  -d   delete entries in zipfile    -m   move into zipfile (delete files)
  -r   recurse into directories     -j   junk (don't record) directory names
  -0   store only                   -l   convert LF to CR LF (-ll CR LF to LF)
  -1   compress faster              -9   compress better
  -q   quiet operation              -v   verbose operation/print version info
  -c   add one-line comments        -z   add zipfile comment
  -@   read names from stdin        -o   make zipfile as old as latest entry
  -x   exclude the following names  -i   include only the following names
  -F   fix zipfile (-FF try harder) -D   do not add directory entries
  -A   adjust self-extracting exe   -J   junk zipfile prefix (unzipsfx)
  -T   test zipfile integrity       -X   eXclude eXtra file attributes
  -y   store symbolic links as the link instead of the referenced file
  -R   PKZIP recursion (see manual)
  -e   encrypt                      -n   don't compress these suffixes

  • « text to html txt2htm
  • linkedlist v1 »

Published

Oct 27, 2008

Category

linux

~860 words

Tags

  • bzip2 1
  • compress 1
  • compression 1
  • decompress 1
  • encrypt 4
  • extract 6
  • gpg 1
  • linux 249
  • tar 2
  • unzip 1
  • zip 1