How to create automated Linux/Unix backups So erstellen Sie automatisierte Linux / Unix-Backups

by Sukrit Dhandhania on November 18, 2008 von Sukrit Dhandhania am 18 November, 2008

Linux

“A stitch in time saves nine”, goes an old saying. "A Stitch in der Zeit spart neun", geht ein altes Sprichwort. In the world of computers we refer to that stitch as backups. In der Welt der Computer beziehen wir uns auf, dass Stich als Backups. When running a production IT infrastructure, it is imperative that we achieve an uptime on our servers and applications of as close to a hundred percent as possible. Bei der Ausführung eines Produktions-IT-Infrastruktur, ist es unerlässlich, dass wir erreichen ein uptime auf unseren Servern und Anwendungen der so nahe wie möglich an ein hundert Prozent wie möglich zu gestalten. While there are a number of ways to make an IT infrastructure solid, it is impossible to make it failure proof. Es gibt zwar eine Reihe von Möglichkeiten, um eine solide IT-Infrastruktur ist es unmöglich zu machen, Scheitern Beweis. So we make a plan B. Also machen wir einen Plan B.

Servers are made of three broad layers - the hardware, the operating system and applications, and the data. Server sind aus drei breite Schichten - die Hardware, das Betriebssystem und Anwendungen, und die Daten. In case of a hardware failure it is rather simple to replace the machine with a new one. Im Falle eines Hardware-Fehler ist es nicht einfach zu ersetzen, die Maschine mit einem neuen ein. Installing the operating system and applications too are relatively simple. Installation von Betriebssystem und Anwendungen sind auch relativ einfach. The part that is hard to replace is the data and configuration that your applications use. Der Teil, ist schwer zu ersetzen ist, die Daten und Konfigurationen zu, dass Ihre Anwendungen verwenden. In case of failure this is the part that can not be replaced. Im Falle des Scheiterns dieser ist der Teil, können nicht ersetzt werden. So we backup that data on a regular basis. So, dass wir Backup-Daten auf einer regulären Basis. We back it up in such a way that it becomes easy to retrieve in case of emergency. Wir unterstützen es so, dass es einfach wird, um in Notfällen.

I will guide you through writing a script to take daily backups on your system. Ich werde Sie durch ein Skript schreiben, um tägliche Backups auf Ihrem System. For this exercise I will use a server that runs the Apache web server, a MySQL database server, and has a directory on the /home partition that contains some data that needs to be backed up. Bei dieser Übung werde ich mit einem Server, läuft der Apache Web-Server, einer MySQL-Datenbank-Server, und hat ein Verzeichnis auf der / home-Partition enthält, dass einige Daten, muss gesichert werden. We will also backup the previous day’s Apache access logs in a separate backup directory. Wir werden auch Backup des letzten Tages, Apache-Logs Zugang in einem separaten Backup-Verzeichnis. First, we need to create a directory where our backups will reside. Zuerst müssen wir zu dem Erstellen eines Verzeichnisses, wo unsere Backups werden soll. As part of the exercise I have created a directory called BACKUP on the /opt partition. Im Rahmen der Übung habe ich erstellt ein Verzeichnis namens BACKUP auf der / opt-Partition. We will organize the backups in this directory so that each days backup resides in a directory formed like this - /opt/BACKUP/YEAR/MONTH/DATE/ . Wir organisieren die Sicherungen in diesem Verzeichnis, so dass jedes Backup-Tag befindet sich in einem Verzeichnis gebildet davon - / opt / backup / Jahr / Monat / DATE /. So on the 11th of August 2008 the backups will go into /opt/BACKUP/2008/08/11/ . Dies auf der 11. August 2008 die Sicherungen werden in / opt/BACKUP/2008/08/11 /. Let’s create these directories. Lassen Sie uns diese Verzeichnisse erstellen. Run the following set of commands as the root user. Führen Sie den folgenden Satz von Befehlen als "root"-Benutzer.

