echobBlog

Reinwerfschachtel [Update]

Dropbox ist ein großartiges Programm und das gesamte Projekt wirkt extrem sympathisch auf mich, auch wenn es ein Internetdienst ist, dem ich Daten anvertraue. Da es vielen schon ein Begriff ist, möchte ich hier nicht genauer auf die Funktionsweise eingehen. Denjenigen, die es noch nicht kennen, kann ich es nur empfehlen. Es ist eine großartige Lösung für einfaches Filesharing und für Backups. Da ich aber ein sehr paranoider Mensch bin – zumindest was Daten angeht – ist mir letzteres zu unsicher. Ich zweifle nicht an der Technologie die hinter Dropbox steht, sondern an mir selbst. Ich neige dazu wichtige Daten in meine Dropbox zu legen, da so ein Ausfall eines Rechners egal ist und ich die Daten auf anderen Rechnern wieder Abrufen kann. Aber wenn ich etwas in meinem Dropbox-Ordner lösche ist es weg und zwar überall … Als, wie erwähnt, paranoider Mensch verwende ich auch Time Machine auf meinem MacBook (die auf meinen Server sichert), allerdings wünsche ich mir noch ein Backup, da ich bei Ausfall meines (einzigen) Mac-Rechners keinen zugriff auf die Daten mehr habe. Um dieses Problem zu lösen habe ich mir einige Skripte gebastelt. Damit ich auf meinem Server überhaupt Dropbox verwenden kann bin ich auf die CLI-Variante des Programms angewiesen. Und in diesem Zusammenhang muss ich zuerst Danke sagen: Danke an Dropbox, dass sie ihre Software auch als CLI zur Verfügung stellen, was in meinen Augen alles andere als selbstverständlich ist. Ein weiteres Danke geht an die Community die echt tolle Python-Skripte zur Steuerung der CLI-Dropbox zusammengebaut hat und die Wiki-Seite dafür pflegt. Nun zu meinen Skripten: backup.sh wird stündlich von Cron(job) aufgerufen. backup2.sh im Moment alle 2 Stunden (kann allerdings einfach geändert werden). Die Skripte sind teilweise schlecht und schnell geschrieben und auch nur für meinen Server angepasst. Theoretisch sollte es aber reichen die Variablen am Anfang zu ändern und es in Crontab einzutragen. Update Also nochmal eine kurze Erklärung zu dem Zeugs unten. backup.sh macht hauptsächlich wöchentliche Backups und backup2.sh in bestimmten Abständen. delete.sh dient zum löschen von alten Backups. backup.sh
#!/bin/sh

#Variables
DIR="/media/Data0/Dropbox-Backup/" #Path for backups
FILE="$DIR""backup.date" #Path to the date-file
DROPBOXDIR="/root/Dropbox/" #Path to the Dropbox
LOGFILE="$DIR""/backup.log" #Path to the log-file
DATE=`date +%Y-%m-%d` #Get the date

if [ -f $FILE ]; then #look for the date-file
INPUT=`cat $FILE` #read the last backup-date
DIFF=`dateDiff -d $INPUT $DATE` # comparte the actual date with the last backup-date
if test $DIFF -ge 7; then #look if the last backup was 7 days ago
echo `date`" Backup" >> $LOGFILE
backup #do a backup
else
echo `date`" no Backup" >> $LOGFILE
fi
else #if there is no date-file
touch $file #create the file
echo $DATE > $FILE #write the date to the date-file
backup #dobackup
fi

#Methods

#create backup as tar.bz2 and save the date
#and call the delete.sh script
backup(){
mkdir "$DIR""$DATE"
cp -R "$DROPBOXDIR" "$DIR""$DATE"
tar -jcvf "$DIR""$DATE.tar.bz2" "$DIR""$DATE"
rm -R "$DIR""$DATE"
rm $FILE
touch $FILE
echo $DATE > $FILE
"$DIR""delete.sh"
}

