Article

Table of Contents
Try Vultr Today with

$50 Free on Us!

Use GNU Tar to Backup Linux

Published on: Thu, Jul 2, 2015 at 3:50 am EST
Linux Guides Server Apps

Introduction

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

Create a 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

Perform a backup.

 /home/john/bin/linux_backup.sh

Want to contribute ?

You could earn up to $300 by adding new articles