You are not logged in.
Hi, i'm into Python and C++ (numerical math codes), so the system administration is really new to me.
I'm trying to write a really simple script that will check if my backup partition is mounted and if it is, where it is mounted and rsync my directory to the partition. The partition is on a USB HDD. I'm having problems because when I save the output of the mount command, and write it out in a for loop, the lines don't get echoed as they do within the bash itself, line per line, but one word per line:
BASH mount:
[tomislav@shakenbake ~]$ mount
/dev/sda2 on / type ext3 (rw,noatime)
udev on /dev type tmpfs (rw,nosuid,relatime,size=10240k,mode=755)
none on /proc type proc (rw,relatime)
none on /sys type sysfs (rw,relatime)
devpts on /dev/pts type devpts (rw)
shm on /dev/shm type tmpfs (rw,nosuid,nodev)
/dev/sda1 on /boot type ext3 (rw,noatime)
/dev/sda3 on /var type reiserfs (rw,noatime,notail)
/dev/sda6 on /home type ext3 (rw,noatime)
/dev/sdb1 on /media/archlinux-backup type ext3 (rw,noexec,nosuid,nodev,noatime)
/dev/sdb2 on /media/tomislav type ext3 (rw,noexec,nosuid,nodev,noatime)
[tomislav@shakenbake ~]$
Here's my script:
#!/bin/sh
# Backup the $WM_PROJECT_USER_DIR/applications/MASTER to /media/tomislav
# USB HDD if it is mounted.
MOUNTED_DISKS="$(mount)"
for line in $MOUNTED_DISKS; do
echo $line
done
gives me this:
[tomislav@shakenbake bin]$ ./backupThesis.sh
/dev/sda2
on
/
type
ext3
(rw,noatime)
udev
on
/dev
type
tmpfs
(rw,nosuid,relatime,size=10240k,mode=755)
none
on
/proc
type
proc
(snip)
I've just started learning BASH programming and I've tried to do the same thing (the for loop) using the $( ls ) command and it worked fine. What is the difference in the string output between the mount and ls if one is printed out in lines normally, and the other one has every word separated within the for loop?
Last edited by tomislavski (2010-04-12 01:52:56)
Offline
This is how I do my backups.
root:~/bin# cat BU
#!/bin/bash
#
if [ -d /mnt/sysbu ]
then
mkdir -p -v /mnt/sysbu
fi
# Make sure we do not double mount
( mount | grep /dev/sda2 ) && umount -v /dev/sda2
mount -v -t ext3 /dev/sda2 /mnt/sysbu
rsync -av --delete --delete-excluded --exclude="/tmp/" --exclude="/sys/" --exclude="/proc/" --exclude="/mnt/" --exclude="/dev/" / /mnt/sysbu
df -h
umount -v /mnt/sysbu
To know or not to know ...
... the questions remain forever.
Offline
This is how I do my backups.
root:~/bin# cat BU #!/bin/bash # if [ -d /mnt/sysbu ] then mkdir -p -v /mnt/sysbu fi # Make sure we do not double mount ( mount | grep /dev/sda2 ) && umount -v /dev/sda2 mount -v -t ext3 /dev/sda2 /mnt/sysbu rsync -av --delete --delete-excluded --exclude="/tmp/" --exclude="/sys/" --exclude="/proc/" --exclude="/mnt/" --exclude="/dev/" / /mnt/sysbu df -h umount -v /mnt/sysbu
Thanks a lot! I'm using the rsync in the same way. The only problem is in my udev rules, i think: when I unmount my USB HDD the directories don't get removed. That's why I thought about using the mount command which tells me what is actually mounted on the system. If I unmount my /media/tomislav or /media/archlinux-backup, these directories are still there afterwards. That induced an accident the other day because I have rsynced my / to /media/archlinux-backup and it wasn't mounted but the directory was there and I have filled up my / .
So I can look into these udev rules to remove the dirs after a successful umount or just try to find the /media/tomislav or tomislav string within the $MOUNTED_DISKS variable.... is this ok?
I've just found a tutorial on regex in BASH, i think this is the way to go. I'll just save the mount output to a variable, find the regular expressions for the mounted drives, check the mount points and then rsync to them respectfuly...
Last edited by tomislavski (2010-04-10 11:07:14)
Offline
The only problem is in my udev rules, i think: when I unmount my USB HDD the directories don't get removed.
This may not be a useful workaround, but all of my back-ups are to subdirectories on the mounted disk (eg /mnt/ext1/home..., /mnt/etc1/usr..., etc.) so I check for the existence of those subdirs to check if a disk is mounted, or not. Not as sophisticated as the other ways, but if the top few levels of the dir tree are stable, may be worth a try?
"...one cannot be angry when one looks at a penguin." - John Ruskin
"Life in general is a bit shit, and so too is the internet. And that's all there is." - scepticisle
Offline
Hi, i'm into Python and C++ (numerical math codes), so the system administration is really new to me.
I'm trying to write a really simple script that will check if my backup partition is mounted and if it is, where it is mounted and rsync my directory to the partition. The partition is on a USB HDD. I'm having problems because when I save the output of the mount command, and write it out in a for loop, the lines don't get echoed as they do within the bash itself, line per line, but one word per line:
BASH mount:
[tomislav@shakenbake ~]$ mount /dev/sda2 on / type ext3 (rw,noatime) udev on /dev type tmpfs (rw,nosuid,relatime,size=10240k,mode=755) none on /proc type proc (rw,relatime) none on /sys type sysfs (rw,relatime) devpts on /dev/pts type devpts (rw) shm on /dev/shm type tmpfs (rw,nosuid,nodev) /dev/sda1 on /boot type ext3 (rw,noatime) /dev/sda3 on /var type reiserfs (rw,noatime,notail) /dev/sda6 on /home type ext3 (rw,noatime) /dev/sdb1 on /media/archlinux-backup type ext3 (rw,noexec,nosuid,nodev,noatime) /dev/sdb2 on /media/tomislav type ext3 (rw,noexec,nosuid,nodev,noatime) [tomislav@shakenbake ~]$
Here's my script:
#!/bin/sh # Backup the $WM_PROJECT_USER_DIR/applications/MASTER to /media/tomislav # USB HDD if it is mounted. MOUNTED_DISKS="$(mount)" for line in $MOUNTED_DISKS; do echo $line done
gives me this:
[tomislav@shakenbake bin]$ ./backupThesis.sh /dev/sda2 on / type ext3 (rw,noatime) udev on /dev type tmpfs (rw,nosuid,relatime,size=10240k,mode=755) none on /proc type proc (snip)
I've just started learning BASH programming and I've tried to do the same thing (the for loop) using the $( ls ) command and it worked fine. What is the difference in the string output between the mount and ls if one is printed out in lines normally, and the other one has every word separated within the for loop?
$line
When you do
MOUNTED_DISKS=$(mount)
bash slurps the whole thing into a shell variable. When you do the
for line in $MOUNTED_DISKS; do echo $line; done
bash splits $MOUNTED_DISKS using $IFS, which by default splits on the usual whitespace characters -- space, tab, and newline.
Instead of trying to parse the output of mount to see if a filesystem is mounted, you can also do
if grep -q " /media/tomislav " /proc/mounts; then rsync -a /from /to; fi
which has the advantage of being much shorter and harder to confuse.
Offline
When you do
MOUNTED_DISKS=$(mount)
bash slurps the whole thing into a shell variable. When you do the
for line in $MOUNTED_DISKS; do echo $line; done
bash splits $MOUNTED_DISKS using $IFS, which by default splits on the usual whitespace characters -- space, tab, and newline.
Instead of trying to parse the output of mount to see if a filesystem is mounted, you can also do
if grep -q " /media/tomislav " /proc/mounts; then rsync -a /from /to; fi
which has the advantage of being much shorter and harder to confuse.
Thanks! I've read about IFS, now I get it... I should have known there is a /proc/mounts file... it's always some file somewhere with Linux... gnarly. Thank you!
Offline