Solved [Solved] Bash script help

gentoobob

Active Member

Reaction score: 7
Messages: 172

I'm trying to set[ ]up a simple backup script but yet I'm finding myself struggling because no matter how much I google and read, can't seem to find what I am looking for.

So what I am wanting to do is mount my external USB drive, rsync data to it, place a time stamp into a log file, then unmount the drive. I don't want it mounted all the time as I may want to yank it out when I need it, not worry about logging into my system to unmount it. So far I've come up with this and it's not working.

Code:
#!/bin/bash

if [ -f /dev/da0p1 ] ; then
    mount /dev/da0p1 /backup
    rsync -a --delete /data/folder /backup/folder
    date > /home/user/backup/file.log
    umount /backup
else
    quit
fi
Any help would be greatly appreciated. :)
 

kpa

Beastie's Twin

Reaction score: 1,801
Messages: 6,318

Re: Bash script help

First, you won't need bash(1) for such a "simple" task, plain sh(1) will do. Next, your test -f /dev/da0p1 will probably fail always because /dev/da0p1 is not a file but a device special file. I would use the kern.geom.conftxt sysctl(8) instead to detect the presence of the disk and the partition, the sysctl -n kern.geom.conftxt output will contain lines like (this is from my system):

Code:
0 DISK cd0 0 2048 hd 0 sc 0
0 DISK ada0 68719476736 512 hd 16 sc 63
1 PART ada0p3 3221191680 512 i 3 o 65498267648 ty freebsd-swap xs GPT xt 516e7cb5-6ecf-11d6-8ff8-00022d09712b
2 LABEL gpt/fb10swap 3221191680 512 i 0 o 0
1 PART ada0p2 65498181632 512 i 2 o 86016 ty freebsd-ufs xs GPT xt 516e7cb6-6ecf-11d6-8ff8-00022d09712b
2 LABEL gpt/fb10root 65498181632 512 i 0 o 0
1 PART ada0p1 65536 512 i 1 o 20480 ty freebsd-boot xs GPT xt 83bd6b9d-7f41-11dc-be0b-001560b84f0f
2 LABEL gptid/596f0690-5efa-11e3-94a8-001c42ea40ca 65536 512 i 0 o 0
You could label the partition with a GPT lable and grep(1) for the partition label in the sysctl(8) output.

You may want to include a trailing slash in the source path for rsync(1) to avoid creating extra directories on the destination side: rsync -a --delete /data/folder/ /backup/folder.
 
OP
OP
gentoobob

gentoobob

Active Member

Reaction score: 7
Messages: 172

Re: Bash script help

Thanks @kpa. I found my issue with the script. As with everything in *nix, there's always more than one way to skin a cat. I just wanted to try to pick up some bash scripting fun. Thank you for the help.
 
Last edited by a moderator:

derekschrock

Member

Reaction score: 33
Messages: 80

Re: Bash script help

kpa said:
First, you won't need bash(1) for such a "simple" task, plain sh(1) will do. Next, your test -f /dev/da0p1 will probably fail always because /dev/da0p1 is not a file but a device special file. I would use the kern.geom.conftxt sysctl(8) instead to detect the precense of the disk and the partition, the sysctl -n kern.geom.conftxt output will contain lines like (this is from my system):

Code:
0 DISK cd0 0 2048 hd 0 sc 0
0 DISK ada0 68719476736 512 hd 16 sc 63
1 PART ada0p3 3221191680 512 i 3 o 65498267648 ty freebsd-swap xs GPT xt 516e7cb5-6ecf-11d6-8ff8-00022d09712b
2 LABEL gpt/fb10swap 3221191680 512 i 0 o 0
1 PART ada0p2 65498181632 512 i 2 o 86016 ty freebsd-ufs xs GPT xt 516e7cb6-6ecf-11d6-8ff8-00022d09712b
2 LABEL gpt/fb10root 65498181632 512 i 0 o 0
1 PART ada0p1 65536 512 i 1 o 20480 ty freebsd-boot xs GPT xt 83bd6b9d-7f41-11dc-be0b-001560b84f0f
2 LABEL gptid/596f0690-5efa-11e3-94a8-001c42ea40ca 65536 512 i 0 o 0
You could label the partition with a GPT lable and grep(1) for the partition label in the sysctl(8) output.

You may want to include a trailing slash in the source path for rsync(1) to avoid creating extra directories on the destination side:

Code:
rsync -a --delete /data/folder/ /backup/folder
Wouldn't it be better to check for the UUID of the file system?
 

kpa

Beastie's Twin

Reaction score: 1,801
Messages: 6,318

Re: Bash script help

Either UUID or label, pick your choice :)
 
Top