“A stitch in time saves nine”, goes an old saying. "Un cucire in tempo di risparmiare nove", un vecchio va dicendo. In the world of computers we refer to that stitch as backups. Nel mondo dei computer ci riferiamo a quella cucire come backup. 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. Nel corso di una infrastruttura IT di produzione, è imperativo che noi raggiungere un uptime sui nostri server e le applicazioni di più vicino ad un cento per cento per quanto possibile. While there are a number of ways to make an IT infrastructure solid, it is impossible to make it failure proof. Mentre ci sono un certo numero di modi per fare una solida infrastruttura IT, è impossibile per rendere la mancata prova. So we make a plan B. Così facciamo un piano B.
Servers are made of three broad layers - the hardware, the operating system and applications, and the data. Server sono costituiti da tre strati - l'hardware, il sistema operativo e delle applicazioni e dei dati. In case of a hardware failure it is rather simple to replace the machine with a new one. In caso di un guasto hardware è piuttosto semplice per sostituire la macchina con uno nuovo. Installing the operating system and applications too are relatively simple. Installare il sistema operativo e le applicazioni sono anche relativamente semplice. The part that is hard to replace is the data and configuration that your applications use. La parte che è difficile da sostituire sia i dati e la configurazione che uso le vostre applicazioni. In case of failure this is the part that can not be replaced. In caso di mancato questa è la parte che non può essere sostituita. So we backup that data on a regular basis. Quindi abbiamo che i dati di backup su base regolare. We back it up in such a way that it becomes easy to retrieve in case of emergency. Ci si torna in modo tale che diventa facile da recuperare in caso di emergenza.
I will guide you through writing a script to take daily backups on your system. Io vi guiderà attraverso scrivere uno script per il backup giornaliero prendere sul vostro sistema. 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. Per questo esercizio io uso un server che esegue il server web Apache, un database MySQL server, e ha una directory in / home partizione che contiene alcuni dati che ha bisogno di essere sostenuta. We will also backup the previous day’s Apache access logs in a separate backup directory. Ci sarà anche il backup del giorno precedente Apache log di accesso in una directory di backup separato. First, we need to create a directory where our backups will reside. In primo luogo, dobbiamo creare una directory dove i nostri backup si risiede. As part of the exercise I have created a directory called BACKUP on the /opt partition. Come parte del l'esercizio ho creato una directory chiamata backup su / opt partizione. 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/ . Noi organizzare i backup in questa directory in modo che ogni giorno di backup risiede in una directory formato come questo - / opt / backup / anno / mese / DATA /. So on the 11th of August 2008 the backups will go into /opt/BACKUP/2008/08/11/ . Così il giorno 11 del mese di agosto 2008, il backup andrà in / opt/BACKUP/2008/08/11 /. Let’s create these directories. Creiamo queste directory. Run the following set of commands as the root user. Eseguire il seguente serie di comandi come utente root.
# 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 DATI LOG
See note below before issuing this last command. Vedi nota qui di seguito prima di rilasciare questo ultimo comando.
# 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. Abbiamo ora due set di backup di directory - uno per i dati e un altro per i file di log. With the backups directories created we are now ready to start writing the script. Note: I’m using the user calvin throughout this article. Con la directory di backup creato siamo ora pronti per iniziare a scrivere la sceneggiatura. Nota: Sto utilizzando l'utente Calvin tutto questo articolo. Please replace calvin with your own username. Si prega di sostituire Calvin con il tuo nome utente.
########################################################### ################################################## #########
# CODE BEGINS HERE # Codice inizia qui
########################################################### ################################################## #########
#!/bin/bash #! / bin / bash
# SERVER DATA BACKUP V1.0 # Server backup dei dati v1.0
#########################
# BACKUP CONFIG # SALVATAGGIO CONFIG
#########################
#Configure the month, date, and day # Configurare il mese, la data e il giorno
YEAR=`date +”%Y”` # 2008 ANNO = `date +% Y" `# 2008
MONTH=`date +”%m”`# 11 MESE = `date +"% m "` # 11
DAY=`date +”%d”` # 14 GIORNATA = `date +"% d "` # 14
YESTERDAY=`date –date=”yesterday” +%Y-%m-%d` # 2008-11-13 IERI = `date-data =" ieri "+% Y-% m-% d` # 2008-11-13
YESTERDAY_DATE=`date –date=yesterday +%d` # 13 YESTERDAY_DATE = `date-data ieri = +% d` # 13
YESTERDAY_MONTH=`date –date=yesterday +%m` # 11 YESTERDAY_MONTH = `date-data ieri = +% m» # 11
YESTERDAY_YEAR=`date –date=yesterday +%Y` # 2008 YESTERDAY_YEAR = `date-data ieri = +% Y` # 2008
TODAY=`date +%Y-%m-%d` # 2008-11-14 OGGI = `date +% Y-% m-% d` # 2008-11-14
TODAY_DATE=`date +%d` # 14 TODAY_DATE = `date +% d` # 14
# directories to backup to # Directory di backup
DATA_BACKUP_PARENT_DIR=/opt/backup/DATA/ # <-- Change this your data backup dir DATA_BACKUP_PARENT_DIR = / opt / backup / DATI / # <- Modificare questa tuoi dati di backup dir
LOG_BACKUP_PARENT_DIR=/opt/backup/LOG/ # <-- Change this your log backup dir LOG_BACKUP_PARENT_DIR = / opt / backup / log / # <- questo cambiamento di registro di backup tuo dir
# Apache Document Root Directory # Apache documento root directory
APACHE_DOCROOT=/var/www/html/ APACHE_DOCROOT = / var / www / html /
# MySQL Database config # Config Database MySQL
DB_IP="localhost" # <-- Change this to the IP of your database server DB_IP = "localhost" # <- questo cambiamento e il periodo del vostro server di database
DB_USER="calvin" # <-- Change this to your database username DB_USER = "Calvino" # <- questo cambiamento al tuo nome utente database
DB_PASS="calvin_password" # <-- Change this to your database password DB_PASS = "calvin_password" # <- Modificare questa vostra base di dati per la password
DB_NAME="clientlist" # <-- Change this to your database name Db_name = "clientlist" # <- Modificare questa vostra base di dati di nome
# Apache Log Dir # Dir log di Apache
APACHE_LOG_DIR="/var/log/httpd/" # <-- Change this to the path data of your logs APACHE_LOG_DIR = "/ var / log / httpd /" # <- Modificare questa la via dei tuoi dati di log
# Application Files' Dir Domanda # File 'Dir
APP_FILES="/home/calvin/application_files/" # <-- Change this to the path of the data directory APP_FILES = "/ home / Calvin / application_files /" # <- questo cambiamento al percorso della directory dei dati
# Config Dir # Config Dir
CONFIG_DIR="/etc/" # <-- Change this to the path data directory Config_dir = "/ etc /" # <- questo cambiamento al percorso directory dei dati
# Mail report address # Relazione indirizzo di posta
EMAIL_ID="you@youremailid.com" # <-- Change this to your e-mail ID EMAIL_ID = "you@youremailid.com" # <- Variazione di questa tua e-mail ID
########################################################### ################################################## #########
# DATA BACKUP # SALVATAGGIO DEI DATI
########################################################### ################################################## #########
# Create and go into backup directory # Crea e andare in directory di backup
cd $DATA_BACKUP_PARENT_DIR/$YEAR/$MONTH DATA_BACKUP_PARENT_DIR cd $ / $ ANNO / $ MESE
mkdir $DAY mkdir $ GIORNO
cd $DAY cd $ GIORNO
# Database backup # Database di 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 L'applicazione di backup #
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 # Crea e andare in directory di backup
cd $LOG_BACKUP_PARENT_DIR/$YESTERDAY_YEAR/$YESTERDAY_MONTH LOG_BACKUP_PARENT_DIR cd $ / $ YESTERDAY_YEAR / $ YESTERDAY_MONTH
mkdir $YESTERDAY_DATE mkdir $ YESTERDAY_DATE
cd $YESTERDAY_DATE cd $ YESTERDAY_DATE
# HTTPD Log Backup # Httpd log di backup
for i in ${APACHE_LOG_DIR}/access_log.${YESTERDAY}*; do cp $i .; done per i in $ () APACHE_LOG_DIR / access_log. IERI $ () *; fare cp $ i.; fatto
for i in access_log.${YESTERDAY}*; do gzip $i; done per i in access_log. IERI $ () *; fare gzip $ i; fatto
########################################################### ################################################## #########
# Send out mail notifications # Spedisci la posta notifiche
########################################################### ################################################## #########
# Mail notify # Mail notificare
du -shc $DATA_BACKUP_PARENT_DIR/$YEAR/$MONTH/$DAY/* | mail -s “Backup for ${HOSTNAME}::DATA done :)” ${EMAIL_ID} du-SHC DATA_BACKUP_PARENT_DIR $ / $ ANNO / MESE $ / $ DAY / * | mail-s "Backup per $ (hostname):: DATI fatto:)" $ () 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 per $ (hostname):: LOG fatto:)" $ () EMAIL_ID
########################################################### ################################################## #########
# CODE ENDS HERE # Codice finisce qui
########################################################### ################################################## #########
Save this script in a directory such as /home/calvin/scripts/backup.sh. Salva questo script in una directory come / home / Calvin / scripts / backup.sh. Give executable permissions to the file: Eseguibile dare le autorizzazioni per il file:
# chmod +x /home/calvin/scripts/backup.sh # Chmod + x / home / Calvin / scripts / backup.sh
Now you are good to go. Ora si sta bene ad andare. Run the script form your command line: Eseguire lo script forma la riga di comando:
# /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. È anche possibile opzionalmente aggiungere questo script come un job di cron in modo che esso verrà eseguito su una base quotidiana in un momento di vostra scelta. If you want to run this script at 4 AM daily do the following: Se si desidera eseguire questo script a 4 AM quotidiano effettuare le seguenti operazioni:
# crontab -e # Crontab-e
Create a new line in the file that opens and add the following: Creare una nuova linea nel file che si apre e aggiungere il seguente:
0 4 * * * /home/calvin/scripts/backup.sh > /dev/null 0 4 * * * / home / Calvin / scripts / backup.sh> / dev / null
There you go. Ci si va. Your server will now backup your data, database, configuration files, application files, and log files to /opt/backup everyday at 4 AM. Il server ora backup dei dati, database, file di configurazione, l'applicazione di file e file di log in / opt / backup di tutti i giorni alle 4 del mattino. 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. Una smart cosa da fare adesso sarebbe quello di esportare questi backup di un computer remoto in modo da non perdere il backup se il server deve affrontare i problemi. We will cover how to do that in my next article. Si vedrà più avanti come fare nel mio prossimo articolo.






















{ 0 comments… (0 commenti ... add one now aggiungere uno ora } )
Leave a Comment Lascia un tuo commento