Zum Inhalt springen

Linux Local System Backup Script: Unterschied zwischen den Versionen

Aus LHlab Wiki
Die Seite wurde neu angelegt: „Mit diesem Backup Script ist es möglich ein Linux System auf Dateiebene zu sichern. Hierbei wir TAR verwendet und es ist möglich ein differenzielles Backup z…“
 
KKeine Bearbeitungszusammenfassung
 
(Eine dazwischenliegende Version desselben Benutzers wird nicht angezeigt)
Zeile 1: Zeile 1:
Mit diesem Backup Script ist es möglich ein Linux System auf Dateiebene zu sichern. Hierbei wir TAR verwendet und es ist möglich ein differenzielles Backup zu nutzen um nicht immer alle Dateien zu sichern, sondern nur die Änderungen seit dem Full Backup. Zudem wird das Backup komprimiert abgespeichert. Möchte man ein tägliches Full Backup haben, so ist dies ebenfalls möglich. Am Ende verschickt das Script eine E-Mail
Mit diesem Backup Script ist es möglich ein Linux System auf Dateiebene zu sichern. Hierbei wir TAR verwendet und es ist möglich ein differenzielles Backup zu nutzen um nicht immer alle Dateien zu sichern, sondern nur die Änderungen seit dem Full Backup. Zudem wird das Backup komprimiert abgespeichert. Möchte man ein tägliches Full Backup haben, so ist dies ebenfalls möglich. Am Ende verschickt das Script eine E-Mail


== Voraussetzungen ==
==Voraussetzungen==


* Linux System
*Linux System
* installiertes <code>tar</code> und <code>gzip</code>  
*installiertes <code>tar</code> und <code>gzip</code>
* Debian Paket <code>debconf-utils</code>  
*Debian Paket <code>debconf-utils</code>
* Versand von lokalen Mails mittels <code>mail</code>  
*Versand von lokalen Mails mittels <code>mail</code>
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
apt update && apt install debconf-utils tar gzip mailutils
apt update && apt install debconf-utils tar gzip mailutils
</syntaxhighlight>
</syntaxhighlight>


== backup-local-system.sh Script ==
==backup-local-system.sh Script==


* Backup via TAR
*Backup via TAR
* komprimiertes Backup Archive
*komprimiertes Backup Archive
* Full-Backup oder Diff-Backup Möglichkeit
*Full-Backup oder Diff-Backup Möglichkeit
* Sichert Debian Paketliste und Paket Antworten
*Sichert Debian Paketliste und Paket Antworten
* Statusmail
*Statusmail
* schreibt ein Logfile
*schreibt ein Logfile
* Kann mit Excludes Arbeiten
*Kann mit Excludes Arbeiten
* Kann mehrere verschiedene Verzeichnisse Sichern
*Kann mehrere verschiedene Verzeichnisse Sichern


=== Installation ===
===Installation===


* Download: https://raw.githubusercontent.com/alaub81/scripts/master/backup-local-system.sh
*Download: https://raw.githubusercontent.com/alaub81/scripts/master/backup-local-system.sh
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
cd /usr/local/sbin
cd /usr/local/sbin
Zeile 31: Zeile 31:
</syntaxhighlight>
</syntaxhighlight>


=== Inhalt des Skriptes ===
===Inhalt des Skriptes===
<code>/usr/local/sbin/backup-local-system.sh</code><syntaxhighlight lang="bash">
<code>/usr/local/sbin/backup-local-system.sh</code><syntaxhighlight lang="bash">
#!/bin/bash
#!/bin/bash
Zeile 157: Zeile 157:
echo "#  Local Backup                                  "                              >> $LOGFILE
echo "#  Local Backup                                  "                              >> $LOGFILE
echo "#  ended: $(date +"%k:%M %d.%m.%Y")              "                              >> $LOGFILE
echo "#  ended: $(date +"%k:%M %d.%m.%Y")              "                              >> $LOGFILE
echo "#  runtime: $RUNTIME                              "                              >> $LOGFILE
echo "#############################################    "                              >> $LOGFILE
echo "#############################################    "                              >> $LOGFILE
}
}
Zeile 165: Zeile 166:


diff=$[ende-anfang]
diff=$[ende-anfang]
echo -e "\n"
RUNTIME="Runtime: $[$diff / 60]min $[$diff % 60]s"
RUNTIME="Runtime: $[$diff / 60]min $[$diff % 60]s"
}
}
Zeile 192: Zeile 192:
fi
fi
CLEANUP
CLEANUP
RUNNINGTIME
ENDBLABLA
ENDBLABLA
RUNNINGTIME
if [ $STATUSMAIL = yes ]; then
if [ $STATUSMAIL = yes ]; then
     MAIL
     MAIL
