Snapshot-RSync: Unterschied zwischen den Versionen

Aus Neobikers Wiki
Zur Navigation springen Zur Suche springen
Keine Bearbeitungszusammenfassung
Keine Bearbeitungszusammenfassung
Zeile 1: Zeile 1:
Ich sichere 3x täglich per Snapshot meine Daten mit folgendem Script. Zusätzlich führe ich 2x die Woche einen Filesystemcheck auf /backup aus. Beises sind cronjobs.
Ich sichere 3x täglich per Snapshot meine Daten mit folgendem Script. Zusätzlich führe ich 2x die Woche einen Filesystemcheck auf /backup aus. Beises sind cronjobs.
<pre>
<pre>
#!/bin/sh
#!/bin/bash
# snapshot
# snapshot
# make a snapshot from directories
# make a snapshot from directories
# an create hourly/daily/monthly/yearly archives
# an create hourly/daily/monthly/yearly archieves
# ---------
# ---------
# neobiker (2006)
# neobiker (2006)
Zeile 10: Zeile 10:
# ...
# ...
# ...
# ...
# The directories are named 'snapshot-<year>-<month>-<day>-<hour>'
# The directories are named 'snapshot-<year>-<month>-<day>__<hour>'
#
#
# Each time snapshot runs, it date stamps a file in the <snapshot> directory called
# Each time snapshot runs, it date stamps a file in the <snapshot> directory called
Zeile 27: Zeile 27:
# you get the idea
# you get the idea
# ...
# ...
#########################################################################
#########################################################################
# Start User Config Stuff
# Start User Config Stuff
snapshot="srv1-data"
snaphost=""            # localhost


# This is the local root of the backup directories. (where to backup to)
# This is the local root of the backup directories. (where to backup to)
backuproot=/backup
backuproot=/srv/backup
snapshot="srv-data"
snaphost=""


# You need to set the newdayhour to 00-23 depending on what hour you run
# You need to set the newdayhour to 00-23 depending on what hour you run
Zeile 43: Zeile 43:
# number of snapshots to save
# number of snapshots to save
savehours=3
savehours=3
savedays=7
savedays=6
saveweeks=4
saveweeks=4
savemonths=12
savemonths=12
saveyears=10
saveyears=10


# --------------------------
# list directories to backup
# list directories to backup
# --------------------------
allsrcdir="
allsrcdir="
/home/
/home/
/srv/
/srv/
/var/lib/cyrus/
/var/spool/
/var/www/
/var/www/
"
"


# -----------------------------------------
# list directories to skip during in backup
# list directories to skip during in backup
# or leave blank
# or leave blank
# EXCLUDES=""
# EXCLUDES=""
# -----------------------------------------


EXCLUDES="--filter=. /tmp/excludes-${snapshot}"
EXCLUDES="--filter=. /tmp/excludes-${snapshot}"
Zeile 69: Zeile 63:
cat > /tmp/excludes-${snapshot} << EOF
cat > /tmp/excludes-${snapshot} << EOF
- lost+found
- lost+found
- /backup/
- /srv/backup
- /snapshot/
- /srv/snapshot
- /var/spool/squid/
- /srv/devel
- /var/spool/samba/
- /var/spool
+ *
+ *
EOF
EOF
Zeile 111: Zeile 105:
cmdline=$1
cmdline=$1
PATH=/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin
PATH=/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin
cmonth=`date +'%m'`
doweek=`date +'%w'`
  cday=`date +'%d'`
chour=`date +'%H'`


  yeardir=${backuproot}/${snapshot}/yearly
  yeardir=${backuproot}/${snapshot}/yearly
Zeile 123: Zeile 112:
  hourdir=${backuproot}/${snapshot}/hourly
  hourdir=${backuproot}/${snapshot}/hourly


snapshotdir=`date +'snapshot-%Y-%m-%d-%H'`
[ -d ${yeardir} ]  || mkdir -p ${yeardir}
[ -d ${monthdir} ] || mkdir -p ${monthdir}
[ -d ${weekdir} ]  || mkdir -p ${weekdir}
[ -d ${daydir} ]  || mkdir -p ${daydir}
[ -d ${hourdir} ]  || mkdir -p ${hourdir}