#methods for time comparison, i did not write them myself
#they work but they give an error - i have to rewrite it
date2stamp () {
    date --utc --date "$1" +%s
}

stamp2date (){
    date --utc --date "1970-01-01 $1 sec" "+%Y-%m-%d %T"
}

dateDiff (){
    case $1 in
        -s)   sec=1;      shift;;
        -m)   sec=60;     shift;;
        -h)   sec=3600;   shift;;
        -d)   sec=86400;  shift;;
        *)    sec=86400;;
    esac
    dte1=$(date2stamp $1)
    dte2=$(date2stamp $2)
    diffSec=$((dte2-dte1))
    if (($diffSec < 0)); then abs=-1; else abs=1; fi
    echo $(($diffSec/sec*abs))
}

backup2.sh
#!/bin/bash

#Copy all back-scipts to the dropbox folder
#i have hardcoded it - if you want to change it do it here^^
cp /media/Data0/Dropbox-Backup/backup.sh /root/Dropbox/Documents/Scripts/backup.sh
cp /media/Data0/Dropbox-Backup/backup.date /root/Dropbox/Documents/Scripts/backupDate.txt
cp /media/Data0/Dropbox-Backup/backup.log /root/Dropbox/Documents/Scripts/backup.log
cp /media/Data0/Dropbox-Backup/backup2.sh /root/Dropbox/Documents/Scripts/backup2.sh
cp /media/Data0/Dropbox-Backup/delete.sh /root/Dropbox/Documents/Scripts/delete.sh

#Variables
FILE="/root/Dropbox/.sync" #File in which you name the files for additional backups
DBPATH="/root/Dropbox/" #Path to dropbox-folder
BACKUPPATH="/media/Data0/Dropbox-Backup/SecureBackup/" #Path to backup-folder
DATE=$(date +%Y-%m-%d) #date of the day
TIME=$(date +%k-%M-%S) #actual time
LATEST="/media/Data0/Dropbox-Backup/SecureBackup/LATEST" #path to the file, which saves the date of the latest backup

for LINE in `cat $FILE` # iterate over the backup-file
do

#look if folder named with the date already exists
#otherwise create it
if [ -d $BACKUPPATH$DATE ]; then
#echo "Folder already exists"
else
mkdir "$BACKUPPATH$DATE"
fi

#create a folder for the actual time
mkdir "$BACKUPPATH$DATE/$TIME"

#save the date of the latest backup
if [ -f $LATEST ]; then
echo "$DATE" > $LATEST
else
touch "$LATEST"
echo "$DATE" > $LATEST
fi

#do backup
cp -R  "$DBPATH$LINE/" "$BACKUPPATH$DATE/$TIME/$LINE/"
done
Die Datei .snyc ist eine einfach Textdatei in der Zeile für Zeile die Verzeichnisse stehen, die gebackupt werden sollen. Beispiel:
Games
Documents/Scripts
Das setzt natürlich voraus, dass die Ordner auch existieren. delete.sh
#!/bin/sh

#Variables
LATEST="/media/Data0/Dropbox-Backup/SecureBackup/LATEST" #path to file, which saves the last date of the last backup
PATHTOFILES="/media/Data0/Dropbox-Backup/SecureBackup" #path of the backup-folder

#check for the date-file
if [ -f $LATEST ]; then
#get the date of the last saved files
DATE=`cat $LATEST`

#move the latest backup and the date-file elsewhere
mv "$PATHTOFILES/$DATE" "$PATHTOFILES/../"
mv "$LATEST" "$PATHTOFILES""/../LATEST"

#delete everything left
rm -r "$PATHTOFILES"
mkdir "$PATHTOFILES"

#move back the latest backup and the date-file
mv "$PATHTOFILES""/../$DATE" "$PATHTOFILES/"
mv "$PATHTOFILES""/../LATEST" "$PATHTOFILES/"
fi

Hinterlasse eine Antwort

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind markiert *

*

Du kannst folgende HTML-Tags benutzen: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>