Scripts and other things

fstab

ls -al /dev/disk/by-uuid
shows uuid keys for use in etc/fstab

eMail file systems disc space usage

Note: email (postfix, sendmail, …) has to be setup correctly

#!/bin/bash
/bin/df -h | /usr/bin/mail mail@example.org -s Disk-Space

Backup with tar

#! /bin/bash
tar -czf /home/semok/Backups/websites_$(date +%F_%H:%M:%S).tar.gz /home/websites --exclude "*.jpg" --exclude "*.JPG"
exit 0

Backup with rsync/cpio

Following scripts where found on the Interweb. Kudos to the original engineer, which sadly I can neither name nor ask, if he is ok with this publishing. But as it is mainly a clever way to do stuff, which all the experts know right out of the top of their heads, I post it anyways:

rsync_cycle.zsh

For description read the comments inside the script. Note: For me it works perfect with the target being on a NFS share.

#!/bin/zsh
# ** rsync_cycle.zsh
# Version 0.1
#
# Script to create a single LOCAL snapshot backup of my
# most recent hourly snapshot. This script assumes we're
# running on the machine on which the hourly backups
# reside, so remote access to anywhere is not required
#
# <cmd>  <target directory> <number> <week|day> ["debug"]
#
# <target directory> is the path name of the backup
# directory, without trailing "/". Hourly
# snapshots are presumed to be of the form
# <target>.0, <target>.1, etc;  daily backups are
# in <target>.day.0, <target>.day.1, etc; weekly
# have the form <target>.week.0, etc.
#
# 2nd param indicates the number of snapshots we are
# keeping at this level
#
# 3rd parameter indicates whether to update the daily from
# the hourly, or the weekly from the hourly snapshots
#
# (if optional 4th parameter is "debug" then script issues
# warnings if there are errors, otherwise # script is silent)
#
# This tool is presumed to be used in conjunction with
# a separate 'rsync' process that keeps the hourly.0
# snapshot up to date. We are called with "hour" to
# rotate hourly.1 through hourly.N, but when called
# with "day" or "week" we rotate day.0 through day.N
# (or week.0 to week.N) respectively and then copy
# over hourly.1 into day.0 or week.0 as required.
# [NB. To avoid potential race conditions with any
# long running, concurrent hourly update, the daily
# and weekly rotations copy the *previous* hour's
# snapshot, hourly.1, rather than the current one
# at hourly.0 which might be still being updated by
# rsync under extreme conditions.]
#
# The trick is that we use "cpio" to copy links rather
# than actual file contents. This minimises disk space
# usage. Subsequent use of 'rsync' ensures new file
# storage is only allocated for files that change from
# one hourly snapshot to the next. (The rsync is currently
# handled outside this program.)
##########

if [[ $1 == "" ]]; then
        echo Missing target directory
        exit 1
fi
TARGET_DIR=$1

if [[ $2 == "" ]]; then
        echo Missing number of rotations
        exit 1
fi

if [[ $2 -lt 1 ]] || [[ $2 -gt 6 ]]; then
        echo Rotations must be between 1 and 6
        exit 1
fi
ROTATIONS=$2

WHENTIME=$3
if [[ $WHENTIME == "weekly" ]]; then
        TARGET=${TARGET_DIR}.week
        TARGET_H=${TARGET_DIR}.1
        ENDCNT=0
else 
	if [[ $WHENTIME == "daily" ]]; then
        TARGET=${TARGET_DIR}.day
        TARGET_H=${TARGET_DIR}.1
        ENDCNT=0
	else 
		if [[ $WHENTIME == "hourly" ]]; then
        TARGET=${TARGET_DIR}
        TARGET_H=${TARGET_DIR}.0
        ENDCNT=1
		else
        echo Problem with 3rd parameter $WHENTIME - must be weekly, daily, or hourly
        exit 1
		fi
	fi
fi

DEBUG=$4

#########
# First sanity check that there's actually an hourly snapshot
# that we can copy from in the first place (although if we're
# doing an hourly snapshot, just create an empty $TARGET_H on
# the assumption that it is about to be filled in anyway.)
######
if [[ ! -e $TARGET_H ]]; then
 if [[ $WHENTIME == "hourly" ]]; then
         mkdir -p $TARGET_H
 else
        if [[ $DEBUG == "debug" ]]; then
					echo Hourly snapshot $TARGET_H does not exist
				fi
        exit 1
 fi
fi

if [[ $DEBUG == "debug" ]]; then
        echo `date`
        echo Rotating $WHENTIME snapshots.
fi

END=$2

