File Backup (tar/gzip)

Rob

Administrator
Staff member
Joined
Oct 27, 2011
Messages
1,210
Reaction score
2,240
Credits
3,485
Using 'tar'

Let's face it, computers aren't perfect. Linux is an "almost perfect" operating system, but things do happen and data is sometimes lost. The best way to avoid problems is to backup your files. Linux provides two key programs to do this: 'tar' and 'gzip'

First we'll start with 'tar'. This program assembles various files into one package, commonly called a "tarball". Let's say you have some files - notes that you've taken during this course.

You have:

Code:
notes_1.txt
notes_2.txt
notes_3.txt
notes_4.txt
notes_5.txt

and you've placed them in a directory called /linux_course. You want to back them up and keep them on a floppy, let's say. You would type the following command to package them in a tarball.

Code:
tar -cvf linux_notes.tar notes*.txt

First, you have tar, the name of the program. Then you have the options, c (--create) v (--verbose-show what files they are) (f--file -make a file - should always be the last option) Then you have the name of the file you want to create ( linux_notes.tar) and the files you want to backup (notes*.txt).

This presupposes that you may have other files in the directory that you don't want to include. If you want to include ALL files in a directory, just substitute notes*.txt for *.*.

If you've got good data storage capabilities (Jaz or Zip drives, a CD writer or a tape backup drive), you might want to back up whole directories along with their corresponding subdirectories. Then you would enter in the directory, let's say /home/bob/ and issue the command:

Code:
tar -cvf bob_backup.tar *

With one asterisk, you will include directories and files without extensions (my_file as opposed to my_file.txt). Be prepared to get a fairly voluminous tarball.

This is the first step in the backup process. Now let's look at the second step; the compression of these files.

Using 'gzip'

As we mentioned, 'tar' just assembles the files together into only one file. There is no reduction in the size of these files (the tarball might even be bigger!) Now we would have to do one more thing in order to reduce this file into a more manageable size: use 'gzip'.

gzip is the preferred compression tool for Linux. To reduce the size of your tar file, you would issue the following command:

Code:
gzip your_tar_file.tar

and the tar file would be compressed. You can also compress a regular file using the same command, but gzip is used primarily with tarballs.

The result would be a file like this: your_tar_file.tar.gz

The two file extensions show us that the file is a tarball and it is compressed with the 'gzip' format. You can now proceed to store this as you see fit.

Putting it all together

tar

tar has an option built into it to use 'gzip' to zip the file at the same time you make the tarball. If you add z to the options, and change the name of the file to create to a .gz extension, you have the whole shebang in one step. Our previous example would be modified to this:

Code:
tar -czvf bob_backup.tar.gz *

Remember f should always be the last option.

UnTar

Using 'tar' and 'gzip' sort of supposes that you're going to want to "untar" and "unzip" these files at one point or another.

The easiest way for doing this is to use 'tar' for the whole process. You would locate the zipped tarball in question and then ask yourself a question:

Did I make any changes to the files inside the tarball after I made it? If you did, then you've got an old tarball. If you untarred it in the same directory, you'd overwrite the existing ones. If you would like a copy of the old file, untar it in a different directory. If you don't want the old files, then you should make a new tarball. It's pretty standard backup practice.

When you've decided what you want to do, to proceed with the "untarring" issue this command:

Code:
tar -zxvpf my_tar_file.tar.gz

I've used my preferred options. I'll explain them:

