LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-09-2011, 12:17 PM   #1
ananthkadalur
Member
 
Registered: Mar 2011
Posts: 38

Rep: Reputation: 0
Incremental backup with tar


Hi..Masters,
I realy hats off to linuxquestions.org site. This is very very useful site to everyone who all are on Linux platform. I am daily once or twice visiting this site so I could get some idea about shell scripting and at last I could made a shell script to do an incremental backup. All of the commands are not my own, I selected some commands from linuxquestions.org itself and also from here and there. I checked it contineously more than 6 hours by changing date and running script. It is doing well as my intention and the incremental backups also will happen only then if any changes are made since last backup file and if there is no changes then it will refuse by creating an empty archive. That is what I needed. Althogh I request you all to suggest me if still any modifications have to be done in the shell script. Or if this is good for an incremental backup with tar then I will go with this for backup.

Here is my shell script
#!/bin/bash
week=`date +%w`
date=`date +%d`
weekly_dir="/backup/weekly"
monthly_dir="/backup/monthly"
owb="/backup/old_week"
omb="/backup/old_month"
backup_dir1="/root /home /etc /var/www /var/spool/cron /data"
backup_dir2="/data/"
f=data
###do monthly full backup on every 1st of the month###
if [ $date -eq "01" ]; then
rm -rf $omb/*; mv $monthly_dir/* $omb
echo archiving $f.`date '+%Y-%m-%d'`.tar.bz2 ...
tar -cjf $monthly_dir/$f.`date '+%Y-%m-%d'`.tar.bz2 $backup_dir1
ls -ltrh $monthly_dir
fi
###do weekly full backup on every sunday###
if [ $week -eq "0" ]; then
rm -rf $owb/*; mv $weekly_dir/* $owb
echo archiving $f.`date '+%Y-%m-%d'`.tar.bz2 ...
tar -cjf $weekly_dir/$f.`date '+%Y-%m-%d'`.tar.bz2 $backup_dir2
else
###do incremental backup###
###prepare backup file name and compute the new number###
cd $weekly_dir
MAX=6
n=`ls -rt1 [0-5]* | tail -1 | cut -d. -f1`
new_n=`expr \( $n + 1 \) % $MAX`
filename=$new_n.$f.`date '+%Y-%m-%d'`
###remove the oldest one###
if [ -f $new_n.$f.* ] ; then rm -f "$new_n.$f"*; fi
###read the time stamp of recent file in $weekly_dir###
stat $( ls -rt $weekly_dir | tail -1 ) | grep Change | cut -d: -f 2,3 | while read time;
do
###create a file in /tmp so find command can find only new files in backup_dir2 ###since timestamp of /tmp/timetamp
touch -d "$time" /tmp/timestamp;
done
echo archiving $filename.tar.bz2 ...
###search only new files in $backup_dir2 and tar it###
tar -cjf $weekly_dir/$filename.tar.bz2 $(find $backup_dir2 -type f -newer /tmp/timestamp)
ls -ltrh $weekly_dir
rm -f /tmp/timestamp
cd
fi
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 10-10-2011, 01:28 AM   #2
kasl33
Member
 
Registered: Oct 2004
Location: USA
Distribution: Ubuntu-Server, Mac OS X, Arch Linux
Posts: 356

Rep: Reputation: 48
I don't really have the attention span to read your script at this time (haven't slept in a couple of days), however, for incremental backups, why not use rsync instead of tar?

For example:

Code:
rsync -azvr /path/to/source /path/to/backup
Using that command, you won't re-backup every single file and directory. You'll only make up the difference.

In my experience, rsync works great for everything except for virtual machines and other [extreme] large files.
 
2 members found this post helpful.
Old 10-10-2011, 12:35 PM   #3
ananthkadalur
Member
 
Registered: Mar 2011
Posts: 38

Original Poster
Rep: Reputation: 0
Incremental backup with tar

Yes master, you are right. I have used rsync many times. It works great. But I just wanted to check an incremental backup with only tar. So I posted this shell script here to finalize by some experts like you. Although rsync is good one, user friendly and can be done with only below command to sync with permission, time stamp, archiving while transfer and also with secure shell etc. But I request you all to suggest me if still any modification has to be done in this shell script.

Quote:
Originally Posted by kasl33 View Post
I don't really have the attention span to read your script at this time (haven't slept in a couple of days), however, for incremental backups, why not use rsync instead of tar?

For example:

Code:
rsync -azvr /path/to/source /path/to/backup
Using that command, you won't re-backup every single file and directory. You'll only make up the difference.

In my experience, rsync works great for everything except for virtual machines and other [extreme] large files.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Commands to incremental backup with tar loveulinux Linux - Newbie 2 09-22-2011 02:24 PM
using tar --listed-incremental as backup lrt Linux - Software 12 06-19-2009 07:55 AM
incremental backup using tar eaglegst Linux - Software 9 02-01-2009 05:00 PM
incremental backup using tar ARsenthil Linux - General 1 03-19-2008 08:29 AM
Incremental Backup (tar command) maginotjr Slackware 4 07-21-2005 01:04 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:49 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration