This is an old revision of the document!


Backup Linux-Rechner Lotte

#!/bin/bash
basedir=
user=lotte
mediadir=/media/$user
mountdir=/mnt/$user/autobackup
mediaids=(
  a134880c-d33e-4c70-8227-ea47e6341b1c
  de6ba9c3-cb24-41c0-aea8-a02be20ae30e
)
sourcedirs2backup=(
  /home/lotte
)

LOGFILE=/tmp/rsyncback.log
LOG_MESSAGES=1

# -------------------------------------------------------- functions ---
function message () {
  if [ ! -z $LOG_MESSAGES ]; then
      local msg=$1
      echo `date +"%Y-%m-%d %H:%M:%S"` $msg
  fi
  echo `date +"%Y-%m-%d %H:%M:%S"` $msg 2>&1 >> $LOGFILE
}

# ------------------------------------------------------------- Main ---
if [ ! -d $mediadir ]; then
  message "Error: Mediadir $mediadir does not exist. Exiting."
  exit 1
fi

for mid in "${mediaids[@]}"; do
  echo mid $mid
  if [ -d $mediadir/$mid ]; then
      basedir=$mediadir/$mid
      device=$(mount | grep $mid | cut -d " " -f 1)
      if [ ! -z "$device" ]; then
          break 
      fi
  fi
done
if [ -z "$basedir" ]; then
  message "No configured media mounted. Exiting"
  exit 1
fi
message "Medium mounted. Basedir: $basedir Device: $device"

lastdir=$(ls -1 $basedir | grep -v lost | tail -1)
targetdir=$basedir/$(date +%Y-%m-%d) 

if [ ! -d $targetdir ]; then
  message "performing backup to $targetdir"
  if [ ! -z "$lastdir" ] && [ -d "$basedir/$lastdir" ]; then
      message "copying $lastdir to $targetdir"
      nice cp -al $basedir/$lastdir $targetdir
  else
      message "no dir for copying found in $basedir"
  fi
  rsyncopts="-a -h --numeric-ids --relative --delete --delete-excluded --hard-links --stats"
  rsyncexcludes="--exclude .cache/ --exclude Trash/ --exclude datareporting/archived/ --exclude *msf"
  message "starting rsync"
  for sourcedir in "${sourcedirs2backup[@]}"; do
      nice rsync $rsyncopts $sourcedir $targetdir
  done
  message "backup on $basedir done" 
else
  message "heute schon gebackupt" 
fi
mount -o ro,remount $device $basedir

# Local variables:
# compile-command: "bash /home/springm/projekte/lotte/rsyncback.sh"
# End:

Eine Stunde nach dem Einschalten, weil sonst nicht sichergestellt ist, dass mit der Anmeldung auch die Sicherungsplatte gemountet ist.
Start über '/etc/rc.local' (neu anlegen, wenn die Datei nicht existiert, wird auch auf einem Ubuntu 22 trotz systemd ausgeführt). Start über rc.local ist nötig, damit das Skript als 'root' ausgeführt hat und die Privilegien für den Dateizugriff und das mounten hat.

#!/bin/bash
if [ ! -f /var/run/rsyncbackup ]; then
  echo /home/lotte/bin/rsyncbackup.sh | at now+60min 2>&1 > /tmp/rc_local_rsyncbackup.log
  touch /var/run/rsyncbackup
fi
exit 0
  • computer/ubuntu/minisini_backup.1671171602.txt.gz
  • Last modified: 2022/12/16 06:20
  • by springm