LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 05-26-2020, 06:41 PM   #1
gw1500se
Member
 
Registered: Mar 2004
Distribution: Mandriva 2010.0
Posts: 244

Rep: Reputation: 31
tar Incremental Backup Not Working


I've written a bash script that does tar incremental backup on specific directories. A full backup works fine. I don't know what I'm doing wrong but when I specify an incremental backup, a full backup is done instead in the incremental directory. Perhaps some new eyes can see what I am missing. TIA.

Code:
#!/bin/bash
#
# backup for git and pit user directories
#
user=`whoami`
if [ "$user" != "root" ] ; then
	echo "must be run as root (sudo)"
	exit -1
fi
if [ `mount|grep /Backups|wc -l` -ne 1 ] ; then
   echo "Mounting backup share"
   mount /Backups
   if [ `mount|grep /Backups|wc -l` -ne 1 ] ; then
      echo "Backup skipped - backup share cannot be mounted"
      exit -1
   fi
fi
DIR1=/home/pi
DIR2=/home/git
DIR3=/usr/local/bin
META1=/Backups/pi.sngz
META2=/Backups/git.sngz
META3=/Backups/usr.sngz
DATE=$(date +%-Y%-m%-d)-$(date +%-T)
if [ $# -gt 0 ] ; then
   TYPE=$1
else
   TYPE="full"
fi
if [[ "$TYPE" == "full" || "$TYPE" == "incremental" ]] ; then
   echo "Backup type is $TYPE"
else
   echo "Usage: $0 full|incremental"
   exit -1
fi
if [ "$TYPE" == "full" ] ; then
   rm $META1
   rm $META2
   rm $META3
   rm /Backups/full/*
   rm /Backups/incremental/*
fi
OUTDIR=$1
OUT1=/Backups/$OUTDIR/pi-$DATE.tgz
OUT2=/Backups/$OUTDIR/git-$DATE.tgz
OUT3=/Backups/$OUTDIR/usr-$DATE.tgz
echo "Backing up $DIR1 to $OUT1"
tar --listed-incremental=$META1 -czf $OUT1 $DIR1
echo "Backing up $DIR2 to $OUT2"
tar --listed-incremental=$META2 -czf $OUT2 $DIR2
echo "Backing up $DIR3 to $OUT3"
tar --listed-incremental=$META3 -czf $OUT3 $DIR3
 
Old 05-26-2020, 09:13 PM   #2
michaelk
Moderator
 
Registered: Aug 2002
Posts: 20,063

Rep: Reputation: 3367Reputation: 3367Reputation: 3367Reputation: 3367Reputation: 3367Reputation: 3367Reputation: 3367Reputation: 3367Reputation: 3367Reputation: 3367Reputation: 3367
Some things to look at:

There are a few syntax errors in your script.

It is considered bad practice these days to use backticks. Use $() instead.

Valid exit codes are 0-255. Anything 1+ is considered an error.

OUTDIR=$1 could be blank if no command line arguments are used since the default is full. Maybe use OUTDIR=$TYPE
That would cause your full tgz files to be written to /Backups and not /Backups/full if not careful. Been a long time since I've played with incremental backups and looked at the listed-incremental file but it could be looking in this case in the wrong directory and therefore since it is empty create a full backup anyway.

Simpler to use grep -c /backups instead of grep /backups | wc -l or maybe use findmnt /backups and check if the string is empty.

It would make more sense to check if the uid = 0 versus username = root but that might be nitpicking.

You might want to add a test case to see of a full backup already exists when using incremental. While it should not happen during normal operations this could lead to confusion while testing...
 
1 members found this post helpful.
Old 05-26-2020, 09:21 PM   #3
jailbait
LQ Guru
 
Registered: Feb 2003
Location: Mineral, Virginia
Distribution: Debian 8
Posts: 7,873

Rep: Reputation: 317Reputation: 317Reputation: 317Reputation: 317
I would try:

if [ "$#" -gt 0 ] ; then
TYPE=$1
else
TYPE="full"
fi

------------------
Steve Stites
 
Old 05-27-2020, 02:14 AM   #4
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 14,633

Rep: Reputation: 4758Reputation: 4758Reputation: 4758Reputation: 4758Reputation: 4758Reputation: 4758Reputation: 4758Reputation: 4758Reputation: 4758Reputation: 4758Reputation: 4758
I would always check my shell script with: www.shellcheck.net
Code:
TYPE="${1:-missing}"
case ${TYPE} in
    full) ...
    incremental) ...
    *)  # something went wrong
esac
looks a bit better for me.

Also I would add set -xv at the beginning of the script to see what's happening.

Last edited by pan64; 05-27-2020 at 02:17 AM.
 
Old 05-27-2020, 07:29 AM   #5
gw1500se
Member
 
Registered: Mar 2004
Distribution: Mandriva 2010.0
Posts: 244

Original Poster
Rep: Reputation: 31
Thanks for all the suggestions (I'll implement them) but none of it addresses the root problem of tar not doing an incremental backup.
 
Old 05-27-2020, 08:37 AM   #6
michaelk
Moderator
 
Registered: Aug 2002
Posts: 20,063

Rep: Reputation: 3367Reputation: 3367Reputation: 3367Reputation: 3367Reputation: 3367Reputation: 3367Reputation: 3367Reputation: 3367Reputation: 3367Reputation: 3367Reputation: 3367
Have you tried running your tar commands separately from your script as a full and incremental backup? Does it work?

Using set -xv as suggested in your script might reveal where some subtle problem.

Nothing obvious is wrong in the actual tar commands but I have not tried simulating your full script.
 
Old 05-27-2020, 02:10 PM   #7
gw1500se
Member
 
Registered: Mar 2004
Distribution: Mandriva 2010.0
Posts: 244

Original Poster
Rep: Reputation: 31
Yes, it works from command line. I echoed the resulting tar command then copy/pasted it and it worked. So there seems to be something happening when run from the script.
 
Old 05-28-2020, 01:11 AM   #8
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 14,633

Rep: Reputation: 4758Reputation: 4758Reputation: 4758Reputation: 4758Reputation: 4758Reputation: 4758Reputation: 4758Reputation: 4758Reputation: 4758Reputation: 4758Reputation: 4758
that's why set -xv may help. It will print all the commands as they executed.
Also better coding may help you to understand what was really happened. Probably you missed a command (to put into this script) [a cd somewhere or similar] ???
 
Old 05-28-2020, 01:15 PM   #9
gw1500se
Member
 
Registered: Mar 2004
Distribution: Mandriva 2010.0
Posts: 244

Original Poster
Rep: Reputation: 31
I guess one of the suggested changes, although seemingly unrelated, fixed something. It is now working. I'm scratching my head but I'm not going to argue with success. Thanks for the help.
 
Old 05-28-2020, 01:47 PM   #10
michaelk
Moderator
 
Registered: Aug 2002
Posts: 20,063

Rep: Reputation: 3367Reputation: 3367Reputation: 3367Reputation: 3367Reputation: 3367Reputation: 3367Reputation: 3367Reputation: 3367Reputation: 3367Reputation: 3367Reputation: 3367
Please mark the thread as solved (if desired) using the thread tools pull down menu at the top.
 
Old 06-01-2020, 09:50 PM   #11
jailbait
LQ Guru
 
Registered: Feb 2003
Location: Mineral, Virginia
Distribution: Debian 8
Posts: 7,873

Rep: Reputation: 317Reputation: 317Reputation: 317Reputation: 317
Quote:
Originally Posted by gw1500se View Post
I guess one of the suggested changes, although seemingly unrelated, fixed something. It is now working. I'm scratching my head but I'm not going to argue with success. Thanks for the help.
My diagnosis of the problem is that one of your if statements was not working properly. No matter what value you put on the first positional parameter the TYPE="full" value was set. By comparing the value of $# instead of the literal $# the if statement began to work correctly.

--------------------
Steve Stites
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
tar --incremental OR tar --update , what to use ? crispyleif Linux - Newbie 1 02-20-2009 07:51 PM
incremental backup using tar eaglegst Linux - Software 9 02-01-2009 05:00 PM
BackUp & Restore with TAR (.tar / .tar.gz / .tar.bz2 / tar.Z) asgarcymed Linux - General 5 12-31-2006 02:53 AM
Incremental backup script - Tar problem joshheald Linux - Software 1 09-28-2004 10:02 AM
Getting date format right in tar incremental backup jonr Linux - Software 3 08-19-2004 09:53 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 03:48 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration