Steeph's Web Site

Go To Navigation
Show/Hide Navigation

Entries tagged 'cat:Backups'

backup.sh
This entry is an update of the entry 'Backup Shell Script'.

I've updated/improved my backup script again.

Download


#!/bin/bash
#2021-11-03

budir="/mnt/backup"
num=8
list="/root/backup-list"
hostname=$(</etc/hostname)
srcdir="$1"
#NAME=${SRCDIR//\//--}
name="$2"

function usage () {
  echo "This script requires either one or two arguments."
  echo "Usage:"
  echo "$0 JOBLIST"
  echo "$0 SOURCE_DIRECTORY NAME"
  echo
  echo "JOBLIST File that contains one backup job per line in the format SOURCE_DIRECTORY NAME"
  echo "SOURCE_DIRECTORY Directory that should be backed up. Please, no trailing slash."
  echo "NAME A string that is used to name the backup in the destination."
  exit 1
}

function single() {
  if grep -qs "$budir " /proc/mounts
  then
    fullname="$budir/$hostname/$name"
    printf "\n"
    printf "Attempting backup of source directory '%s' to '%s'.\n" "$srcdir" "$fullname"
    printf "Number of differential backups to keep: %s\n" "$num"
    printf "Removing oldest backup... "
#    rm -rf "$fullname.$num"
    rsync --archive --delete "$budir/empty/" "$fullname.$num/"		# This is quicker than rm.
    printf "Done.\n"
    for ((i=$num; i>=2; i--)); do
      printf "Renaming '%s' to '%s' ... " "$fullname.$((i-1))" "$fullname.$i"
      mv "$fullname.$((i-1))" "$fullname.$i"
      printf "Done.\n"
    done
    printf "Duplicating last backup ('%s' to '%s')... " "$fullname.0" "$fullname.1"
    [[ -d $fullname.1 ]] && exit 1							# This directory should not exist at that point.
    cp -al "$fullname.0" "$fullname.1"
#    rsync --archive --acls --xattrs --hard-links "$fullname.0/" "$fullname.1/"
    printf "Done.\n"
    now=$(date)
    printf "\n\n\n" >> "$fullname.log"
    printf "STARTING INCREMENTAL BACKUP AT %s\n" "$now" >> "$fullname.log"
    printf "Starting new incremental backy uppy at '%s.0'..." "$fullname"
    if rsync --exclude ".cache" --archive --no-links --delete "$srcdir/" "$fullname.0/" 2>&1 | tee -a "$fullname.log"
    then
      printf " Done.\n"
      printf "Done.\n" >> "$fullname.log"
    else
      printf " Failed.\n"
      printf "Failed.\n" >> "$fullname.log"
    fi
    now=$(date)
    printf "\nIf this line is here the script finished (with or without errors) at %s\n" "$now" >> "$fullname.log"
  else
    printf "'%s' is not mounted. Aborting." "$budir"
    exit 1
  fi
}

function fromlist() {
  while read job; do
    if [ -n "$job" ]; then
      name="${job#* }"
      srcdir="${job% *}"
      single
    fi
  done < "$list"
}

mkdir "$budir/empty"									# Empty directory for a quicker method to delete a large directory.

case $# in
0)
  mount "$budir"
  printf "Reading backup jobs from list. Defaulting to %s.\n" "$list"
  fromlist
  ;;
1)
  mount "$budir"
  list="$1"
  printf "Reading backup jobs from %s." "$list"
  fromlist
  ;;
2)
  mount "$budir"
  single
  ;;
3)
#  I can't decide whether to make the third argument num or budir. I don't need it anyway.
#  num=$3
#  budir=$3
#  mount $budir
#  single
  usage
  ;;
*)
  usage
  ;;
esac

exit 0

Theres no feeling like the feeling you get when you finally tidied up your .bashrc, added every alias and function you ever wanted there and then accidently overwrite it with something silly before you made a backup.

Backup Shell Script
This entry is an update of the entry 'Automatic backups with rsync under Linux'.
There is a newer version of this entry: 'backup.sh'.

This is an outdated version of my backup script. I've posted a newer version here.


Here is an updated, more versatile version of the simple backup script I posted before. It takes arguments now (wow)! So now you need only one backup.sh and in your cron tab or similar you can pass it the directory you would like to back up.

For example backup.sh /home/user UsersHomeDir will back up the home directory of user to the predefined location.

In preperation you only need to mount the drive you would like to back up to and enter the mount point (or a path inside it) at the top of the script (variable $BUDIR). In this destination folder (which can be an external drive or a NAS) backups are organised in directories named by the host name of the machine the script was executed on and the name of the backup job (the second argument). By default it will keep 8 incremental backups per backup name. This can be changed with the variable $NUM.

If you give the script only one argument it will interpret it as a name of a file that contains the directories that you want to back up.

If you pass no argument to the script it will look for such a file in the location that is pre-defined at the top of the script (variable $LIST).

You can download the script here if you want to.

Automatic backups with rsync under Linux
There is a newer version of this entry: 'Backup Shell Script'.

This is a very primitive and bad script. I've posted a new version here.

"No backup no pity!"

I don't agree. But I like the saying for reasons that I shall not analyse here right now.

I don't always have proper backups of my data, let alone off-site backups. I have no backup at all of a large portion of my hoarded data. But I did at some point take the time to create scripts that back up important stuff automatically. Here is a simple one using rsync and a mounted share as a target.


#!/bin/bash

NAME="usershomedir"
SRCDIR="/home/user"
BUDIR="/mnt/backup/"

rm -rf $BUDIR$NAME.8
mv $BUDIR$NAME.7 $BUDIR$NAME.8
mv $BUDIR$NAME.6 $BUDIR$NAME.7
mv $BUDIR$NAME.5 $BUDIR$NAME.6
mv $BUDIR$NAME.4 $BUDIR$NAME.5
mv $BUDIR$NAME.3 $BUDIR$NAME.4
mv $BUDIR$NAME.2 $BUDIR$NAME.3
mv $BUDIR$NAME.1 $BUDIR$NAME.2
cp -al $BUDIR$NAME.0 $BUDIR$NAME.1
NOW=$(date)
echo >> $BUDIR$NAME.log
echo >> $BUDIR$NAME.log
echo >> $BUDIR$NAME.log
echo "##### STARTING INCREMENTAL BACKUP AT $NOW" >> $BUDIR$NAME.log
echo >> $BUDIR$NAME.log
rsync --archive -L --delete $SRCDIR  $BUDIR$NAME.0/ 2>&1 | tee -a $BUDIR$NAME.log

You would create one of these scripts per directory you want to backup. BUDIR is the directory where you want to backup to be stored. This will probably be the same for every script if you have only one backup drive/server. SRCDIR is the directory you want to backup. NAME is the name you want the backup to be stored under. It can be a sub-directory if you want, so you can sort the backups into directories on the target.