Zeile 204: Zeile 204:
</syntaxhighlight>
</syntaxhighlight>


== Tägliches Backup via cronjob ==
==Tägliches Backup via cronjob==
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
ln -s /usr/local/sbin/backup-local-system.sh /etc/cron.daily/backup-local-system
ln -s /usr/local/sbin/backup-local-system.sh /etc/cron.daily/backup-local-system
</syntaxhighlight>
</syntaxhighlight>
{{#related:System Backup Skript mit Nagios Überwachung}}
{{#related:Docker Backup und Restore - eine kleine Anleitung}}
{{#related:Docker Volume Backup Script}}
[[Kategorie:Backup]]
[[Kategorie:Backup]]
[[Kategorie:Linux]]
[[Kategorie:Linux]]

Aktuelle Version vom 25. April 2020, 07:28 Uhr

Mit diesem Backup Script ist es möglich ein Linux System auf Dateiebene zu sichern. Hierbei wir TAR verwendet und es ist möglich ein differenzielles Backup zu nutzen um nicht immer alle Dateien zu sichern, sondern nur die Änderungen seit dem Full Backup. Zudem wird das Backup komprimiert abgespeichert. Möchte man ein tägliches Full Backup haben, so ist dies ebenfalls möglich. Am Ende verschickt das Script eine E-Mail

Voraussetzungen

  • Linux System
  • installiertes tar und gzip
  • Debian Paket debconf-utils
  • Versand von lokalen Mails mittels mail
apt update && apt install debconf-utils tar gzip mailutils

backup-local-system.sh Script

  • Backup via TAR
  • komprimiertes Backup Archive
  • Full-Backup oder Diff-Backup Möglichkeit
  • Sichert Debian Paketliste und Paket Antworten
  • Statusmail
  • schreibt ein Logfile
  • Kann mit Excludes Arbeiten
  • Kann mehrere verschiedene Verzeichnisse Sichern

Installation

cd /usr/local/sbin
wget https://raw.githubusercontent.com/alaub81/scripts/master/backup-local-system.sh
chmod +x backup-local-system.sh

Inhalt des Skriptes

/usr/local/sbin/backup-local-system.sh

#!/bin/bash
#########################################################################
#backup-local-system.sh
#Backup Script
#by A. Laub
#andreas[-at-]laub-home.de
#
#License:
#This program is free software: you can redistribute it and/or modify it
#under the terms of the GNU General Public License as published by the
#Free Software Foundation, either version 3 of the License, or (at your option)
#any later version.
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
#or FITNESS FOR A PARTICULAR PURPOSE.
#########################################################################
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
export PATH

#########################################################################
#Backup Options
#########################################################################
#where to save the backup files
BACKUPPATH="/backup/system"
#Temporary backup files path
BACKUPTEMPPATH="/tmp"
#Backup filenames
FULLBACKUPFILE="backup-$(date +"%F-%H%M")-FULL.tar.gz"
DIFFBACKUPFILE="backup-$(date +"%F-%H%M")-DIFF.tar.gz"
#What you want to backup
FILESTOSAVE="/root/ /etc/ /usr/local/sbin/ /srv/ /var/ /home/ /boot/ /media/"
#Files you want to exclude from the backup (uncomment to use)
EXCLUDEFILES="--exclude=/var/lib/docker/* --exclude=/var/cache/apt/* --exclude=/var/swap"
# Make only a Full Backups and No Differential (yes/no)
FULLBACKUPONLY="no"
# Day for the FullBackup (Sun,Mon,Tue,Wed,Thu,Fri,Sat)
FULLBACKUPDAY="Sun"
#Delete old Backupfiles when they are xx Days old
DAYS="14"

#########################################################################
#Logging Options
#########################################################################
#Mail Notification (yes/no)
STATUSMAIL="yes"
#Email Adress, where to send the logfile
MAILADRESS="andreas@laub-home.de"
#Logfile
LOGFILE="/var/log/systembackup.log"


##########################################################################
#Here the functions starting: Don't edit below
##########################################################################

function STARTBLABLA {
echo "#############################################     "                               > $LOGFILE
echo "#  Local Backup                                   "                               >> $LOGFILE
echo "#  started: $(date +"%k:%M %d.%m.%Y")             "                               >> $LOGFILE
echo "#############################################     "                               >> $LOGFILE
echo ""                                                                                 >> $LOGFILE
echo ""                                                                                 >> $LOGFILE
}

function BACKUPDIRCHECK {
if [ -d $BACKUPPATH ]; then
        echo "Backup folder exists"                                                     >> $LOGFILE
else
        mkdir -p $BACKUPPATH
        echo "Backup folder created"                                                    >> $LOGFILE
fi
}

function REMOVE {
#Remove old Backup files 
find $BACKUPPATH -iname "*.tar.gz" -daystart -mtime +$DAYS -delete                      >> $LOGFILE
#Remove Timestamp File on Fullbackupday
if [ $(date +%a) = $FULLBACKUPDAY -o $FULLBACKUPONLY = yes ]; then
        rm -f $BACKUPPATH/timestamp.txt
fi
}

function PACKAGE {
#Backup Packagelist
#Help for restore:
#1. dpkg --set-selections < $BACKUPTEMPPATH/$BACKUPHOST_pkglist.txt
#2. dselect
echo "Packagelist Backup"                                                               >> $LOGFILE
dpkg --get-selections > $BACKUPTEMPPATH/pkglist.txt

#Backup Package Answers
#Help for restore:
#1. debconf-set-selections $BACKUPPATH/$BACKUPHOST_pkganswers.tx
echo "Pakage Answers Backup"                                                            >> $LOGFILE
debconf-get-selections > $BACKUPTEMPPATH/pkganswers.txt
#read OTHERBACKUPFILES
OTHERBACKUPFILES="$BACKUPTEMPPATH/pkglist.txt $BACKUPTEMPPATH/pkganswers.txt"
}

function FULLBACKUP {
echo "Full Backup:"                                                                     >> $LOGFILE
date '+%Y-%m-%d %H:%M:%S' > $BACKUPPATH/timestamp.txt
tar czPf $BACKUPPATH/$FULLBACKUPFILE $EXCLUDEFILES $FILESTOSAVE $OTHERBACKUPFILES \
        2> /tmp/tmp_file; grep "^tar:" /tmp/tmp_file | grep -v "file is unchanged"      >> $LOGFILE
/bin/ls -lh $BACKUPPATH/$FULLBACKUPFILE | awk '{ print $5 " " $8 " " $9}'               >> $LOGFILE
}

function DIFFBACKUP {
#Timestamp Datei Auslesen
echo "Diff Backup:"                                                                     >> $LOGFILE
tar czPf $BACKUPPATH/$DIFFBACKUPFILE --newer $BACKUPPATH/timestamp.txt $EXCLUDEFILES $FILESTOSAVE $OTHERBACKUPFILES \
        2> /tmp/tmp_file; grep "^tar:" /tmp/tmp_file | grep -v "file is unchanged"      >> $LOGFILE
/bin/ls -lh $BACKUPPATH/$DIFFBACKUPFILE | awk '{ print $5 " " $8 " " $9}'               >> $LOGFILE
}

function CLEANUP {
rm -f $OTHERBACKUPFILES
}
function ENDBLABLA {
echo ""                                                                                 >> $LOGFILE
echo ""                                                                                 >> $LOGFILE
echo "#############################################     "                               >> $LOGFILE
echo "#  Local Backup                                   "                               >> $LOGFILE
echo "#  ended: $(date +"%k:%M %d.%m.%Y")               "                               >> $LOGFILE
echo "#  runtime: $RUNTIME                              "                               >> $LOGFILE
echo "#############################################     "                               >> $LOGFILE
}

function RUNNINGTIME {
# stop the timer ########################################
ende=$(date +%s)

diff=$[ende-anfang]
RUNTIME="Runtime: $[$diff / 60]min $[$diff % 60]s"
}

function MAIL {
mail -s "Local Backup on $(hostname -s)" $MAILADRESS                                    < $LOGFILE
}


#####################################################################################
# All functions into 1 function :-)
#####################################################################################

function SYSTEMBACKUP {
# start timer ########################################
anfang=$(date +%s)

STARTBLABLA
BACKUPDIRCHECK
REMOVE
PACKAGE
if [ ! -f $BACKUPPATH/timestamp.txt ]; then 
        FULLBACKUP
elif [ -f $BACKUPPATH/timestamp.txt ]; then
        DIFFBACKUP
fi
CLEANUP
RUNNINGTIME
ENDBLABLA
if [ $STATUSMAIL = yes ]; then
    MAIL
fi
}

# at least start the backup
SYSTEMBACKUP

Tägliches Backup via cronjob

ln -s /usr/local/sbin/backup-local-system.sh /etc/cron.daily/backup-local-system