You are not logged in.

Announcement

Due to heavy spamming of forums registration is going in stages. If you wish to register as a new user with ArchBang Forums, first register and then send an e-mail to: archbangforums at gmail dot com. It should contain the problem you want to discuss or some other AB related content. You will be promoted from registering member with no posting rights to new member with posting rights after that. If your mail is ignored you haven't fulfilled the requirements.

#1 2011-11-29 12:11:54

hymie
Member
Registered: 2011-02-25
Posts: 66

question about my bash backup script [SOLVED]

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

#2 2011-11-29 13:37:38

hymie
Member
Registered: 2011-02-25
Posts: 66

Re: question about my bash backup script [SOLVED]

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

#3 2011-11-29 18:30:32

hymie
Member
Registered: 2011-02-25
Posts: 66

Re: question about my bash backup script [SOLVED]

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/"
fi

Last edited by hymie (2011-11-30 00:49:54)

Offline

Board footer

Powered by FluxBB