Hi all
I am just learning bash wrote my first script. It’s purpose is to perform a daily backup of hard drive to a usb stick. I appears to be working ok, but would appreciate any insight, comments or suggestions as to how I might improve it. If possible, please don’t post actual code, just point me in the right direction or else I’ll be to lazy to learn lol!. Thanks all
# Test if usb drive is mounted
#
TESTDIR="/media/sparkz"
if
-d "$TESTDIR" ]
then
# echo "already mounted"
# ls -al /media
rsync -a -v -i --progress -H -s \
--delete \
--delete-excluded \
--exclude-from=/home/sparkz/Backup/exclude_list.txt \
--log-file=/var/log/rsync.log / /media/
umount /dev/sdb1
else
mount /dev/sdb1 /media/
rsync -a -v -i --progress -H -s \
--delete \
--delete-excluded \
--exclude-from=/home/sparkz/Backup/exclude_list.txt \
--log-file=/var/log/rsync.log / /media/
# echo "now mounted"
# ls -al /media
umount /dev/sdb1
fi
#
# rsync exclude list used by full system backup
#
dev
lost+found
media
mnt
proc
run
sys
tmp
var
.readahead
*/lost+found/*
*/.readahead/*
*/Trash/*
*~*
*.backup
*.bak
*.cache
*.swp
*.tmp
*.temp
As an addition, you should not use /media yourself. /media is for the use of the desktop mounting mechanism. And as you now use /media as mountpoint, you are in the way of any mounts there.
Use e.g. something inside /mnt (and not /mnt itself) or any other place you like.
I appreciate the feedback, forgot to copy the whole file for some reason. Yes, I’m testing for a directory to see if the usb is mounted. If there is a better way, then I’m open to suggestions. Right now I’m reading up on ‘exit’ codes to see if I can use them in this script. Thanks again for the reply and here (hopefully) is the full text:
#! /bin/bash
#
# Shell program to run rsyn backup of system
#
#
# Test if usb drive is mounted
#
TESTDIR="/media/sparkz"
if
-d "$TESTDIR" ]
then
# echo "already mounted"
# ls -al /media
rsync -a -v -i --progress -H -s \
--delete \
--delete-excluded \
--exclude-from=/home/sparkz/Backup/exclude_list.txt \
--log-file=/var/log/rsync.log / /media/
umount /dev/sdb1
else
mount /dev/sdb1 /media/
rsync -a -v -i --progress -H -s \
--delete \
--delete-excluded \
--exclude-from=/home/sparkz/Backup/exclude_list.txt \
--log-file=/var/log/rsync.log / /media/
# echo "now mounted"
# ls -al /media
umount /dev/sdb1
fi
#
# rsync exclude list used by full system backup
#
dev
lost+found
media
mnt
proc
run
sys
tmp
var
.readahead
*/lost+found/*
*/.readahead/*
*/Trash/*
*~*
*.backup
*.bak
*.cache
*.swp
*.tmp
*.temp
And I can not see the difference between the two rsync statements you have in the two cases. To me they look the same and thus should be outside if the if-the-else contruct.
And that list at the end should not be in the script. But maybe you do not show one file, but two.
Please copy/paste complete: the prompt, the command, theoutput and the next promt. Thus we can see waht you did. So the following is “wrong”
inhoud
nieuwe inhoud
But this is providing the information needed:
henk@boven:~/test> cat file
inhoud
nieuwe inhoud
henk@boven:~/test>
You did not say so, but as I see it, this is to be a script to be run by the superuser (root).
You should not depend on that device mounted somewhere (maybe tomorrow on a different place) when an ennduser (thus not root) connects it to the system. To begin with the ownership of the mountpoint will also be wrong.
As this is a root action, you should mount as root at a place defined by root (create the mountpoint in advance as root and not in /media or the /var places, that is all for end user media mounts, media meaning CDs DVDs, Stickies with movies, music, etc.) and best is to have an fstab entry for it (with noauto).
BTW there is no real need to test if it is mounted. When you do the mount, it will mount when it is not mounted and do nothing (maybe a message) when it is already mounted.
Or use “udisksctl” to mount it. This will mount it the same way as KDE or GNOME do (and to the same mount point of course), and will even be possible if run as a user.
You could also check whether it’s mounted already, but it wouldn’t harm if you try to mount it again in that case (it won’t be mounted again), as already mentioned.
Sorry, but I did not ask you to run the script. I did ask you to post complete when you show the script. In post #4 above you showed output (looked like a script and some data after it, but shown in one long list), but not how you got that output.
appear twice in the script, exactly the same. Those should then of copurse not be inside the if-then-else, but outside. Thus, where you have (I changed the layout to make it better readable for me):
if -d "$TESTDIR" ]
then
rsync -a -v -i --progress -H -s \
--delete \
--delete-excluded \
--exclude-from=/home/sparkz/Backup/exclude_list.txt \
--log-file=/var/log/rsync.log / /media/
umount /dev/sdb1
else
mount /dev/sdb1 /media/
rsync -a -v -i --progress -H -s \
--delete \
--delete-excluded \
--exclude-from=/home/sparkz/Backup/exclude_list.txt \
--log-file=/var/log/rsync.log / /media/
umount /dev/sdb1
fi
You can take the common part out:
if -d "$TESTDIR" ]
then
else
mount /dev/sdb1 /media/
fi
rsync -a -v -i --progress -H -s \
--delete \
--delete-excluded \
--exclude-from=/home/sparkz/Backup/exclude_list.txt \
--log-file=/var/log/rsync.log / /media/
umount /dev/sdb1
fi
Which isn’t of course correct coding, so change to something like:
if -d ${TESTDIR} ]] || mount /dev/sdb1 /media/
rsync -a -v -i --progress -H -s \
--delete \
--delete-excluded \
--exclude-from=/home/sparkz/Backup/exclude_list.txt \
--log-file=/var/log/rsync.log / /media/
umount /dev/sdb1
After all, the logic is that you check if it is mounted, when not mount it.
And then do the rsync, regardless of what happened earlier.
Thanks Hank, I’ ll try out your suggestion(s), first I have to read up on what exactly I’ll be doing Just out of curiosity, would I not also need a ‘fi’ since I’m using an ‘if’ ?
> You could also check whether it’s mounted already, but it wouldn’t harm
> if you try to mount it again in that case (it won’t be mounted again),
> as already mentioned.
I always test to see if it is mounted. It might have failed for some
reason, and it is better to bail out or the backup of /home may en on
the “/” directory and crash the machine (no space).
What I do is something like:
OUTPUT=`mount | grep /mnt/somewhere"
and then test that variable to find what it contains.
You can test for the mount, try to mount if not there, test again, bail
out if failed second time.
Thanks all for the input. Definitely seems I have some reading and testing to do. I appreciate your taking the time and effort. To give you all an idea what a true noob I am at this, I created a /usb directory to use as my new mount point…which is surprisingly similar to /usr, particularly when your not paying attention. Soo, I am reloading openSUSE (for the 3rd time in as many months ).
Since it will be a bit before I return, does anyone know how I close this thread?
On 2014-05-12 00:06, sparkz alot wrote:
>
> Thanks all for the input. Definitely seems I have some reading and
> testing to do. I appreciate your taking the time and effort. To give you
> all an idea what a true noob I am at this, I created a /usb directory to
> use as my new mount point…which is surprisingly similar to /usr,
> particularly when your not paying attention.
You mean that that you mounted the external device on “/usr”, and
perhaps wrote to it? Then don’t worry, nothing was destroyed. Just
umount it, everything will be back to normal.
Or you mean you wrote the backup into “/usr”, instead of where the
device was, at “/usb”? Then you may have overwritten some files (worst
case), but probably with the same file. Or, best case, you created some
extra directories there, which you can simply delete now.
> Since it will be a bit before I return, does anyone know how I close
> this thread?
You can’t. Threads are not closed in this forum, unless they break some
rule and have to.