How To Use GNU Tar to Backup Linux
Table of Contents
Now that you have a perfectly configured Linux server on IT Web Services, 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.
Tar uses the following syntax.
sudo /usr/bin/tar -czpvf /home/john/backup/linux_backup.tar.gz /
-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
-vif you don’t want to see verbose output.
-f: Specify where to store the tar files. Here we save the backup file to
backupdirectory under user John’s home directory and name it
/: 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
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 /
--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
/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
You can add other files and folders that you don’t want to backup. In the example above, I have included
/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 /
$(date +%F-%H-%M), the current date, hours, and minutes will be added to the backup file name. For example,
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 /
-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
Create a script.
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
Perform a backup.
Do you need help setting up this on your own service?
Please contact us and we’ll provide you the best possible quote!