You are not logged in.

#1 2007-04-14 10:30:11

Lontronics
Member
Registered: 2006-08-28
Posts: 121

Backup script

This is a script I have added to my /etc/rc.local, so a daily backup is made from some important files/ directories.

Please read the script before using it wink

#!/bin/sh

bu_path=/data/backups/diversen          # path for backup
bu_name=$(date '+%y%m%d')_backup_PC1    # name of backup
archive=gz                              # empty, bz2 or gz. archive is made and the backup directory will be removed

# Define the files which should be copied into backup
backupfiles=(
/etc/fonts/fonts.conf
/etc/fstab
/etc/group
/etc/hosts
/etc/locales.gen
/etc/rc.conf
/etc/network-profiles/eth0
/etc/network-profiles/wlan0
/etc/pacman.conf
/etc/pacman.d
/etc/rc.d
/etc/rc.local
/etc/rc.local.shutdown
/etc/resolv.conf
/etc/sane.d/plustek.conf
/etc/udev/rules.d/sane.rules
/etc/wpa_supplicant.conf
/etc/X11
/home/jan
/usr/share/blackbox
)

######################################################

# Check if everything is ok and set internal parameters

org=$(pwd)
backuproot=$bu_path/$bu_name

if [ "$archive" = "bz2" ]; then
  compprog=bzip2
elif [ "$archive" = "gz" ]; then
  compprog=gzip
else
  archive=""
fi

if [ "$bu_path" = "" ]; then
  echo "ERROR: The backup root directory name must be filled in before using this script."
  echo "Please edit the script parameters before using this script."
  exit 1
fi

if [ "$bu_name" = "" ]; then
  echo "ERROR: The backup name must be filled in before using this script."
  echo "Please edit the script parameters before using this script."
  exit 1
fi
 
if [ -d $backuproot ]; then
  echo "Backup directory $backuproot does already exist."
  if [ "$1" = "--force" ]; then
    rm -rf $backuproot
    echo "Old directory $backuproot is removed because of using -force"
  else
    echo "Please rename to make a new backup, or use --force to overwrite"
    exit 1
  fi
fi

if [ -f $backuproot.tar.$archive ]; then
  echo "Backup file $backuproot.tar.$archive does already exist."
  if [ "$1" = "--force" ]; then
    rm -rf $backuproot.tar.$archive
    echo "Old backup file $backuproot.tar.$archive is removed because of using --force"
  else
    echo "Please rename to make a new backup, or use --force to overwrite"
    exit 1
  fi
fi

# Make new backup directory
mkdir -p $backuproot

# Make the backup
echo -n "Copy files into backup directory..."

cnt=${#backupfiles[@]}
for (( i=0 ; i < cnt ; i++ ))
do
  if [ -d ${backupfiles[$i]} ]; then
    cp -afr --parent ${backupfiles[$i]} $backuproot
  elif [ -f ${backupfiles[$i]} ]; then
    cp -af --parent ${backupfiles[$i]} $backuproot
  fi
done

echo "OK"

# Make archive if wanted
if [ "$archive" != "" ]; then
  cd $bu_path
  echo -n "Making archive..."
  tar -cvf $bu_name.tar $bu_name &> /dev/null
  $compprog $bu_name.tar
  sleep 0.5
  rm -rf $bu_path/$bu_name
  rm -rf $bu_path/$bu_name.tar
  cd $org
  echo "OK"
fi

Offline

Board footer

Powered by FluxBB