You are not logged in.
I have more than one computer with more than one OS installed on each and am trying to unify all my bash scripts putting them all in the "dropbox" folder and having just symlinks from everywhere pointing to the scripts. So when I change something, I don't have to edit 20 ;-) different files. Trying to make my backup script being "universal" I have slight problem and that is finding out the drive label of the root ("/") partition.
What I have is this two possibilities of getting the label of any /dev/sdXN:
label=$(sudo blkid /dev/sda2 -o value -s LABEL)
label=$(sudo e2label /dev/sda2)
if [ -z "$label" ]
then
echo "ERROR: root partition has no label!"
exit 1
fi
dest="/media/Backup/$HOSTNAME/$label"
if [ ! -d "$dest" ]
then
sudo mkdir -p "$dest"
fi
.....What I'm too stupid to get is the /dev/sdXN of the root partition or maybe the label of the root partition directly. I know that I could set a variable on each system to distinguish, but I'd like not to bother about that and having it done automatically, just putting a error-message when the root partition has no label at all.
Last edited by hymie (2011-11-29 13:38:55)
Offline
I found a solution through "mount -l". Maybe there is a more "elegant" one, but this seems to work...
label=$(mount -l | grep " / ")
label="${label##*[}"
label="${label%]*}"Offline
Here's the complete script, after having tested it a bit - maybe it can serve anybody else ;-)
It expects (or not) 3 files for "rsync" in the home folder (pretty self-explanatory):
backup-etc
backup-exclude-root
backup-exclude-home
If any of the files is not present, it will simply not backup the respective folder...
#!/bin/bash
if [ -z "$(mount | grep " /media/Backup ")" ]
then
echo "ERROR: /media/Backup not mounted!"
exit 1
fi
label=$(mount -l | grep " / ")
if [ "$label" == "${label/[/}" ]
then
label=""
else
label="${label##*[}"
label="${label%]*}"
fi
if [ -z "$label" ]
then
echo "ERROR: root partition has no label!"
exit 1
fi
dest="/media/Backup/$HOSTNAME/$label"
if [ -f "$HOME/backup-etc" ]
then
echo "***"
echo "*** BACKUP /etc to $dest/etc"
echo "***************"
if [ ! -d "$dest/etc" ]
then
sudo mkdir -p "$dest/etc"
fi
sudo rsync -a --progress --files-from="$HOME/backup-etc" /etc/ "$dest/etc/"
fi
if [ -f "$HOME/backup-exclude-root" ]
then
echo "***"
echo "*** BACKUP /root to $dest/root"
echo "***************"
if [ ! -d "$dest/root" ]
then
sudo mkdir -p "$dest/root"
fi
sudo rsync -a --delete --progress --exclude-from="$HOME/backup-exclude-root" /root/ "$dest/root/"
fi
if [ -f "$HOME/backup-exclude-home" ]
then
echo "***"
echo "*** BACKUP $HOME to $dest$HOME"
echo "***************"
if [ ! -d "$dest$HOME" ]
then
sudo mkdir -p "$dest$HOME"
sudo chown $(id -un):$(id -gn) "$dest$HOME"
fi
rsync -a --delete --progress --exclude-from="$HOME/backup-exclude-home" $HOME/ "$dest$HOME/"
fiLast edited by hymie (2011-11-30 00:49:54)
Offline