# mkdir -p /opt/backup/DATA/2008 # Mkdir-p / opt/backup/DATA/2008
# cd /opt/backup/DATA/2008 # Cd / opt/backup/DATA/2008
# mkdir 01 02 03 04 05 06 07 08 09 10 11 12 # Mkdir 01 02 03 04 05 06 07 08 09 10 11 12
# cd /opt/backup/DATA/ # Cd / opt / backup / DATA /
# cp -r 2008 2009 # Cp-r 2008 2009
# cd /opt/backup/ # Cd / opt / backup /
# cp -r DATA LOG # Cp-r DATA LOG

See note below before issuing this last command. Siehe Hinweis unten, bevor dieser letzte Befehl.

# chown -R calvin.calvin /opt/backup # Chown-R calvin.calvin / opt / backup

We now have two sets of backup directories - one for data and another for log files. Wir haben jetzt zwei Sätze von Backup-Verzeichnisse - ein für die Daten-und ein anderes für die Log-Dateien. With the backups directories created we are now ready to start writing the script. Note: I’m using the user calvin throughout this article. Mit der Backups Verzeichnisse erstellt wir sind nun bereit, mit dem Schreiben beginnen das Skript. Hinweis: Ich bin mit dem Benutzer Calvin in diesem Artikel. Please replace calvin with your own username. Ersetzen Sie bitte Calvins mit Ihren eigenen Benutzernamen.


########################################################### ################################################## #########
# CODE BEGINS HERE #-Code beginnt hier
########################################################### ################################################## #########

#!/bin/bash #! / bin / bash

# SERVER DATA BACKUP V1.0 # Server-Daten-Backup v1.0

#########################
# BACKUP CONFIG # BACKUP CONFIG
#########################

#Configure the month, date, and day # Konfigurieren Sie den Monat, Datum, Tag und
YEAR=`date +”%Y”` # 2008 YEAR = `date +"% Y "` # 2008
MONTH=`date +”%m”`# 11 MONTH = `date +"% m "" # 11
DAY=`date +”%d”` # 14 TAG = `date +"% d "` # 14
YESTERDAY=`date –date=”yesterday” +%Y-%m-%d` # 2008-11-13 GESTERN = `date-date =" gestern "+% Y-% m-% d` # 2008-11-13
YESTERDAY_DATE=`date –date=yesterday +%d` # 13 YESTERDAY_DATE = `date-date = gestern +% d` # 13
YESTERDAY_MONTH=`date –date=yesterday +%m` # 11 YESTERDAY_MONTH = `date-date = gestern +% m` # 11
YESTERDAY_YEAR=`date –date=yesterday +%Y` # 2008 YESTERDAY_YEAR = `date-date = gestern +% Y" # 2008
TODAY=`date +%Y-%m-%d` # 2008-11-14 HEUTE = `date +% Y-% m-% d` # 2008-11-14
TODAY_DATE=`date +%d` # 14 TODAY_DATE = `date +% d` # 14

# directories to backup to # Verzeichnisse zu Backup-to -
DATA_BACKUP_PARENT_DIR=/opt/backup/DATA/ # <-- Change this your data backup dir DATA_BACKUP_PARENT_DIR = / opt / backup / DATA / # <- Ändern Sie diese Ihren Datensicherung R.
LOG_BACKUP_PARENT_DIR=/opt/backup/LOG/ # <-- Change this your log backup dir LOG_BACKUP_PARENT_DIR = / opt / backup / LOG / # <- Ändern Sie diese Ihren Log-Sicherung R.

# Apache Document Root Directory # Apache Document Root-Verzeichnis
APACHE_DOCROOT=/var/www/html/ APACHE_DOCROOT = / var / www / html /

# MySQL Database config # MySQL-Datenbank-Konfiguration
DB_IP="localhost" # <-- Change this to the IP of your database server DB_IP = "localhost" # <- Ändern Sie diese auf die IP Ihres Datenbank-Server
DB_USER="calvin" # <-- Change this to your database username Db_user = "Calvin" # <- Ändern Sie dies in Ihrer Datenbank-Benutzernamen
DB_PASS="calvin_password" # <-- Change this to your database password DB_PASS = "calvin_password" # <- Ändern Sie dies in Ihrer Datenbank Passwort
DB_NAME="clientlist" # <-- Change this to your database name DB_NAME = "clientlist" # <- Ändern Sie dies in Ihrer Datenbank Name

