Linux-Backup-Script

Aus Info-Theke
Zur Navigation springen Zur Suche springen


Zielsetzung[Bearbeiten]

Erstellen eines Backups auf einem USB-Laufwerk.

Script[Bearbeiten]

#! /bin/bash
CMD=$1
ARG1=$2
LABEL=usb_sams232
OPT_BTRFS="noatime,nodiratime,compress=zstd,space_cache,autodefrag,subvolid=5,subvol=/"
MOUNT_OPT="$OPT_BTRFS"
MOUNT_POINT=/media/fs.$LABEL
MARKER=.backup.$LABEL.mrk
#SOURCES="/home /opt /etc"
SOURCES="/home /opt"
STD_USER=wk
TO_DELETE="/home/$STD_USER/.cache/mozilla/firefox/5xur4v4t.default-esr/cache2 /home/$STD_USER/.cache/mozilla/firefox/i9nyukos.default-esr-1/cache2"
TO_DELETE="$TO_DELETE /home/$STD_USER/.cache/xreader/CacheStorage /home/$STD_USER/.cache/thumbnails"
TO_DELETE="$TO_DELETE /home/$STD_USER/.local/share/Trash"
HOST=rambo
RSYNC_OPTS="-auv --progress --delete"
IS_BTRFS=True

function Usage(){
  echo "Usage: BackupUsb { mount | run | init LABEL | xfce | help { init }}"
  echo "Examples:"
  echo "BackupUsb mount"
  echo "BackupUsb run"
  echo "BackupUsb help init"
  echo "BackupUsb init usb-black"

  echo "+++ $*"
}
function DeleteCache(){
  for dir in $TO_DELETE; do
    test -d $dir && rm -Rfv $dir/* 
  done
}
function Help(){
  what=$1
  case "$what" in
    init)
      HelpInit
      ;;
    *)
      Usage
      ;;
  esac
}
function HelpInit(){
    echo "Run:"
    echo "lsblk"
    echo "LABEL=usb-blue"
    echo "DEV=/dev/sdc3"
    echo "mkfs.btrfs -L \$LABEL \$DEV"
    echo "tune2fs –L \$LABEL \$DEV"
    echo "btrfs filesystem label \$DEV \$LABEL"
    echo "BackupUsb init \$LABEL"
}

function Mount(){
  local mark=$1
  local rc=0
  test -d $MOUNT_POINT || mkdir $MOUNT_POINT
  #set -x
  local msg="$(mount | grep $LABEL)"
  if [ -n "$msg" ]; then
    echo "+++ already mounted: $msg"
  else
    mount LABEL=$LABEL -o $MOUNT_OPT $MOUNT_POINT
    mount | grep "$LABEL"
  fi
  if [ -n "$mark" ]; then
    if [ ! -e $MOUNT_POINT/$mark ]; then
      Usage "not a backup drive. Use 'init' to initialize"
      rc=1
    fi
  fi
  return $rc
}

function Init(){
  local label=$1
  Mount
  if [ $? = 0 ]; then
    echo "= creating marker"
    touch $MOUNT_POINT/$MARKER
    mkdir $MOUNT_POINT/$HOST
  fi
}

function SnapshotBtrfs(){
  local name=$1
  local trgBase=$MOUNT_POINT/$HOST/snapshots/$name
  local trg=$trgBase/$(date +%Y.%m.%d)
  local src=$MOUNT_POINT/$HOST/$name
  test -d $trgBase || mkdir -p $trgBase
  if [ -d $trg ]; then
    echo "= deleting snapshot $trg"
    btrfs subvolume delete $trg 
  fi
  echo "= creating snapshot $trg"
  btrfs subvolume snapshot -r $src $trg
}

function Run(){
  Mount $MARKER
  if [ $? = 0 ]; then
    for source in $SOURCES; do
      echo "=== $source"
      local name=$(basename $source)
      trg=$MOUNT_POINT/$HOST/$name
      if [ ! -d $trg ]; then
        echo "= creating $trg..."
        if [ "$IS_BTRFS" = "True" ]; then
          btrfs sub create $trg
        else
          mkdir $trg
        fi
      fi
      if [ $IS_BTRFS = True ]; then
        SnapshotBtrfs $name
      fi
      rsync $RSYNC_OPTS $source/ $trg
    done
    umount $MOUNT_POINT
  fi
}

function Xfce(){
  local trg2=$HOME/.local/share/applications/BackupUsb.desktop
  local trg=/tmp/BackupUsb.desktop
  if [ "$(id -u)" = 0 ]; then
    Usage "don't be root!"
  else
    cat <<EOS >$trg
[Desktop Entry]
Version=1.0
Type=Application
Name=BackupUsb
Comment=
Exec=/usr/bin/sudo /usr/local/bin/local/BackupUsb run
Icon=media-removable
Path=/tmp
Terminal=true
StartupNotify=true
EOS
    desktop-file-install --dir=$HOME/.local/share/applications $trg
    echo "= Find BackupUsb in Wisker menu and put it to the desktop"
  fi
}
if [ "$CMD" != xfce -a "$(id -u)" != 0 ]; then
  Usage "Be root!"
else
  case $CMD in
  help)
    Help $ARG1
    ;;
  init)
    Init $ARG1
    ;;
  run)
    DeleteCache
    Run
    read dummy "Bitte ENTER drücken"
    ;;
  xfce)
    Xfce
    ;;
  *)
    Usage "unknown command: $CMD"
    ;;
  esac
fi