Backup auf WebDAV Server (hier:GMX Mediacenter)

daralla

New Member
Hallo,

ich bin kürzlich auf ein FTP-Backup Script von Grafx (Hersteller u.a. von Powertoys für Plesk) gestoßen. Da ich aber einerseits keinen geeigneten kostenlosen FTP-Anbieter gefunden habe, andererseits aber sowieso einen GMX Pro Mailaccount mit 5GB WebDAV Speicher besitze, habe ich das Grafx (FTP-)Script für WebDAV angepasst (stark vereinfacht).

Voraussetzung ist die Installation von "cadaver" einem command line WebDAV client ähnlich FTP - gibts hier

Der Zugriff per cadaver muß unbedingt reibungslos funktionieren, insbesondere auch der automatische Login via .netrc, sonst hat alles weitere keine Zweck. Ist alles recht einfach in den manpages von cadaver beschrieben.

Das original script benutzt für den Zugriff das Programm lftp, cadaver ist vergleichsweise simpler gestrickt. Um das Script für ein tägliches Backup zu nutzen, müssen daher auf dem WebDAV Server zunächst einige Verzeichnisse angelegt werden, im Beispiel ist das "Ober"-Backup-Verzeichnis "/VSBackup". Darin wird dann je ein Unterverzeichnis pro Wochentag angelegt: "Monday", "Tuesday", usw.
Das Script legt dann für jeden Wochentag eine Sicherung im entsprechenden Unterverzeichnis an und löscht die Sicherung der Vorwoche, so sind immer 7 Backups vorrätig.

Die Variablen des Scripts müssen zwingend an die eigenen Bedürfnisse angepasst werden, ist alles im script selbst dokumentiert (da die original comments dort in englisch waren, habe ich meine Änderungen auch weiter in englisch dokumentiert).

Das Original-Script hatte auch eine Restore-Funktion, für deren Anpassung reichen meine Kenntnisse aber nicht aus :confused: ;).

Jedenfalls funktioniert das angepasste Script bei mir (STRATO V-Server A, SUSE 10.1, Plesk 8.2) sehr gut, vielleicht kann das ja noch jemand gebrauchen (oder gern auch verbessern :D)

(file: webdav-backup)
Code:
#!/bin/bash
#
# Remote WebDAV Backup
# based on original script (for FTP backup) from:
# Copyright (c) 1998-2006 by GraFX, ltd
# Visit us at http://www.web-hosting-control-panel-addons.com/
# All rights reserved
#
# adjusted for WebDAV functionality
#
#=====================================================================
# Set the following variables to your system needs
#=====================================================================

### REQUIREMENTS
### WebDAV account somewhere
### in this example a GMX Mediacenter account (5GB) is used
### "cadaver", a WebDAV command line client similar to FTP
### cadaver has to be configured for automatic login to you WebDAV account,
### via "/root/.netrc" file - see man page of cadaver

### INSTALLATION NOTES
### Create directory ex: /private-backup/backup_script
### Copy the webdav-backup file in /private-backup/backup_script
### Edit /private-backup/backup_script/webdav-backup and fill in the variables
### chmod 700 /private-backup/backup_script/webdav-backup
### Setup DIRS, WebDAV Server details, EMAILID, and other stuffs.
### Copy webdav-backup-cron to /etc/cron.daily for daily execution
### chmod 700 /etc/cron.daily/webdav-backup-cron

### working directory of the script
ROOTDIR="/private-backup/backup_script/"

### directories to include in backup ###
### /etc - all config files
### /var/qmail/ - QMAIL Home
### /srv/www/vhosts
### /private-backup/mysql-backup/daily - database backup directory
### /root - root's files

DIRS="/etc /root /srv/www/vhosts /var/qmail /private-backup/mysql-backup/daily" ## Add directories with spaces.

### EXCLUDES subdirectories within $DIRS which NOT to backup###
### in this example the "/root/psa" directory
EXCLUDES=--exclude=psa

### WebDAV server Setup ###
### set path to the Webdav directory (VSBAckup/<day of week>.
### in this example we need to have a subdir for every weekday (Monday to Sunday)
### backup is done according to current weekday to the corresponding directory
### directories have to exist!
DAVD="/VSBackup/$(date "+%A")" ### set path to the Webdav directory (VSBAckup/<day of week>.
### adress of remote server
DAVS="https://mediacenter.gmx.net"