# Apache Log Dir # Apache-Log-R
APACHE_LOG_DIR="/var/log/httpd/" # <-- Change this to the path data of your logs APACHE_LOG_DIR = "/ var / log / httpd /" # <- Ändern Sie diese auf den Weg Ihrer Daten logs

# Application Files' Dir # Application-Dateien 'R
APP_FILES="/home/calvin/application_files/" # <-- Change this to the path of the data directory APP_FILES = "/ home / Calvin / application_files /" # <- Ändern Sie diese auf den Pfad der Daten-Verzeichnis

# Config Dir # Config R
CONFIG_DIR="/etc/" # <-- Change this to the path data directory Config_dir = "/ etc /" # <- Ändern Sie diese auf den Pfad Daten-Verzeichnis

# Mail report address # Bericht Mail-Adresse
EMAIL_ID="you@youremailid.com" # <-- Change this to your e-mail ID EMAIL_ID = "you@youremailid.com" # <- Ändern Sie diese an Ihre E-Mail-ID

########################################################### ################################################## #########
# DATA BACKUP # Datensicherung
########################################################### ################################################## #########

# Create and go into backup directory # Erstellen und wechselt in den Backup-Verzeichnis
cd $DATA_BACKUP_PARENT_DIR/$YEAR/$MONTH cd $ DATA_BACKUP_PARENT_DIR / $ JAHR / MONAT $
mkdir $DAY mkdir $ TAG
cd $DAY cd $ TAG

# Database backup # Datenbank-Backup
mysqldump -h ${DB_IP} -u ${DB_USER} -p${DB_PASS} ${DB_NAME} > ${DB_NAME}.db mysqldump-h $ () DB_IP-u $ () db_user-p $ (DB_PASS) $ (DB_NAME)> $ () DB_NAME. db
tar -zcvf ${DB_NAME}.tar.gz ${DB_NAME}.db tar-zcvf $ () DB_NAME. tar.gz $ () DB_NAME. db
rm -f ${DB_NAME}.db rm-f $ () DB_NAME. db

# /etc backup # / Etc Backup
tar -zcf etc.tar.gz /etc tar-zcf etc.tar.gz / etc

# Application backup # Backup-Anwendung
tar -zcf Apache_Doc_Root.tar.gz ${APACHE_DOCROOT} tar-zcf Apache_Doc_Root.tar.gz $ (APACHE_DOCROOT)
tar -zcf App_Files.tar.gz ${APP_FILES} tar-zcf App_Files.tar.gz $ (APP_FILES)

########################################################### ################################################## #########
# LOG BACKUP # BACKUP LOG
########################################################### ################################################## #########

# Create and go into backup directory # Erstellen und wechselt in den Backup-Verzeichnis
cd $LOG_BACKUP_PARENT_DIR/$YESTERDAY_YEAR/$YESTERDAY_MONTH cd $ LOG_BACKUP_PARENT_DIR / $ YESTERDAY_YEAR / $ YESTERDAY_MONTH
mkdir $YESTERDAY_DATE mkdir $ YESTERDAY_DATE
cd $YESTERDAY_DATE cd $ YESTERDAY_DATE

# HTTPD Log Backup # Httpd Log-Sicherung
for i in ${APACHE_LOG_DIR}/access_log.${YESTERDAY}*; do cp $i .; done für i in $ (APACHE_LOG_DIR) / access_log. GESTERN $ () *; do cp $ i.; getan
for i in access_log.${YESTERDAY}*; do gzip $i; done für i in access_log. GESTERN $ () *; tun gzip $ i; getan

########################################################### ################################################## #########
# Send out mail notifications # Das Verschicken von E-Mail-Benachrichtigungen
########################################################### ################################################## #########