lockfile=${backuproot}/${snapshot}/syncing-now
lockfile=${backuproot}/${snapshot}/syncing-now
snapdir=$hourdir/`date +'snapshot-%Y-%m-%d-%H'`


lsync=`/bin/ls -1r ${hourdir} | head -1`
cmonth=`date +'%m'`
lday=`date +'snapshot-%Y-%m-%d-' -d '1 day ago'`${chour}
doweek=`date +'%w'`
lweek=`date +'snapshot-%Y-%m-%d-' -d '1 week ago'`${chour}
  cday=`date +'%d'`
chour=`date +'%H'`
lhour=`/bin/ls -1r ${hourdir} | tail -1`
  lday=`/bin/ls -1r ${daydir}  | tail -1`
lweek=`/bin/ls -1r ${weekdir} | tail -1`
lmonth=`/bin/ls -1r ${monthdir}| tail -1`


makedirs ()
for dir in ${hourdir} ${daydir} ${weekdir} ${monthdir} ${yeardir}; do
{
  sync=`/bin/ls -1 ${dir} | tail -1`
    [ -d ${yeardir} ]  || mkdir -p ${yeardir}
  if [ -n "${sync}" ]; then
    [ -d ${monthdir} ] || mkdir -p ${monthdir}
     syncdir=$dir/$sync
    [ -d ${weekdir} ]  || mkdir -p ${weekdir}
    break
    [ -d ${daydir} ]   || mkdir -p ${daydir}
  fi
     [ -d ${hourdir} ]  || mkdir -p ${hourdir}
done
}


dosync ()
dosync ()
{
{
     [ -d ${hourdir}/${snapshotdir} ] || mkdir -p ${hourdir}/${snapshotdir}
     [ -d ${snapdir} ] || mkdir -p ${snapdir}
     [ -n "${lsync}" ] && rsync="${rsync} --link-dest=${hourdir}/${lsync}"
     [ -n "${syncdir}" ] && rsync="${rsync} --link-dest=${syncdir}"


     for srcdir in ${allsrcdir}; do
     for srcdir in ${allsrcdir}; do
Zeile 149: Zeile 147:
         srcdir=${snaphost}:${srcdir}
         srcdir=${snaphost}:${srcdir}
       fi
       fi
       ${rsync} "${EXCLUDES}" ${srcdir} ${hourdir}/${snapshotdir}
       ${rsync} "${EXCLUDES}" ${srcdir} ${snapdir}
     done
     done
}
}


doyearly ()
movesnap ()
{
{
     [ ${saveyears} -eq 0 ] && return
    case "$1" in
     if [ -d ${monthdir}/${snapshotdir} ]; then
        d)
       cp -alf  ${monthdir}/${snapshotdir} ${yeardir}
          dir=$daydir
          nr=$savedays
          snap=$hourdir/$lhour
          ;;
        w)
          dir=$weekdir
          nr=$saveweeks
          snap=$daydir/$lday
          ;;
        m)
          dir=$monthdir
          nr=$savemonths
          snap=$weekdir/$lweek
          ;;
        y)
          dir=$yeardir
          nr=$saveyears
          snap=$monthdir/$lmonth
          ;;
        *)
          echo >&2 "movesnap: unknow option $1, aborting..."
          rm -f ${lockfile}
          rm -f /tmp/excludes-${snapshot}
          umounting
          exit 1
          ;;
    esac
     [ ${nr} -eq 0 ] && return
     if [ -d ${snap} ]; then
       mv ${snap} ${dir}
     else
     else
       echo >&2 "I can't find snapshot ${monthdir}/${snapshotdir}..."
       echo >&2 "I can't find snapshot ${snap}..."
    fi
}
 
domonthly ()
{
    [ ${savemonths} -eq 0 ] && return
    if [ -d ${hourdir}/${snapshotdir} ]; then
      mv ${hourdir}/${snapshotdir} ${monthdir}
      ln -s ${monthdir}/${snapshotdir} ${hourdir}/${snapshotdir}
    else
      echo >&2 "I can't find snapshot ${hourdir}/${snapshotdir}..."
    fi
}
 
