Incremental backups using tar

Incremental backups using tar

by -
0 1592

Large amount of data use to backup every day.  Takes long time and huge space required. We backup files fully by first time then want to backup files and folders which are recently modified or newly created, need not to backup unchanged files everyday. This saves time and space both. Incremental backup process should backup all modified/new files leaving behind unchanged files.

To do this task using tar utility, We need to provide an argument called “-listed-incremental=snapshot-file” or “-g” where snapshot-file is a special file created by the tar command to track files that are been modified, added or deleted.

Here  we are creating a Scenario to understand the task better :

  • /data :   Required data to be backed up
  • /backup:  Place where data is to be backed up

We have one directory “/data” which contains important file and we want to make full backup at first and then on daily bases we required incremental backup. Backup directory “/backup” where our all backups will be stored.

[[email protected] ~]# mkdir /data
[[email protected] ~]# mkdir /backup

[[email protected] data]# cat > file1
this is first file
dsa
das

[[email protected] data]# cat > file2
this is second file
asa
asdas
[[email protected] data]#

[[email protected] data]# ls
file1  file2

[[email protected] data]#

To create first full backup of /data directory  with snapshot file  using  –g  argument.

[[email protected] data]# tar -zcvf /backup/backup-full.tar.gz -g /data/snapshot-1 /data/

tar: /data: Directory is new
tar: Removing leading `/’ from member names
/data/
/data/file1
/data/file2
/data/snapshot-1
[[email protected] data]#

[[email protected] data]# ls
file1  file2  snapshot-1     <——-Snapshot file created by tar
[[email protected] data]#
[[email protected] data]# cd /backup
[[email protected] backup]# ls
backup-full.tar.gz               <—————-Full backup file by tar

Explaination here >
[[email protected] data]# tar -zcvf /backup/backup-full.tar.gz -g /data/snapshot-1 /data/

Where >>

  • Tar = Tape Archive Utility
  • c   = Create Tar File
  • v = Verbose Mode
  • f = It allows tar to get file name.
  • g = list all incremental files

/backup/backup-full.tar.gz  means we pointing the location of backup file and create archive files with backup-full.tar.gz  name.
/data/snapshot-1  is a snapshot file created by tar
To  view the content of backup-full.tar.gz  without extracting, provide this command

[[email protected] backup]# tar tvf backup-full.tar.gz
drwxr-xr-x root/root        27 2016-08-06 01:17 data/
-rw-r–r– root/root        27 2016-08-06 00:59 data/file1
-rw-r–r– root/root        30 2016-08-06 01:00 data/file2
-rw-r–r– root/root         0 2016-08-06 01:17 data/snapshot-1
[[email protected] backup]#
Now  I am creating one more file in /data directory.
[[email protected] backup]# cd /data
[[email protected] data]# cat >file3
this is third file
dsad
asd

[[email protected] data]#
[[email protected] data]# ls
file1  file2  file3  snapshot-1
[[email protected] data]#

Now run tar command again with addition to change tar archive file name to backup-1.tar.gz.

[[email protected] data]# tar -zcvf /backup/backup-1.tar.gz -g /data/snapshot-1 /data/
tar: Removing leading `/’ from member names
/data/
/data/file3
/data/snapshot-1
[[email protected] data]#

Only file3 is backed up.
[[email protected] data]# cd /backup/
[[email protected] backup]#
[[email protected] backup]# tar tvf backup-1.tar.gz
drwxr-xr-x root/root        34 2016-08-06 01:21 data/
-rw-r–r– root/root        29 2016-08-06 01:21 data/file3
-rw-r–r– root/root       106 2016-08-06 01:17 data/snapshot-1
[[email protected] backup]#

Result of this command is showing only file3  was backed up leaving behind unchanged data file1 and file2, It means tar is performing incremental backup properly.

Note :  We also use command like this

tar –listed-incremental=snapshot.file -cvzf backup.tar.gz /path/to/dir

 Create  a Shell Scripts for backup automation:

# vi backup.sh

#!/bin/bash
TIME=`date +%b-%d-%y%s`                 # Time Format
FILENAME=backup-$TIME.tar.gz       # backup file name using Time Syntax
SRC=/data                                                  # Source Directory
DST=/backup                                             # Destination Directory
SNAP=/data/snapshot1                           # SnapShot FIle
tar -cvf $DST/$FILENAME -g $SNAP $SRC
# END

Automation using Cron:

# contab –e

0 0 * * 1-6  /root/backup.sh

Where:
Script will run Monday to Saturday  at  Mid Night of every day of every Month

To Test, run it manually

[[email protected] ~]# sh backup.sh
tar: /data: Directory is new
tar: Removing leading `/’ from member names
/data/
/data/file1
/data/file2
/data/file3
/data/snapshot1

[[email protected] ~]# cd /backup/

[[email protected] backup]# ls
backup-Aug-06-161470428064.tar.gz
[[email protected] backup]#

Now I am modifying a file here to test incremental backup.

[[email protected] data]# cat >> file3
new line here
new line here
new line here
[[email protected] data]#

Run this script again to check
[[email protected] ~]# sh backup.sh
tar: Removing leading `/’ from member names
/data/
/data/file3

 [[email protected] ~]# cd /backup
[[email protected] backup]# ls
backup-Aug-06-161470428064.tar.gz  backup-Aug-06-161470428230.tar.gz

[[email protected] backup]# tar tvf backup-Aug-06-161470428230.tar.gz
drwxr-xr-x root/root        33 2016-08-06 01:44 data/
-rw-r–r– root/root        71 2016-08-06 01:46 data/file3
[[email protected] backup]#

Working perfectly   !!!

Ref site:
http://broexperts.com/2016/02/how-to-perform-incremental-backup-in-linux-using-tar-utility/
https://www.unixmen.com/performing-incremental-backups-using-tar/  

Enjoy Linux ……….!!!

Download PDF

CEO, KV IT-Solutions Pvt. Ltd. | [email protected] | 9810028374|
Linux Professional and an Industrial Trainer | 20 + years Experience in IT Industry

” We are born free, No Gate and Windows can snatch our freedom “

CEO, KV IT-Solutions Pvt. Ltd. | [email protected] | 9810028374| Linux Professional and an Industrial Trainer | 20 + years Experience in IT Industry " We are born free, No Gate and Windows can snatch our freedom "

SIMILAR ARTICLES

0 1237

0 1131

1 1175

NO COMMENTS

Leave a Reply