Intro:
I needed to compress a directory for backup but installing ZIP was not possible. Therefore I used TAR:
How to:
You can do this with tar and a few convenient options:
tar -zcvf backup.tar.gz /var/www
The -zcvf option list breaks down like this:
-z: Gzip the archive
-c: create archive
-v: verbose mode (show you what’s happening)
-f: set the file name
Example
I will make a backupfile (backupjellufin.tar.gz) in the folder /backup/ and copy everything from the source /config/ which is located in the root.
Before, I mounted the folder /backup/ to my NAS. In this way I could share the folder by NFS to the target machine which will contain my new Jellyfin installation.
tar -zcvf /backup/backupjellyfin.tar.gz /config/
Unpack
If you would like to unpack use this:
tar -xvf ../yourtargetfolder/backup.tar.gz -C ../yourdestinationfolder/
Open tar file via command line
Use the following examples to open tar files on the Linux command line.
Extracting the contents of a tar file is very easy, and can be done with the -x (extract option). You’ll also have to include the -f (file) option to indicate to tar that you will specify the location of the file. Use the following syntax to extract the contents of a tar file.
$ tar -xf archive.tar
You can also add the -v (verbose) option to see the extraction progress.
$ tar -xvf archive.tar
Note that you don’t need to add any extra options to extract files from a compressed tar file.
$ tar -xf archive.tar.gz
$ tar -xf archive.tar.bz2
$ tar -xf archive.tar.xz
etc...
To list the contents of a tar file, use the -t (list) option.
$ tar -tf archive.tar
file1.txt
file2.txt
file3.txt
Once you’ve seen what files are contained within the tar archive, you can extract them individually by specifying which files to extract.
$ tar -xf archive.tar file1.txt file2.txt
You can also specify a wildcard in your command with the --wildcards option.
$ tar -xf archive.tar --wildcards '*.txt'
If you want to extract files to some location other than your present working directory, use the -C option and specify the path.
$ tar -xf archive.tar -C /path/to/directory
That should be all you need to know when it comes to extracting tar files via command line. Check out the man page for further examples.
$ man tar
Extra:
If you choose to automate this, you can include the current date in the file name like this:
tar -zcvf backup-$(date '+%Y-%m-%d').tar.gz /var/www