[[ -e ${TARGET}.$END ]] && rm -rf ${TARGET}.$END

while ( [[ $END -gt $ENDCNT ]] ); do
        END_LESS_ONE=$((END - 1))
        if [[ -e ${TARGET}.$END_LESS_ONE ]]; then 
					mv ${TARGET}.$END_LESS_ONE ${TARGET}.$END
				fi
        END=$END_LESS_ONE
done

#########
# Explicitly re-create an empty place to copy the current hourly snapshot
######
mkdir -p ${TARGET}.${ENDCNT}

#########
# Create a copy of the directory entries in ${TARGET_H}
# but do not create independent copies of the files themselves
# (this line is roughly equivalent to the GNU 'cp -al <src> <dst>')
######
if [[ $DEBUG == "debug" ]]; then
		( cd $TARGET_H && find . -print | cpio -dplm ${TARGET}.${ENDCNT} )
else
   ( cd $TARGET_H && find . -print | cpio -dplm ${TARGET}.${ENDCNT} ) >& /dev/null
fi

if [[ $DEBUG == "debug" ]]; then
	echo Rotation completed `date`
fi

exit 0

rsync_backup.zsh

#!/bin/zsh
#
# Backups are stored on $TARGET_HOST under a sequence of
# directories with prefix $TARGET_DIR
#
#  Hourly: ${TARGET_DIR}.0, ${TARGET_DIR}.1, etc
#  Daily:  ${TARGET_DIR}.day.0, ${TARGET_DIR}.day.1, etc
#  Weekly: ${TARGET_DIR}.week.0, ${TARGET_DIR}.week.1, etc
#
# Command line options:
#
# cmd  <hourly|daily|weekly> [debug]
#
# We rely on rsync_cycle to perform snapshot directory
# rotations on the remote machine. Yes, this could be done by
# the remote machine, but triggering it here ensures everything
# stops if this local machine dies for some period of time
#

# Validate command line option
WHENTIME=$1
if [[  $WHENTIME != "weekly"  && $WHENTIME != "daily" &&  $WHENTIME != "hourly"  ]];  then
        echo Problem with 3rd parameter $WHENTIME - must be weekly, daily, or hourly
        exit 1
fi

DEBUG=$2

# Demonstrates how to use rsync to back up a directory tree from a local
# machine to a remote machine.  Then re-run the script, as needed, to keep
# the two machines "in sync."  It only copies new or changed files and ignores
# identical files.

# Destination host machine name ()
TARGET_HOST="localhost"

# User that rsync will connect as
# Are you sure that you want to run as root, though?
USER="bak"

# Directory to copy from on the source machine.
SRCDIR="/home/semok"
# Directory to copy to on the destination machine (NFS is mounted there in this example)
DESTDIR="/media/backup"

# excludes file - Contains wildcard patterns of files to exclude.
# i.e., *~, *.bak, etc.  One "pattern" per line.
# You must create this file.
EXCLUDES=/home/semok/rsync_exclude.txt

# Options.
# -n Don't do any copying, but display what rsync *would* copy. For testing.
# -a Archive. Mainly propogate file permissions, ownership, timestamp, etc.
# -u Update. Don't copy file if file on destination is newer.
# -v Verbose -vv More verbose. -vvv Even more verbose.
# See man rsync for other options.

# For testing.  Only displays what rsync *would* do and does no actual copying.
#OPTS="-n -vv -u -a --rsh=ssh --exclude-from=$EXCLUDES --stats --progress"

# May be needed if run by cron?
#export PATH=$PATH:/bin:/usr/bin:/usr/local/bin

# Only run rsync if $TARGET_HOST responds.
VAR=`ping -s 1 -c 1 $TARGET_HOST > /dev/null; echo $?`
if [[ $VAR -eq 0 ]]; then
        rsync -buaz --exclude-from=$EXCLUDES --stats --stats --force --delete --delete-excluded --human-readable --no-p --no-g --chmod=ugo=rwX $SRCDIR $DESTDIR.0/ 2> /dev/null
# Output of disk usage. ts-xelddc is the machine which hosts the NFS share
  echo ''
  df -h | egrep 'ts-xelddc' -A 1
else
    echo Cannot connect to $TARGET_HOST.
fi


exclude.txt

someDir/
somefile.dat

+ .mozilla/
+ .psi/
+ .ssh/

- Cache/*
- /semok/SomeDir/
- /semok/.*/
- /lost+found
scripts.txt · Last modified: 2011/03/04 15:12 by semok
CC Attribution-Noncommercial-Share Alike 3.0 Unported chimeric.de = chi`s home Creative Commons License Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0