doweekly ()
{
    [ ${saveweeks} -eq 0 ] && return
    if [ -d ${daydir}/${lweek} ]; then
      mv ${daydir}/${lweek} ${weekdir}
    else
      echo >&2 "I can't find snapshot ${daydir}/${lweek}..."
    fi
}
 
dodaily ()
{
    [ ${savedays} -eq 0 ] && return
    if [ -d ${hourdir}/${lday} ]; then
      mv ${hourdir}/${lday} ${daydir}
    else
      echo >&2 "I can't find snapshot ${hourdir}/${lday}..."
     fi
     fi
}
}
Zeile 199: Zeile 195:
     count=0
     count=0
     for i in `/bin/ls -r ${hourdir}`; do
     for i in `/bin/ls -r ${hourdir}`; do
       let count=count+1
       ((count++))
       if [ ${count} -gt ${savehours} ]; then
       if [ ${count} -gt ${savehours} ]; then
         rm -Rf ${hourdir}/${i}
         rm -Rf ${hourdir}/${i}
Zeile 205: Zeile 201:
     done
     done


    [ ${saveweeks} -gt 0 -a ${savedays} -lt 7 ] && savedays=7
     count=0
     count=0
     for i in `/bin/ls -r ${daydir}`; do
     for i in `/bin/ls -r ${daydir}`; do
       let count=count+1
       ((count++))
       if [ ${count} -gt ${savedays} ]; then
       if [ ${count} -gt ${savedays} ]; then
         rm -Rf ${daydir}/${i}
         rm -Rf ${daydir}/${i}
Zeile 216: Zeile 211:
     count=0
     count=0
     for i in `/bin/ls -r ${weekdir}`; do
     for i in `/bin/ls -r ${weekdir}`; do
       let count=count+1
       ((count++))
       if [ ${count} -gt ${saveweeks} ]; then
       if [ ${count} -gt ${saveweeks} ]; then
         rm -Rf ${weekdir}/${i}
         rm -Rf ${weekdir}/${i}
Zeile 224: Zeile 219:
     count=0
     count=0
     for i in `/bin/ls -r ${monthdir}`; do
     for i in `/bin/ls -r ${monthdir}`; do
       let count=count+1
       ((count++))
       if [ ${count} -gt ${savemonths} ]; then
       if [ ${count} -gt ${savemonths} ]; then
         rm -Rf ${monthdir}/${i}
         rm -Rf ${monthdir}/${i}
Zeile 232: Zeile 227:
     count=0
     count=0
     for i in `/bin/ls -r ${yeardir}`; do
     for i in `/bin/ls -r ${yeardir}`; do
       let count=count+1
       ((count++))
       if [ ${count} -gt ${saveyears} ]; then
       if [ ${count} -gt ${saveyears} ]; then
         rm -Rf ${yeardir}/${i}
         rm -Rf ${yeardir}/${i}
Zeile 263: Zeile 258:
[ -f ${lockfile} ] && inuse
[ -f ${lockfile} ] && inuse
mounting
mounting
makedirs
touch ${lockfile}
touch ${lockfile}


case ${cmdline} in
case ${cmdline} in
   -y)
   -y)
     doyearly
     dosync
    movesnap "y"
     docleanup
     docleanup
     ;;
     ;;
   -m)
   -m)
     domonthly
     dosync
    movesnap "m"
     docleanup
     docleanup
    ;;
  -w)
    dosync
    movesnap "w"
    docleanup
    ;;
  -d)
    dosync
    movesnap "d"
    docleanup
    ;;
  --scalix)
    ombackup
     ;;
     ;;
   *)
   *)
     dosync
     dosync
     if [ ${newdayhour} -eq ${chour} ]; then
     if [ ${newdayhour} -eq ${chour} ]; then
       dodaily
       movesnap "d"
       if [ ${doweek} -eq 0 ]; then
       if [ ${doweek} -eq 0 ]; then
         doweekly
         movesnap "w"
       fi
       fi
       if [ ${cday} -eq 01 ]; then
       if [ ${cday} -eq 01 ]; then
         domonthly
         movesnap "m"
         if [ ${cmonth} -eq 01 ]; then
         if [ ${cmonth} -eq 01 ]; then
           doyearly
           movesnap "y"
         fi
         fi
       fi
       fi

