Snapshot-RSync: Unterschied zwischen den Versionen
Zur Navigation springen
Zur Suche springen
Keine Bearbeitungszusammenfassung |
Keine Bearbeitungszusammenfassung |
||
Zeile 33: | Zeile 33: | ||
# 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=/srv/backup | backuproot=/srv/backup | ||
snapshotdir="srv-data" | |||
snaphost="" | snaphost="" | ||
# | # archivehour: hour (2 digits) when the snapshot will be moved to archive dirs | ||
# | # daily, weekly, monthly, yearly | ||
archivehour=04 | |||
# number of snapshots to | # number of snapshots to archive | ||
savehours=3 | savehours=3 | ||
savedays=6 | savedays=6 | ||
Zeile 52: | Zeile 51: | ||
/home/ | /home/ | ||
/srv/ | /srv/ | ||
/var/ | /var/spool/ | ||
" | " | ||
Zeile 59: | Zeile 58: | ||
# EXCLUDES="" | # EXCLUDES="" | ||
EXCLUDES="--filter=. /tmp/excludes-${ | EXCLUDES="--filter=. /tmp/excludes-${snapshotdir}" | ||
cat > /tmp/excludes-${ | cat > /tmp/excludes-${snapshotdir} << EOF | ||
- lost+found | - lost+found | ||
- /srv/backup | - /srv/backup | ||
- /srv/snapshot | - /srv/snapshot | ||
- /srv/daten/client-backups | |||
- /srv/daten/friedrich/Daten/Musik/ | |||
- /srv/devel | - /srv/devel | ||
- /var/spool | - /var/spool/squid | ||
- /var/spool/samba | |||
+ * | + * | ||
EOF | EOF | ||
# End User Config Stuff | # End User Config Stuff | ||
############################################################################ | ############################################################################ | ||
# Start Static Code | # Start Static Code | ||
PATH=/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin | |||
cmdline=$1 | cmdline=$1 | ||
lockfile=${backuproot}/${snapshotdir}/syncing-now | |||
# Typical Unix saving localhost | |||
rsync="rsync -a -R -q --delete" | |||
[ -n "${snaphost}" ] && rsync="${rsync} --numeric-ids -e ssh" | |||
cmonth=`date +'%m'` | |||
doweek=`date +'%w'` | |||
cday=`date +'%d'` | |||
yeardir=${backuproot}/${snapshotdir}/yearly | |||
monthdir=${backuproot}/${snapshotdir}/monthly | |||
weekdir=${backuproot}/${snapshotdir}/weekly | |||
daydir=${backuproot}/${snapshotdir}/daily | |||
hourdir=${backuproot}/${snapshotdir}/hourly | |||
snap=`date +'snapshot-%Y-%m-%d-%H'` | |||
snapdir=${hourdir}/${snap} | |||
for dir in ${hourdir} ${daydir} ${weekdir} ${monthdir} ${yeardir}; do | for dir in ${snapdir} ${hourdir} ${daydir} ${weekdir} ${monthdir} ${yeardir}; do | ||
[ -d ${dir} ] || mkdir -p ${dir} | |||
done | done | ||
dosync () | dosync () | ||
{ | { | ||
# search a snapshot-base for rsync | |||
for dir in ${hourdir} ${daydir} ${weekdir} ${monthdir} ${yeardir}; do | |||
sync=`/bin/ls -1 ${dir} | tail -1` | |||
if [ -n "${sync}" ]; then | |||
rsync="${rsync} --link-dest=${dir}/${sync}" | |||
break | |||
fi | |||
done | |||
touch $lockfile | |||
for srcdir in ${allsrcdir}; do | for srcdir in ${allsrcdir}; do | ||
if [ -n "$snaphost" ]; then | if [ -n "${snaphost}" ]; then | ||
srcdir=${snaphost}:${srcdir} | srcdir=${snaphost}:${srcdir} | ||
fi | fi | ||
${rsync} "${EXCLUDES}" ${srcdir} ${snapdir} | ${rsync} "${EXCLUDES}" ${srcdir} ${snapdir} | ||
done | done | ||
rm -f $lockfile | |||
} | } | ||
Zeile 157: | Zeile 130: | ||
dir=$daydir | dir=$daydir | ||
nr=$savedays | nr=$savedays | ||
from=$hourdir | |||
;; | ;; | ||
w) | w) | ||
dir=$weekdir | dir=$weekdir | ||
nr=$saveweeks | nr=$saveweeks | ||
snap=$ | from=$daydir | ||
if [ -e "${from}/`date +'snapshot-%Y-%m-%d-' -d '1 day ago'`$archivehour" ]; then | |||
snap=`date +'snapshot-%Y-%m-%d-' -d '1 day ago'`$archivehour | |||
else | |||
snap=`/bin/ls -1 ${from} | tail -1` | |||
fi | |||
;; | ;; | ||
m) | m) | ||
dir=$monthdir | dir=$monthdir | ||
nr=$savemonths | nr=$savemonths | ||
snap=$ | from=$weekdir | ||
if [ -e "${from}/`date +'snapshot-%Y-%m-%d-' -d '1 week ago'`$archivehour" ]; then | |||
snap=`date +'snapshot-%Y-%m-%d-' -d '1 week ago'`$archivehour | |||
else | |||
snap=`/bin/ls -1 ${from} | tail -1` | |||
fi | |||
;; | ;; | ||
y) | y) | ||
dir=$yeardir | dir=$yeardir | ||
nr=$saveyears | nr=$saveyears | ||
snap=$ | from=$monthdir | ||
if [ -e "${from}/`date +'snapshot-%Y-%m-%d-' -d '1 month ago'`$archivehour" ]; then | |||
snap=`date +'snapshot-%Y-%m-%d-' -d '1 month ago'`$archivehour | |||
else | |||
snap=`/bin/ls -1 ${from} | tail -1` | |||
fi | |||
;; | ;; | ||
*) | *) | ||
echo >&2 "movesnap: unknow option $1, aborting..." | echo >&2 "movesnap: unknow option $1, aborting..." | ||
rm -f ${lockfile} | rm -f ${lockfile} | ||
rm -f /tmp/excludes-${ | rm -f /tmp/excludes-${snapshotdir} | ||
umounting | umounting | ||
exit 1 | exit 1 | ||
;; | ;; | ||
esac | esac | ||
[ ${nr} -eq 0 ] && return | [ ${nr} -eq 0 ] && return | ||
if [ -d ${snap} ]; then | |||
mv ${snap} ${dir} | if [ -d ${from}/${snap} ]; then | ||
mv ${from}/${snap} ${dir} | |||
ln -s ${dir}/${snap} ${from} | |||
else | else | ||
echo >&2 "I can't find snapshot ${snap}..." | echo >&2 "I can't find snapshot ${from}/${snap}..." | ||
fi | fi | ||
} | } | ||
Zeile 236: | Zeile 227: | ||
inuse () | inuse () | ||
{ | { | ||
echo >&2 "I am already syncing snapshot ${ | echo >&2 "I am already syncing snapshot ${snapshotdir}" | ||
exit 1 | exit 1 | ||
} | } | ||
Zeile 250: | Zeile 241: | ||
echo >&2 "I can't unmount filesystem ${backuproot}" | echo >&2 "I can't unmount filesystem ${backuproot}" | ||
exit 1 | exit 1 | ||
} | |||
mounting () | |||
{ | |||
mount -o remount,rw ${backuproot} || mountfail | |||
} | |||
umounting () | |||
{ | |||
mount -o remount,ro ${backuproot} || umountfail | |||
} | } | ||
Zeile 258: | Zeile 259: | ||
[ -f ${lockfile} ] && inuse | [ -f ${lockfile} ] && inuse | ||
mounting | mounting | ||
case ${cmdline} in | case ${cmdline} in | ||
--scalix) # backup Scalix Mailserver | |||
ombackup # start remote-Backup | |||
;; | |||
-y) | -y) | ||
dosync | dosync | ||
Zeile 281: | Zeile 284: | ||
docleanup | docleanup | ||
;; | ;; | ||
*) | |||
dosync | dosync | ||
if [ ${ | if [ ${archivehour} -eq `date +'%H'` ]; then | ||
movesnap "d" | movesnap "d" | ||
if [ ${doweek} -eq 0 ]; then | if [ ${doweek} -eq 0 ]; then | ||
Zeile 299: | Zeile 299: | ||
fi | fi | ||
docleanup | docleanup | ||
;; | |||
esac | esac | ||
touch ${backuproot}/${ | touch ${backuproot}/${snapshotdir}/lastrun.time | ||
rm -f /tmp/excludes-${snapshotdir} | |||
rm -f /tmp/excludes-${ | |||
umounting | umounting | ||
echo "Backup for ${ | echo "Backup for ${snapshotdir} is complete..." | ||
</pre> | </pre> |
Version vom 26. April 2011, 17:01 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 (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 snapshotdir="srv-data" snaphost="" # archivehour: hour (2 digits) when the snapshot will be moved to archive dirs # daily, weekly, monthly, yearly archivehour=04 # number of snapshots to archive savehours=3 savedays=6 saveweeks=4 savemonths=12 saveyears=10 # list directories to backup allsrcdir=" /home/ /srv/ /var/spool/ " # list directories to skip during in backup # or leave blank # EXCLUDES="" EXCLUDES="--filter=. /tmp/excludes-${snapshotdir}" cat > /tmp/excludes-${snapshotdir} << EOF - lost+found - /srv/backup - /srv/snapshot - /srv/daten/client-backups - /srv/daten/friedrich/Daten/Musik/ - /srv/devel - /var/spool/squid - /var/spool/samba + * EOF # End User Config Stuff ############################################################################ # Start Static Code PATH=/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin cmdline=$1 lockfile=${backuproot}/${snapshotdir}/syncing-now # Typical Unix saving localhost rsync="rsync -a -R -q --delete" [ -n "${snaphost}" ] && rsync="${rsync} --numeric-ids -e ssh" cmonth=`date +'%m'` doweek=`date +'%w'` cday=`date +'%d'` yeardir=${backuproot}/${snapshotdir}/yearly monthdir=${backuproot}/${snapshotdir}/monthly weekdir=${backuproot}/${snapshotdir}/weekly daydir=${backuproot}/${snapshotdir}/daily hourdir=${backuproot}/${snapshotdir}/hourly snap=`date +'snapshot-%Y-%m-%d-%H'` snapdir=${hourdir}/${snap} for dir in ${snapdir} ${hourdir} ${daydir} ${weekdir} ${monthdir} ${yeardir}; do [ -d ${dir} ] || mkdir -p ${dir} done dosync () { # search a snapshot-base for rsync for dir in ${hourdir} ${daydir} ${weekdir} ${monthdir} ${yeardir}; do sync=`/bin/ls -1 ${dir} | tail -1` if [ -n "${sync}" ]; then rsync="${rsync} --link-dest=${dir}/${sync}" break fi done touch $lockfile for srcdir in ${allsrcdir}; do if [ -n "${snaphost}" ]; then srcdir=${snaphost}:${srcdir} fi ${rsync} "${EXCLUDES}" ${srcdir} ${snapdir} done rm -f $lockfile } movesnap () { case "$1" in d) dir=$daydir nr=$savedays from=$hourdir ;; w) dir=$weekdir nr=$saveweeks from=$daydir if [ -e "${from}/`date +'snapshot-%Y-%m-%d-' -d '1 day ago'`$archivehour" ]; then snap=`date +'snapshot-%Y-%m-%d-' -d '1 day ago'`$archivehour else snap=`/bin/ls -1 ${from} | tail -1` fi ;; m) dir=$monthdir nr=$savemonths from=$weekdir if [ -e "${from}/`date +'snapshot-%Y-%m-%d-' -d '1 week ago'`$archivehour" ]; then snap=`date +'snapshot-%Y-%m-%d-' -d '1 week ago'`$archivehour else snap=`/bin/ls -1 ${from} | tail -1` fi ;; y) dir=$yeardir nr=$saveyears from=$monthdir if [ -e "${from}/`date +'snapshot-%Y-%m-%d-' -d '1 month ago'`$archivehour" ]; then snap=`date +'snapshot-%Y-%m-%d-' -d '1 month ago'`$archivehour else snap=`/bin/ls -1 ${from} | tail -1` fi ;; *) echo >&2 "movesnap: unknow option $1, aborting..." rm -f ${lockfile} rm -f /tmp/excludes-${snapshotdir} umounting exit 1 ;; esac [ ${nr} -eq 0 ] && return if [ -d ${from}/${snap} ]; then mv ${from}/${snap} ${dir} ln -s ${dir}/${snap} ${from} else echo >&2 "I can't find snapshot ${from}/${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 ${snapshotdir}" exit 1 } mountfail () { echo >&2 "I can't mount filesystem ${backuproot}" exit 1 } umountfail () { echo >&2 "I can't unmount filesystem ${backuproot}" exit 1 } mounting () { mount -o remount,rw ${backuproot} || mountfail } umounting () { mount -o remount,ro ${backuproot} || umountfail } #### #### Programm starts here ... ########################### #### [ -f ${lockfile} ] && inuse mounting case ${cmdline} in --scalix) # backup Scalix Mailserver ombackup # start remote-Backup ;; -y) dosync movesnap "y" docleanup ;; -m) dosync movesnap "m" docleanup ;; -w) dosync movesnap "w" docleanup ;; -d) dosync movesnap "d" docleanup ;; *) dosync if [ ${archivehour} -eq `date +'%H'` ]; 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}/${snapshotdir}/lastrun.time rm -f /tmp/excludes-${snapshotdir} umounting echo "Backup for ${snapshotdir} is complete..."