-z - unzip the file first
-x - extract the files from the tarball
-v - \"verbose\" (i.e tar tells you what files it's extracting)
-p - preserves dates, permissions of the original files
-f - use the file in question (if you don't specify this, tar just sort of sits around doing nothing)

The files are extracted and your original tarball is preserved (my_tar_file.tar.gz).

You can also untar the file and then use gzip separately. Just leave the z option out of the previous example and type:

Code:
gzip -d my_tar_file.tar.gz
or
Code:
gunzip my_tar_file.tar.gz

(gunzip runs gzip -d "automagically"!)

These commands are good if you've just zipped a regular file (not a tarball).

Other compression tools

zip

Most Linux distributions come with other tools to compress files. One of these is zip, famous in the MS-DOS/Windows world. If you're planning on compressing files to give to someone who (still) uses the Windows operating system, this might be your best bet. You can also use unzip if someone gives you a file compressed with 'zip'. Consult the man file ( man zip) for specific instructions on using this tool.

bzip2

There is also another tool that is rapidly gaining acceptance in the Linux world: bzip2. As a matter of fact, the Linux kernel source package, usually comes "bzipped". When you compile a kernel (create a custom kernel for yourself from source) there is an option to create a bzipped kernel. This is supposed to become the official way of doing it in the near future, so it may be a good idea to get to know 'bzip2'

For all practical purposes you would use this tool in the same way as you would 'gzip'. The compression factor is supposed to be a little better. There are some differences in options for more advanced users. Consult man bzip2for more information.
 
Last edited:


Another informative read for this newbie.
In UNTAR, line 3 it is Did I make any changes to the files inside the tarball after I made it?
I suspect that what you are asking is 'Did I make any changes to the files after I made the tarball from them? On first reading I thought you were reaching inside the tarball, which seemed hard.
 
Using 'tar'

Let's face it, computers aren't perfect. Linux is an "almost perfect" operating system, but things do happen and data is sometimes lost. The best way to avoid problems is to backup your files. Linux provides two key programs to do this: 'tar' and 'gzip'

First we'll start with 'tar'. This program assembles various files into one package, commonly called a "tarball". Let's say you have some files - notes that you've taken during this course.

You have:

Code:
notes_1.txt
notes_2.txt
notes_3.txt
notes_4.txt
notes_5.txt

and you've placed them in a directory called /linux_course. You want to back them up and keep them on a floppy, let's say. You would type the following command to package them in a tarball.

Code:
tar -cvf linux_notes.tar notes*.txt

First, you have tar, the name of the program. Then you have the options, c (--create) v (--verbose-show what files they are) (f--file -make a file - should always be the last option) Then you have the name of the file you want to create ( linux_notes.tar) and the files you want to backup (notes*.txt).

This presupposes that you may have other files in the directory that you don't want to include. If you want to include ALL files in a directory, just substitute notes*.txt for *.*.

If you've got good data storage capabilities (Jaz or Zip drives, a CD writer or a tape backup drive), you might want to back up whole directories along with their corresponding subdirectories. Then you would enter in the directory, let's say /home/bob/ and issue the command:

Code:
tar -cvf bob_backup.tar *

With one asterisk, you will include directories and files without extensions (my_file as opposed to my_file.txt). Be prepared to get a fairly voluminous tarball.

This is the first step in the backup process. Now let's look at the second step; the compression of these files.

Using 'gzip'

As we mentioned, 'tar' just assembles the files together into only one file. There is no reduction in the size of these files (the tarball might even be bigger!) Now we would have to do one more thing in order to reduce this file into a more manageable size: use 'gzip'.

gzip is the preferred compression tool for Linux. To reduce the size of your tar file, you would issue the following command:

Code:
gzip your_tar_file.tar

and the tar file would be compressed. You can also compress a regular file using the same command, but gzip is used primarily with tarballs.

The result would be a file like this: your_tar_file.tar.gz

The two file extensions show us that the file is a tarball and it is compressed with the 'gzip' format. You can now proceed to store this as you see fit.

Putting it all together

tar

tar has an option built into it to use 'gzip' to zip the file at the same time you make the tarball. If you add z to the options, and change the name of the file to create to a .gz extension, you have the whole shebang in one step. Our previous example would be modified to this:

Code:
tar -czvf bob_backup.tar.gz *

Remember f should always be the last option.

UnTar

Using 'tar' and 'gzip' sort of supposes that you're going to want to \"untar\" and \"unzip\" these files at one point or another.

The easiest way for doing this is to use 'tar' for the whole process. You would locate the zipped tarball in question and then ask yourself a question:

Did I make any changes to the files inside the tarball after I made it? If you did, then you've got an old tarball. If you untarred it in the same directory, you'd overwrite the existing ones. If you would like a copy of the old file, untar it in a different directory. If you don't want the old files, then you should make a new tarball. It's pretty standard backup practice.

When you've decided what you want to do, to proceed with the \"untarring\", issue this command:

Code:
tar -zxvpf my_tar_file.tar.gz

I've used my preferred options. I'll explain them:

-z - unzip the file first
-x - extract the files from the tarball
-v - \"verbose\" (i.e tar tells you what files it's extracting)
-p - preserves dates, permissions of the original files
-f - use the file in question (if you don't specify this, tar just sort of sits around doing nothing)

The files are extracted and your original tarball is preserved (my_tar_file.tar.gz).

You can also untar the file and then use gzip separately. Just leave the z option out of the previous example and type:

Code:
gzip -d my_tar_file.tar.gz
or
Code:
gunzip my_tar_file.tar.gz

(gunzip runs gzip -d "automagically"!)

These commands are good if you've just zipped a regular file (not a tarball).

Other compression tools

zip

Most Linux distributions come with other tools to compress files. One of these is zip, famous in the MS-DOS/Windows world. If you're planning on compressing files to give to someone who (still) uses the Windows operating system, this might be your best bet. You can also use unzip if someone gives you a file compressed with 'zip'. Consult the man file ( man zip) for specific instructions on using this tool.

bzip2

There is also another tool that is rapidly gaining acceptance in the Linux world: bzip2. As a matter of fact, the Linux kernel source package, usually comes \"bzipped\". When you compile a kernel (create a custom kernel for yourself from source) there is an option to create a bzipped kernel. This is supposed to become the official way of doing it in the near future, so it may be a good idea to get to know 'bzip2'

For all practical purposes you would use this tool in the same way as you would 'gzip'. The compression factor is supposed to be a little better. There are some differences in options for more advanced users. Consult man bzip2for more information.

Even being the "newbie" I am, if you don't want to have to do the work over........ backup and backup often!! Thanx for the instruction.
 
Ah! Now I have the information to untar and unzip a file.
Just one query though so I know what to expect when I carry out the operation.
When you untar and unzip the file where does it place the file?
Is it along side the tar.gz in the folder or elsewhere?
Or does it ask you where you want it?
 
Depends where you are in the terminal.
For example if the compressed file is example.tar.gz, placed in ~/Documents and you open a terminal there, it will be extracted there.
~/Documents$ tar -zxvpf example.tar.gz
example1.txt
example2.txt
But if you are in home directory, then
~$ tar -zxvpf ~/Documents/example.tar.gz
example1.txt
example2.txt
the files will be extracted in ~
in second case you need to call the directory inside the comand, together with the name of the file
By the way, ~ and /home/myuser is the same thing, is just a short argument
 
Last edited:
Using 'tar'

Let's face it, computers aren't perfect. Linux is an "almost perfect" operating system, but things do happen and data is sometimes lost. The best way to avoid problems is to backup your files. Linux provides two key programs to do this: 'tar' and 'gzip'

First we'll start with 'tar'. This program assembles various files into one package, commonly called a "tarball". Let's say you have some files - notes that you've taken during this course.

You have:

Code:
notes_1.txt
notes_2.txt
notes_3.txt
notes_4.txt
notes_5.txt

and you've placed them in a directory called /linux_course. You want to back them up and keep them on a floppy, let's say. You would type the following command to package them in a tarball.

Code:
tar -cvf linux_notes.tar notes*.txt

First, you have tar, the name of the program. Then you have the options, c (--create) v (--verbose-show what files they are) (f--file -make a file - should always be the last option) Then you have the name of the file you want to create ( linux_notes.tar) and the files you want to backup (notes*.txt).

This presupposes that you may have other files in the directory that you don't want to include. If you want to include ALL files in a directory, just substitute notes*.txt for *.*.

If you've got good data storage capabilities (Jaz or Zip drives, a CD writer or a tape backup drive), you might want to back up whole directories along with their corresponding subdirectories. Then you would enter in the directory, let's say /home/bob/ and issue the command:

Code:
tar -cvf bob_backup.tar *

With one asterisk, you will include directories and files without extensions (my_file as opposed to my_file.txt). Be prepared to get a fairly voluminous tarball.

This is the first step in the backup process. Now let's look at the second step; the compression of these files.

Using 'gzip'

As we mentioned, 'tar' just assembles the files together into only one file. There is no reduction in the size of these files (the tarball might even be bigger!) Now we would have to do one more thing in order to reduce this file into a more manageable size: use 'gzip'.

gzip is the preferred compression tool for Linux. To reduce the size of your tar file, you would issue the following command:

Code:
gzip your_tar_file.tar

and the tar file would be compressed. You can also compress a regular file using the same command, but gzip is used primarily with tarballs.

The result would be a file like this: your_tar_file.tar.gz

The two file extensions show us that the file is a tarball and it is compressed with the 'gzip' format. You can now proceed to store this as you see fit.

Putting it all together

tar

tar has an option built into it to use 'gzip' to zip the file at the same time you make the tarball. If you add z to the options, and change the name of the file to create to a .gz extension, you have the whole shebang in one step. Our previous example would be modified to this:

Code:
tar -czvf bob_backup.tar.gz *

Remember f should always be the last option.

UnTar

Using 'tar' and 'gzip' sort of supposes that you're going to want to "untar" and "unzip" these files at one point or another.

The easiest way for doing this is to use 'tar' for the whole process. You would locate the zipped tarball in question and then ask yourself a question:

Did I make any changes to the files inside the tarball after I made it? If you did, then you've got an old tarball. If you untarred it in the same directory, you'd overwrite the existing ones. If you would like a copy of the old file, untar it in a different directory. If you don't want the old files, then you should make a new tarball. It's pretty standard backup practice.

When you've decided what you want to do, to proceed with the "untarring" issue this command:

Code:
tar -zxvpf my_tar_file.tar.gz

I've used my preferred options. I'll explain them:

-z - unzip the file first
-x - extract the files from the tarball
-v - \"verbose\" (i.e tar tells you what files it's extracting)
-p - preserves dates, permissions of the original files
-f - use the file in question (if you don't specify this, tar just sort of sits around doing nothing)

The files are extracted and your original tarball is preserved (my_tar_file.tar.gz).

You can also untar the file and then use gzip separately. Just leave the z option out of the previous example and type:

Code:
gzip -d my_tar_file.tar.gz
or
Code:
gunzip my_tar_file.tar.gz

(gunzip runs gzip -d "automagically"!)

These commands are good if you've just zipped a regular file (not a tarball).

Other compression tools

zip

Most Linux distributions come with other tools to compress files. One of these is zip, famous in the MS-DOS/Windows world. If you're planning on compressing files to give to someone who (still) uses the Windows operating system, this might be your best bet. You can also use unzip if someone gives you a file compressed with 'zip'. Consult the man file ( man zip) for specific instructions on using this tool.

bzip2

There is also another tool that is rapidly gaining acceptance in the Linux world: bzip2. As a matter of fact, the Linux kernel source package, usually comes "bzipped". When you compile a kernel (create a custom kernel for yourself from source) there is an option to create a bzipped kernel. This is supposed to become the official way of doing it in the near future, so it may be a good idea to get to know 'bzip2'

For all practical purposes you would use this tool in the same way as you would 'gzip'. The compression factor is supposed to be a little better. There are some differences in options for more advanced users. Consult man bzip2for more information.
Excellent stuff Rob, refreshed my memory to. Thank you for your time and effort. Davi
 
  • Like
Reactions: Rob
Am I correct that gzip cannot compress entire directories/sub-dirs, which is why tar is needed?
 
You want to back them up and keep them on a floppy,
Really ?....on a floppy ?....just a touch outdated, perhaps?
 
Well, the thread is a decade old...

Though I can't imagine using a floppy in 2013 either.

Heck, they were falling out of use in 2003.
 
I've never used a floppy...not that it matters now.
m1213.gif

As for tar.gz...I just do this...
2023-08-27-16-28.png


Don't know if this could be done back then...of cause backing up is very different now.
m1204.gif
 

Members online


Latest posts

Top