Even though private users hardly use it today, the program continues to be the most popular tool for archiving on Unix systems.
Regular, incremental backups of a server can be created with the packing program. Here we explain how tar functions and which commands are used to run backups. Tar is a program for archiving on Linux and related systems. But the program is very popular because it offers the great advantage that entire directories can be merged into one file.
This explains the sequential, linear storage of the tar format. New files are attached to the back of the archive. However, to achieve successful compression , tar is often used in combination with gzip.
The two programs complement each other perfectly: gzip can only compress individual files. So, in the end, either. With Ubuntu, tar should already be preinstalled. If you use another Linux or Unix distribution, install the helpful program with:. When creating tar archives, you also have the option to create wildcards with an asterisk.
If you create a new archive, always indicate the options first, then the file names of the archive that you want to create, and finally the files and folders that it should contain. In the following example, create an archive -c from two text files, compress it with gzip -z , and write it to the file archive.
If you want to combine all text files in a directory into an archive, use a corresponding wildcard:. You can also combine complete directories and their subdirectories into an archive. In the following example, you extract -x the compressed -z archive that we created in the first example into another directory -C :. It makes sense for your security strategy if you create a backup script for the archiving of your system instead of simply doing the archiving by hand.
This way, you can automatically archive multiple directories , compress them, or transfer them to an external storage device. In our example, we name this backup , followed by the current time stamp. The file name is completed with the specification of the format in which the file is created. If you want to use a different compression, remember to change both the file format as well the option in the command. The systems read out the type of file by comparing the file structure with a magic file.
In any case, it has become common to specify file extensions so that you as a user can keep track of everything more easily. Now save the file with the name backup in the bin directory, and add this path to the PATH variable:. This makes the file executable only for you u. You can also assign permissions to a group g , to others o , or to all a. You are now finished and can run the script:.
If you would like to produce the backup again to extract the archive, then you can do so with this command:. The script creates a full backup. Therefore, you should consider whether an incremental backup with tar makes more sense for your purposes.
This reduces the chance of accidentally overwriting your entire system. If you really want to replace existing content, you must first navigate to the root directory. But you can get around this using the - P option. Webmasters create regular backups to avoid data loss. Should the actual system be denied, compromised, or deleted, you can install a working version from the backup. Instead, you can create incremental backups.
An incremental backup always requires a full backup. You have to first archive the entire system once or at least the part that you want to back up. Afterward, only new or modified files are saved with an incremental backup. This results in a much smaller amount of data, but requires more effort when it comes to recovery. If a file is lost which is less likely nowadays than it was when magnet tapes were used , the backup will be incomplete. With tar, you can create regular incremental backups.
You can also write your own backup script. For example, you can specify that a full backup is to be created once a month and then an incremental backup is performed daily. The following script also makes sure that old backups are regularly moved into folders sorted by date. In addition to tar, you also need cron.
This daemon a program that runs in the background allows for time-based execution of other processes, and is always included with Ubuntu.
First, open another text editor and create this script:. With daily archiving, the script moves backup files into a new archive folder each month, so that the actual backup directory only contains the current data. There is no built-in function, though, limiting the number of archive folders. That means that these must be manually deleted. This completes the script for the creation of an incremental backup with tar: Save the file as backup in the bin directory.
You also need to export the path here and make the script executable:. You can do so by appending an --exclude switch for each directory or file you want to exclude.
The --exclude switch is very powerful. For example, you could archive an entire directory and exclude all. While gzip compression is most frequently used to create. This allows you to create bzip2-compressed files, often named. To do so, just replace the -z for gzip in the commands here with a -j for bzip2.
Gzip is faster, but it generally compresses a bit less, so you get a somewhat larger file. Bzip2 is slower, but it compresses a bit more, so you get a somewhat smaller file. Gzip is also more common, with some stripped-down Linux systems including gzip support by default, but not bzip2 support. In general, though, gzip and bzip2 are practically the same thing and both will work similarly.
Append files to an existing archive using the -r tag. The syntax is:. Already compressed archives cannot be updated, so the syntax only works for tarball files. Use the --concatenate or -A option to combine multiple archives. The basic syntax is:. As an example, copy the existing files. To check the difference between an archive and files on disk, use the -d tag:. The command searches for the same contents and compares them to what is in the archive.
The option only checks for existing files and ignores any newly added files. The steps below show how to use the -d , --diff , or --compare tag with tar :. This time, the output shows differences in the modification time and the size for a specific file. Comparing provides insight into any changes made on the system after creating the archive.
Update the existing files in the archive with a newer version from disk with the -u option:. Tar offers various options to modify the file's timestamp. Set a custom date when creating an archive by adding the --mtime option and providing a date:. A useful feature when working with time is filtering files modified after a specific date. For example, to extract files created after a date, use the --newer-mtime option and add the date:.
Modify the permissions to the default umask value:. Tar allows file ownership configuration. For example, to set the file owner when creating an archive, add the --owner and --group options and provide values for each:. To find these values for a user, run:. Tar allows preserving the ownership when extracting from an archive. To do so, add the --same-owner option at the end:. The --to-command option instructs tar to send each extracted file to the standard output for an external program.
The command creates directories named after each extracted file. For more information and the available Linux environment variables , visit the manual page.
The tar command creates a compressed archive, while the find command seeks backup files older than one day. After reading this article, you know how to use the tar command. However, there are various other options available that are not in this tutorial.
Use the man command to find all the details about tar options. Create an Archive 2. Remove Files After Creation 3. Extract from Archive 4. Overwrite Control 5. List Archive Contents 6. Find a File in an Archive 7. Find Multiple Files in an Archive 8. Exclude Files when Creating Archive 9.
0コメント