### Other stuff ###
NOTIFICATION="yes" ## yes / no - setup this if you want to get email or not.
EMAILID="<your-email-adress>" ### your email here, where program will send info

####################################################
########### DO NOT EDIT LINES BELOW ################
####################################################

###	Some other stuff, edit only if you know what you do	###
TMPFILE=$TEMPDIR"tmp.txt"
TEMPDIR=$ROOTDIR"tmp/"
RESTOREDIR=$ROOTDIR"restore/"
BACKUP=$TEMPDIR"backup.$$"
NOW=$(date +"%Y%m%d")

###	FUNCTIONS	###
## root user cheking for security reasons
check_root() {
   if [ "$UID" != "0" ]; then
      echo "You must be root to run this script!"
      exit 0
   fi
}

## Mail sending module
start_mail(){

if [ "$NOTIFICATION" == "yes" ]; then
 T=/tmp/backup.fail
 echo "Date: $(date)">$T
 echo "Hostname: $(hostname)" >>$T
 echo "Remote hostname: $DAVS" >>$T
 echo "Folders: $DIRS" >>$T
 echo "Put to: $DAVD" >>$T

 mail  -s "Backup START $(hostname) to $DAVS" "$EMAILID" <$T
 rm -f $T
fi

}

## Will work this module local, tar etc.
local_temporary_backup(){

### Start Backup for file system ###
[ ! -d $BACKUP ] && mkdir -p $BACKUP || :

### See if we want to make a full backup ###
FILE="BACKUP_""$(hostname | tr . _ )_"$NOW".tar.gz"

tar -zcf $BACKUP/$FILE $EXCLUDES $DIRS  2> /dev/null

FILESIZE=`du -h "$BACKUP/$FILE" | cut -f1`

}

## Will put file to remote server
ftp_dump(){

### Dump backup using FTP ###
#Start FTP backup using cadaver
/usr/local/bin/cadaver $DAVS<<EOF
cd $DAVD
rm *
lcd $BACKUP
mput *
quit
EOF

### Find out if WebDAV backup failed or not ###
if [ "$?" == "0" ]; then
 rm -f $BACKUP/*
 if [ "$NOTIFICATION" == "yes" ]; then
  T=/tmp/backup.fail
  echo "Date: $(date)">$T
  echo "Hostname: $(hostname)" >>$T
  echo "Remote hostname: $DAVS" >>$T
  echo "Folders: $DIRS" >>$T
  echo "Put to: $DAVD" >>$T
  echo "Size of $FILE = $FILESIZE bytes." >>$T

  mail  -s "Backup END $(hostname) to $DAVS SUCCESS" "$EMAILID" <$T
  rm -f $T
 fi
else
  T=/tmp/backup.fail
  echo "Date: $(date)">$T
  echo "Hostname: $(hostname)" >>$T
  echo "Remote hostname: $DAVS" >>$T
  echo "Folders: $DIRS" >>$T
  echo "Put to: $DAVD" >>$T
  echo "Operation failed" >>$T

  mail  -s "Backup END $(hostname) to $DAVS FAILED" "$EMAILID" <$T
  rm -f $T

fi

rmdir --ignore-fail-on-non-empty $BACKUP

}

###	END FUNTIONS	###
 
## Perform the root checking

check_root

###	tempdir	###
[ ! -d $TEMPDIR ] && mkdir -p $TEMPDIR || :

case "$1" in
--backup)

### STARTING MAIL  ###
start_mail

###	LOCAL BACKUP	###
local_temporary_backup

###	FTP DUMP	###
ftp_dump

;;

--help)
echo "--backup		: start the backup from your local server to the"
echo "			  remote server"
echo "--help 		: this message "
;;
*)
   echo "Error: Missing or invalid option"
   echo "Use --help to view available options"
   ;;
esac

exit;

Ergänzung / Änderung:
- Pfad zum "cadaver" Programm in das Skript eingefügt
- der Cronjob sollte besser direkt in Plesk (Server->Scheduled Tasks) für den Benutzer root angelegt werden, z.B.
für tägliches Backup um 04:28:
Minute: 28
Hour: 4
Day of Month: *
Month: *
Day of Week: *
Command: /private-backup/backup_script/webdav-backup --backup
 
Last edited by a moderator:
Back
Top