Creating Backups - Part I
Ø Archiving Tools:
n Linux offers many native tools that can be utilized to archive files for storage or distribution:
n These tools include: “tar” and “star”, both of which have the ability to preserve general file attributes, such as ownership, group membership, and timestamp.
Ø Occasionally, you might want to make a backup of important files on your computer:
· The "tar" (tape archive) command creates, appends, updates, lists, and extracts files to and from a single file, which is called a “tar” file (also a tarball) (Ref: RH Linux 7 Book-pg40)
· This command has the ability to archive SELinux file contexts as well as any extended attributes set on files
· Can also be instructed to compress an archive while it’s being accessed
n “tar” options: (RH Linux – pg40)
--- -c [creates a tarball]
--- -f [specifies a tarball name]
--- -j [compresses a tarball with bzip2 command]
--- -r [appends files to the end of an existing tarball. Does not append to compressed tarballs]
--- -t [Lists contents of a tarball]
--- -u [appends files to the end of an existing tarball if the specified files are newer. Does not append to compressed tarballs]
--- -v [verbose mode]
--- -x [Extracts from a tarball]
--- -z [compresses a tarball with gzip command]
--- Verbose mode is an option available in many computer operating systems, including Microsoft Windows, Mac OS and Linux that provides additional details as to what the computer is doing and what drivers and software it is loading during startup.
· "tar" utility gets its name from "Tape Archive"... This tool will receive and send files to a destination(typically a tape or a regular file)...
· "tar" command has many arguments:
¨ 3 Major tasks are involved in using "tar":
1. Creating an archive
2. Verifying the contents of an archive
3. Extracting an archive
Note: The archive can be written to multiple destinations, but the most common procedure is to write it to a file.
Ø While using "tar", use the "f" option to name/create the tarfile
§ The "c" option is used to create a tar archive
§ The "v" option is "Verbose" mode:
· Verbose mode is an option available in many computer operating systems, including Microsoft Windows, Mac OS and Linux that provides additional details as to what the computer is doing and what drivers and software it is loading during startup. This level of detail can be very helpful for troubleshooting problems with hardware or software, if errors are occurring during startup or after the operating system has loaded.
§ For example: To create an archive of all configuration files in the "/etc" directory, use command:
tar cvf /tmp/etc.tar /etc
OR
tar cf /backup/etc.tar /etc
ls -l /backup/etc.tar
· Note: options are not proceeded by a -(minus) in this command (which is common behavior in tar)
· The order of the options is specific. . ."tar fvc" wouldn't work
· notice that you specify the location where to write the archive before specifying what to put into the archive
Ø Next, once you have created an archive file using the tar command, you can verify it's content (list contents of a tar file)..... How?
§ By using the "t" option... "c"(create) option is replaced...
--- so, tar tvf /tmp/etc.tar yields the content of the previously created archive
Ø Finally, the 3rd task to accomplish with "tar" is the extraction of an archive... in this process, you get the files out of the archive and write them to the file system of your computer:
§ Example: extract to another directory:
tar xvf /tmp/etc.tar -C ~/tmp/etc_bkup/
v Compression: (65.2 - LinuxAdmin, pg: 677)
Ø It can be beneficial to compress files before backup
Ø 2 most popular tools for compression of regular files on Linux are:
§ gzip/gunzip (generally much faster)
§ bzip2/bunzip2 (compresses a lot better)
Ø compression can be achieved without pipes since tar uses the "z" flag to compress with gzip, and the "j" flag to compress with bzip2
¨ tar czf /backup/etc.tar.gz /etc (gzip example)
¨ tar cjf /backup/etc.tar.bz2 /etc (bzip2 example)
to see results: ls -l /backup/etc.ta*
Other Examples:
v To list a specific file in a tar archive:
¨ tar tvf /backup/etc.tar etc/resolv.conf
v To preserve file permissions, use the "p" flag:
¨ Example: tar cpzf /backup/etc_with_perms.tgz /etc
v To exclude directories or files, use "exclude" within command:
-- Example: tar cpzf /backup/etc_no_sysconf.tgz /etc --exclude='syscon*'
v "tar" can be used to copy a directory (more efficient than using "cp -r") Example: (cd /etc; tar -cf - .) | (cd /backup/copy_of_etc/ ; tar -xpf - )
v "tar" used to copy a directory securely over the network:
Ø Example: (cd /etc;tar -cf - . )|(ssh user@srv 'cd /backup/cp_of_etc/; tar -xf - ')
v Compress the tar backup when it is on the network, but leave it uncompressed at the destination:
Ø Example: cat backup.tar | gzip | ssh "gunzip|cat -> backup.tar"
v To create a tarball called /tmp/home.tar of the entire /home directory tree: tar cvf /tmp/home.tar /home
v To create a tarball called /tmp/files.tar containing multiple files from the /etc directory: tar cvf /tmp/files.tar /etc/host.conf /etc/shadow /etc/passwd /etc/yum.conf
v To append files located in the /etc/yum.repos.d directory to home.tar: tar rvf/tmp/home.tar /etc/yum.repos.d
v To list the contents of home.tar: tar tvf /tmp/home.tar
v To list the contents of files.tar: tar tvf /tmp/files.tar