Version vom 24. April 2011, 08:39 Uhr

Ich sichere 3x täglich per Snapshot meine Daten mit folgendem Script. Zusätzlich führe ich 2x die Woche einen Filesystemcheck auf /backup aus. Beises sind cronjobs.

#!/bin/bash
# snapshot
# make a snapshot from directories
# an create hourly/daily/monthly/yearly archieves
# ---------
# neobiker (2006)
# originally based on mirror Version 3.11 By Stu Sheldon stu@actusa.net
# ...
# ...
# The directories are named 'snapshot-<year>-<month>-<day>__<hour>'
#
# Each time snapshot runs, it date stamps a file in the <snapshot> directory called
# 'lastrun.time'. This file's date when listing it using ls -laF shows the last
# date and time snapshot ran on that host.
#
# The last thing you need to do is add snapshot in your crontab with the proper
# times and switches.
#
# If you are going for hourly sync's, add the following to your crontab:
# 0 * * * *    /usr/local/sbin/snapshot
#
# Every four hours would be:
# 0 0,4,8,12,16,20  * * *    /usr/local/sbin/snapshot
#
# you get the idea
# ...

#########################################################################
# Start User Config Stuff

# This is the local root of the backup directories. (where to backup to)
backuproot=/srv/backup
snapshot="srv-data"
snaphost=""

# You need to set the newdayhour to 00-23 depending on what hour you run
# the script in cron.
# 1 4,16,19 * * * /usr/local/sbin/snapshot-srv1
newdayhour=04

# number of snapshots to save
savehours=3
savedays=6
saveweeks=4
savemonths=12
saveyears=10

# list directories to backup
allsrcdir="
/home/
/srv/
/var/www/
"

# list directories to skip during in backup
# or leave blank
# EXCLUDES=""

EXCLUDES="--filter=. /tmp/excludes-${snapshot}"

cat > /tmp/excludes-${snapshot} << EOF
- lost+found
- /srv/backup
- /srv/snapshot
- /srv/devel
- /var/spool
+ *
EOF


# Mount and Dismount commands for all reasons are in the following functions
# you can also mount windows-shares via smbclient e.g.
# here: mount for writing during backup, and mount readonly afterwards

mounting ()
{
    precom=0
    mount -o remount,rw ${backuproot} || mountfail
}

umounting ()
{
    postcom=0
    mount -o remount,ro ${backuproot} || umountfail
}

# Typical Unix saving localhost
if [ -n "$snaphost" ]; then
    rsync="rsync -a -R -q --delete --numeric-ids -e ssh"
else
    rsync="rsync -a -R -q --delete"
fi

  # Typical Unix using ssh and public keys
  # rsync="rsync -aR -q  --numeric-ids -e ssh --delete"
  # This has settings for windows and expects to use a mounted share.
  # rsync="rsync -a -R -q --delete --modify-window=10"

# End User Config Stuff
############################################################################
# Start Static Code

cmdline=$1
PATH=/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin

 yeardir=${backuproot}/${snapshot}/yearly
monthdir=${backuproot}/${snapshot}/monthly
 weekdir=${backuproot}/${snapshot}/weekly
  daydir=${backuproot}/${snapshot}/daily
 hourdir=${backuproot}/${snapshot}/hourly

[ -d ${yeardir} ]  || mkdir -p ${yeardir}
[ -d ${monthdir} ] || mkdir -p ${monthdir}
[ -d ${weekdir} ]  || mkdir -p ${weekdir}
[ -d ${daydir} ]   || mkdir -p ${daydir}
[ -d ${hourdir} ]  || mkdir -p ${hourdir}

lockfile=${backuproot}/${snapshot}/syncing-now
snapdir=$hourdir/`date +'snapshot-%Y-%m-%d-%H'`

cmonth=`date +'%m'`
doweek=`date +'%w'`
  cday=`date +'%d'`
 chour=`date +'%H'`
 lhour=`/bin/ls -1r ${hourdir} | tail -1`
  lday=`/bin/ls -1r ${daydir}  | tail -1`
 lweek=`/bin/ls -1r ${weekdir} | tail -1`
lmonth=`/bin/ls -1r ${monthdir}| tail -1`

