- Use GNU Tar to Backup Linux
- Basic Syntax
- Enhancements
- Exclude files that you won’t want (or need) to backup.
- Add current date to the name of backup
- Use a better compression program other than GZip
- Putting everything in a shell script
- Want to contribute?
- Linux tar command
- Description
- Syntax
- Operation
- Functions
- Other options
- Environment
- Examples
- Related commands
Use GNU Tar to Backup Linux
Now that you have a perfectly configured Linux server on Vultr, you need to choose a backup solution for it. GNU tar is a very good choice — it’s reliable and makes it easy to backup and restore a server.
Basic Syntax
Tar uses the following syntax.
sudo /usr/bin/tar -czpvf /home/john/backup/linux_backup.tar.gz /
- -c : Archive.
- -z : Use GZip format to backup. GZip is fast but it generates a larger file size than other compression tools.
- -p : Preserve permission so that when you restore the backup you will not encounter a permission problem.
- -v : Show details during backup. Omit -v if you don’t want to see verbose output.
- -f : Specify where to store the tar files. Here we save the backup file to backup directory under user John’s home directory and name it linux_backup.tar.gz .
- / : The Linux root file system. This means to backup the whole disk including all mounted drives, so be careful, make sure to exclude folders or files that you don’t want to backup with excluded.txt (explained below).
Enhancements
Exclude files that you won’t want (or need) to backup.
sudo /usr/bin/tar --exclude-from=/home/john/exclude.txt -czpvf /home/john/backup/linux_backup.tar.gz /
Use —exclude-from=/home/john/exclude.txt to specify excludes in a file. An exclude.txt sample is pasted below.
/home/john/backup/* /tmp/* /proc/* /dev/* /sys/* /run/* /var/tmp/* /var/run/* /var/lock/* /usr/portage/* /usr/src/*
In this example, I exclude /home/john/backup/* , otherwise the first backup will be included in the second backup, and so forth.
On every Linux system, the /tmp/* , /proc/* , /dev/* , /sys/* , and /run/* are dynamically created, so you don’t need to include them in the backup, but you need to keep the directory structure. Therefore, you need to use /proc/* in the excluded.txt , as opposed to /proc .
You can add other files and folders that you don’t want to backup. In the example above, I have included /usr/portage/* and /usr/src/* . These are Gentoo Linux specific. The first one contains the Gentoo package ebuilds and sources. The second one contains Gentoo Linux kernel sources. They are big, and can both be downloaded again after a system restore. Feel free to edit the excluded.txt to suit your needs.
Add current date to the name of backup
sudo /usr/bin/tar --exclude-from=/home/john/exclude.txt -czpvf /home/john/backup/linux_backup-$(date +%F-%H-%M).tar.gz /
With $(date +%F-%H-%M) , the current date, hours, and minutes will be added to the backup file name. For example, linux_backup-2015-07-02-15-22.tar.gz .
Use a better compression program other than GZip
sudo /usr/bin/tar --exclude-from=/home/john/exclude.txt -cJpvf /home/john/backup/linux_backup-$(date +%F-%H-%M).tar.xz /
The -J switch can replace -z to use Xz for compression. It also changes the extension to tar.xz . Xz is slower than GZip, but it has a much better compression ratio, which results in a smaller backup file.
Putting everything in a shell script
vim /home/john/bin/linux_backup.sh
Include the following code in the script.
#!bin/sh _tarfile=/home/john/backup/linux_backup-$(date +%F-%H-%M).tar.xz sudo /usr/bin/tar --exclude-from=/home/john/exclude.txt -cJpvf $ /
Grant the script execute permissions.
sudo chmod +x /home/john/bin/linux_backup.sh
/home/john/bin/linux_backup.sh
Want to contribute?
You could earn up to $600 by adding new articles.
Linux tar command
On Unix-like operating systems, the tar command creates, maintains, modifies, and extracts files that are archived in the tar format.
This page covers the GNU/Linux version of tar.
Description
«Tar» stands for tape archive. It is an archiving file format.
tar was originally developed in the early days of Unix for the purpose of backing up files to tape-based storage devices. It was later formalized as part of the POSIX standard, and today is used to collect, distribute, and archive files, while preserving file system attributes such as user and group permissions, access and modification dates, and directory structures.
This documentation covers the GNU version of tar, which is included with most modern variants of the Linux operating system.
Syntax
tar [-] A --catenate --concatenate | c --create | d --diff --compare | --delete | r --append | t --list | --test-label | u --update | x --extract --get [options] [pathname . ]
Operation
The first argument to tar should be a function specification: either one of the letters A, c, d, r, t, u, or x, or one of the long function names. A function letter does not need to be prefixed with a dash («—«), and may be combined with other single-letter options. A long function name must be prefixed with a double dash («—«). Some options take a parameter; with the single-letter form these must be given as separate arguments. With the long form, they may be given by appending «=value» to the option.
For example, the following commands are all equivalent:
tar --create --file=archive.tar file1 file2
tar -c -f archive.tar file1 file2
tar -cf archive.tar file1 file2
tar cf archive.tar file1 file2
Functions
Specifying one of the following functions selects what tar‘s main mode of operation will be:
A, —catenate, —concatenate | Append tar files to an archive. |
c, —create | Create a new archive. |
d, —diff, —compare | Calculate any differences between the archive and the file system. |
—delete | Delete from the archive. (This function doesn’t work on magnetic tapes). |
r, —append | Append files to the end of a tar archive. |
t, —list | List the contents of an archive. |
—test-label | Test the archive label, and exit. |
u, —update | Append files, but only those that are newer than the copy in the archive. |
x, —extract, —get | Extract files from an archive. |
Other options
The following options specify the way tar operates:
bell | Play an audible bell at the console. |
dot, . | Print a single dot. |
echo | Display a text message at the console (to standard error). |
echo=string | Display string string on standard error; before output, any metacharacters in string are expanded. |
exec=command | Execute the given command. |
sleep=time | Wait for time seconds. |
ttyout=string | Output string on the current console (‘/dev/tty‘). |
Several —checkpoint-action options can be specified. The supplied actions will be executed in the order of their appearance on the command line.
- CVS/, and everything under it
- RCS/, and everything under it
- SCCS/, and everything under it
- .git/, and everything under it
- .gitignore
- .cvsignore
- .svn/, and everything under it
- .arch-ids/, and everything under it
- /, and everything under it
- =RELEASE-ID
- =meta-update
- =update
- .bzr
- .bzrignore
- .bzrtags
- .hg
- .hgignore
- .hgrags
- _darcs
PROG should follow two conventions: First, when invoked without additional options, it should read data from standard input, compress it and output it on standard output. Secondly, if invoked with the additional ‘-d‘ option, it should do exactly the opposite: read the compressed data from the standard input and produce uncompressed data on the standard output.
This option instructs tar to process only the numberth occurrence of each named file. The number defaults to 1, so:
tar -x -f archive.tar --occurrence filename
The —same-order option tells tar that the list of file names to be listed or extracted is sorted in the same order as the files in the archive. This allows a large list of names to be used, even on a small machine that would not otherwise be able to hold all the names in memory at the same time. Such a sorted list can easily be created by running tar -t on the archive and editing its output.
tar --extract --file archive.tar --strip-components=2
tar cf archive.tar --transform 's,^\./,usr/,'
Multiple «—warning» specifications may be used.
There are keywords for various warning messages available in tar. The two global keywords are:
Environment
The following environment variables affect the operation of tar:
SIMPLE_BACKUP_SUFFIX | File name suffix to use when backing up files, if —suffix is not specified. The default backup suffix is «~«. |
TAR_OPTIONS | Any options specified in this variable will be prepended to options specified to tar on the command line. |
TAPE | The archiving tape or file to use if —file is not specified. If this variable is not defined, and no —file is specified, tar uses standard input and standard output instead. |
Examples
tar -cf archive.tar file1 file2
Create archive archive.tar containing files file1 and file2. Here, the c tells tar you will be creating an archive; the f tells tar that the next option (here it’s archive.tar) will be the name of the archive it creates. file1 and file2, the final arguments, are the files to be archived.
List the files in the archive archive.tar verbosely. Here, the t tells tar to list the contents of an archive; v tells tar to operate verbosely; and f indicates that the next argument will be the name of the archive file to operate on.
Extract the files from archive archive.tar. x tells tar to extract files from an archive; f tells tar that the next argument will be the name of the archive to operate on.
Extract the files from gzipped archive archive.tar.gz verbosely. Here, the z tells tar that the archive will be compressed with gzip.
Creates an archive of the directory mydir.
tar -czf archive.tar.gz mydir/
Creates an gzip-compressed archive of the directory mydir.
Extract the contents of the myfile.tar.gz into the current directory.
tar -xvf archive.tar documents/work/budget.doc
Extract only the file documents/work/budget.doc from the archive archive.tar. Produce verbose output.
tar -xvf archive.tar documents/work/
Extract only the directory documents/work/, and any files it contains, from the archive archive.tar. Produce verbose output.
tar -xvf archive.tar --wildcards '*.doc'
Extract only files with the extension .doc from the archive archive.tar. The —wildcards option tells tar to interpret wildcards in the name of the files to be extracted; the file name (*.doc) is enclosed in single-quotes to protect the wildcard (*) from being expanded incorrectly by the shell.
tar -rvf archive.tar documents/work/budget.doc
Add the file documents/work/budget.doc to the existing archive archive.tar. The r option is the same as the long option —append.
tar -uvf archive.tar documents/work/budget.doc
Add the file documents/work/budget.doc to the archive archive.tar only if it’s newer than the version already in the archive (or does not yet exist in the archive). Here, u is the same as the long option —update.
tar -cf - documents/work/ | wc -c
Estimate the file size of an archive of the directory documents/work, but do not create the file. Here, the archive file is specified as a dash («—«), which tells tar to send its archived output to the standard output rather than a file on disk. This output is then piped to the wc command, which reports how many bytes (-c) were in the input it received.
tar -czf DogPhotos.tar.gz --exclude='kitty.jpg' MyPetPhotos
Create DogPhotos.tar.gz of all files contained in the MyPetPhotos without the kitty.jpg photo.
tar tf hope.tar.gz | grep myfile.txt
Search the hope.tar.gz file for the file myfile.txt and list the full path of the file. The returned results would resemble the line shown below.
tar -zxvf hope.tar.gz computerhopehope/homedir/public_html/data/myfile.txt
In the above example, the tar command would extract the one file myfile.txt from the hope.tar.gz. The full path to this file was determined using the example shown earlier.
Related commands
ar — Create, modify, and extract files from archives.
basename — Strip directory information and suffixes from file names.
cd — Change the working directory.
chown — Change the ownership of files or directories.
cpio — Copy files to or from archives.
dirname — Strip the file name from a pathname, leaving only the directory component.
gzip —Create, modify, list the contents of, and extract files from GNU zip archives.
ls — List the contents of a directory or directories.
mt — Control magnetic tapes.
zcat — Print the uncompressed contents of compressed files.