Linux - GeneralThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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...
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.
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] ???
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.