for dir in ${hourdir} ${daydir} ${weekdir} ${monthdir} ${yeardir}; do
  sync=`/bin/ls -1 ${dir} | tail -1`
  if [ -n "${sync}" ]; then
    syncdir=$dir/$sync
    break
  fi
done

dosync ()
{
    [ -d ${snapdir} ] || mkdir -p ${snapdir}
    [ -n "${syncdir}" ] && rsync="${rsync} --link-dest=${syncdir}"

    for srcdir in ${allsrcdir}; do
      if [ -n "$snaphost" ]; then
        srcdir=${snaphost}:${srcdir}
      fi
      ${rsync} "${EXCLUDES}" ${srcdir} ${snapdir}
    done
}

movesnap ()
{
    case "$1" in
        d)
           dir=$daydir
           nr=$savedays
           snap=$hourdir/$lhour
           ;;
        w)
           dir=$weekdir
           nr=$saveweeks
           snap=$daydir/$lday
           ;;
        m)
           dir=$monthdir
           nr=$savemonths
           snap=$weekdir/$lweek
           ;;
        y)
           dir=$yeardir
           nr=$saveyears
           snap=$monthdir/$lmonth
           ;;
        *)
           echo >&2 "movesnap: unknow option $1, aborting..."
           rm -f ${lockfile}
           rm -f /tmp/excludes-${snapshot}
           umounting
           exit 1
           ;;
    esac
    [ ${nr} -eq 0 ] && return
    if [ -d ${snap} ]; then
      mv ${snap} ${dir}
    else
      echo >&2 "I can't find snapshot ${snap}..."
    fi
}

docleanup ()
{
    [ ${savehours} -lt 1 ] && savehours=1
    count=0
    for i in `/bin/ls -r ${hourdir}`; do
      ((count++))
      if [ ${count} -gt ${savehours} ]; then
        rm -Rf ${hourdir}/${i}
      fi
    done

    count=0
    for i in `/bin/ls -r ${daydir}`; do
      ((count++))
      if [ ${count} -gt ${savedays} ]; then
        rm -Rf ${daydir}/${i}
      fi
    done

    count=0
    for i in `/bin/ls -r ${weekdir}`; do
      ((count++))
      if [ ${count} -gt ${saveweeks} ]; then
        rm -Rf ${weekdir}/${i}
      fi
    done

    count=0
    for i in `/bin/ls -r ${monthdir}`; do
      ((count++))
      if [ ${count} -gt ${savemonths} ]; then
        rm -Rf ${monthdir}/${i}
      fi
    done

    count=0
    for i in `/bin/ls -r ${yeardir}`; do
      ((count++))
      if [ ${count} -gt ${saveyears} ]; then
        rm -Rf ${yeardir}/${i}
      fi
    done
}

inuse ()
{
  echo >&2 "I am already syncing snapshot ${snapshot}"
  exit 1
}

mountfail ()
{
  echo >&2 "I can't mount filesystem ${backuproot}"
  exit 1
}

umountfail ()
{
  echo >&2 "I can't unmount filesystem ${backuproot}"
  exit 1
}

####
#### Programm starts here ... ###########################
####

[ -f ${lockfile} ] && inuse
mounting
touch ${lockfile}

case ${cmdline} in
  -y)
    dosync
    movesnap "y"
    docleanup
    ;;
  -m)
    dosync
    movesnap "m"
    docleanup
    ;;
  -w)
    dosync
    movesnap "w"
    docleanup
    ;;
  -d)
    dosync
    movesnap "d"
    docleanup
    ;;
  --scalix)
    ombackup
    ;;
  *)
    dosync
    if [ ${newdayhour} -eq ${chour} ]; then
      movesnap "d"
      if [ ${doweek} -eq 0 ]; then
        movesnap "w"
      fi
      if [ ${cday} -eq 01 ]; then
        movesnap "m"
        if [ ${cmonth} -eq 01 ]; then
          movesnap "y"
        fi
      fi
    fi
    docleanup
  ;;
esac

touch ${backuproot}/${snapshot}/lastrun.time
rm -f ${lockfile}
rm -f /tmp/excludes-${snapshot}

umounting
echo "Backup for ${snapshot} is complete..."