# Mail notify # E-Mail benachrichtigen
du -shc $DATA_BACKUP_PARENT_DIR/$YEAR/$MONTH/$DAY/* | mail -s “Backup for ${HOSTNAME}::DATA done :)” ${EMAIL_ID} du-SHC DATA_BACKUP_PARENT_DIR $ / $ JAHR / MONAT $ / $ TAG / * | mail-s "Backup für $ (hostname):: DATA getan:)" $ (EMAIL_ID)
du -shc $LOG_BACKUP_PARENT_DIR/$YESTERDAY_YEAR/$YESTERDAY_MONTH/$YESTERDAY_DATE/* | mail -s “Backup for ${HOSTNAME}::LOG done :)” ${EMAIL_ID} du-SHC LOG_BACKUP_PARENT_DIR $ / $ YESTERDAY_YEAR / $ YESTERDAY_MONTH / $ YESTERDAY_DATE / * | mail-s "Backup für $ (hostname):: LOG getan:)" $ (EMAIL_ID)

########################################################### ################################################## #########
# CODE ENDS HERE #-Code endet hier
########################################################### ################################################## #########

Save this script in a directory such as /home/calvin/scripts/backup.sh. Speichern Sie dieses Skript in einem Verzeichnis wie / home / Calvin / scripts / backup.sh. Give executable permissions to the file: Geben Sie ausführbare Berechtigungen für die Datei:

# chmod +x /home/calvin/scripts/backup.sh # Chmod + x / home / Calvin / scripts / backup.sh

Now you are good to go. Jetzt bist du gut zu gehen. Run the script form your command line: Führen Sie das Skript Form Ihre Befehlszeile:

# /home/calvin/scripts/backup.sh # / Home / Calvin / scripts / backup.sh

You can also optionally add this script as a cron job so that it will be executed on a daily basis at a time of your choice. Sie können auch optional hinzufügen, dieses Skript als cron-Job so, dass es ausgeführt werden auf einer täglichen Basis zu einem Zeitpunkt Ihrer Wahl. If you want to run this script at 4 AM daily do the following: Wenn Sie dieses Skript auf 4 täglich gehen Sie wie folgt vor:

# crontab -e # Crontab-e

Create a new line in the file that opens and add the following: Erstellen Sie eine neue Zeile in die Datei, die geöffnet, und fügen Sie die folgenden Schritte aus:

0 4 * * * /home/calvin/scripts/backup.sh > /dev/null 0 4 * * * / home / Calvin / scripts / backup.sh> / dev / null

There you go. Dort gehen. Your server will now backup your data, database, configuration files, application files, and log files to /opt/backup everyday at 4 AM. Ihr Server wird nun ein Backup Ihrer Daten, Datenbank-, Konfigurations-Dateien, die Anwendung Dateien und Log-Dateien nach / opt / Backup täglich um 4 Uhr. A smart thing to do now would be to export these backups to a remote machine so that you don’t lose your backups if your server faces problems. Eine schlanke Sache zu tun, jetzt wäre die Ausfuhr dieser Sicherungen auf einem Remote-Rechner, so dass Sie nicht verlieren Ihre Backups, wenn Ihr Server steht vor Probleme. We will cover how to do that in my next article. Wir decken, wie Sie das in meinem nächsten Artikel.

Related Posts: Verwandte Einträge:
  • Increase your Linux/Unix Productivity: How to use crontab Steigern Sie Ihren Linux / Unix-Produktivität: Wie benutzt man crontab
  • Walkthrough: Time Machine for OS X 10.5 Leopard Walkthrough: Time Machine für OS X Leopard 10/5
  • How to backup your files to your Dreamhost account (OS X) Wie sichern Sie Ihre Dateien auf Ihren DreamHost Konto (OS X)
  • How to create a custom Windows Vista Installation DVD So erstellen Sie eine benutzerdefinierte Windows Vista Installations-DVD
  • Review: BackBlaze backup service - 25 Beta Invites for Simple Help readers Review: BackBlaze Backup-Service - 25 Beta bittet um Hilfe für Simple Leser
  • Get Simple Help tutorials just like this one in your email inbox every day - for free! Holen Sie sich Einfache Hilfe Tutorials wie diese ein in Ihrem E-Mail-Posteingang jeden Tag - gratis! Just enter your email address below: Geben Sie einfach Ihre E-Mail-Adresse unten ein:

    You can always opt out of this email subscription at any time. Sie können immer Opt-out in dieser E-Mail-Abonnement zu jeder Zeit.


    Bookmark and Share Lesezeichen und Aktie

    { 0 comments… (0 Kommentare ... add one now Add jetzt ein } )

    Leave a Comment Schreibe einen Kommentar

    You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> Sie können diese HTML-Tags und Attribute: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>