Incremental backups using tar

Incremental backups using tar

by -
0 1124

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.

[root@pc1 ~]# mkdir /data
[root@pc1 ~]# mkdir /backup

[root@pc1 data]# cat > file1
this is first file
dsa
das

[root@pc1 data]# cat > file2
this is second file
asa
asdas
[root@pc1 data]#

[root@pc1 data]# ls
file1  file2

[root@pc1 data]#

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

[root@pc1 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
[root@pc1 data]#

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

Explaination here >
[root@pc1 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

[root@pc1 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
[root@pc1 backup]#
Now  I am creating one more file in /data directory.
[root@pc1 backup]# cd /data
[root@pc1 data]# cat >file3
this is third file
dsad
asd

[root@pc1 data]#
[root@pc1 data]# ls
file1  file2  file3  snapshot-1
[root@pc1 data]#

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

[root@pc1 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
[root@pc1 data]#

Only file3 is backed up.
[root@pc1 data]# cd /backup/
[root@pc1 backup]#
[root@pc1 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
[root@pc1 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

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

[root@pc1 ~]# cd /backup/

[root@pc1 backup]# ls
backup-Aug-06-161470428064.tar.gz
[root@pc1 backup]#

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

[root@pc1 data]# cat >> file3
new line here
new line here
new line here
[root@pc1 data]#

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

 [root@pc1 ~]# cd /backup
[root@pc1 backup]# ls
backup-Aug-06-161470428064.tar.gz  backup-Aug-06-161470428230.tar.gz

[root@pc1 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
[root@pc1 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 ……….!!!

CEO, KV IT-Solutions Pvt. Ltd. | vikas@kvit.in | 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 “

Download PDF
CEO, KV IT-Solutions Pvt. Ltd. | vikas@kvit.in | 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 790

0 750

1 758

NO COMMENTS

Leave a Reply

Required Captcha *