How to create automated Linux/Unix backups Kā izveidot automatizētu Linux / Unix backups

by Sukrit Dhandhania on November 18, 2008 ar Sukrit Dhandhania 2008 18.novembrī

Linux

“A stitch in time saves nine”, goes an old saying. "Laikā dūriens ietaupa deviņi", iet Senais teiciens. In the world of computers we refer to that stitch as backups. Ar datoru pasaulē mēs atsaucamies uz šo dūriens kā 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. Ekspluatējot ražošanas IT infrastruktūra, ir ļoti svarīgi, ka mēs sasniedzam uptime uz mūsu serveriem un lietojumiem, kā gandrīz simts procentiem, cik iespējams. While there are a number of ways to make an IT infrastructure solid, it is impossible to make it failure proof. Lai gan ir vairāki veidi, kā izmantot IT infrastruktūru cieta, tas nav iespējams, lai padarītu to nav pierādījumu. So we make a plan B. Tātad mēs plāns B.

Servers are made of three broad layers – the hardware, the operating system and applications, and the data. Serveri tiek izgatavotas no trīs galvenie slāņi - aparatūras, operētājsistēmas un lietojumu, un datus. In case of a hardware failure it is rather simple to replace the machine with a new one. Gadījumā aparatūras kļūmes ir diezgan vienkārši nomainīt mašīna ar jauno. Installing the operating system and applications too are relatively simple. Instalēšana operētājsistēmas un lietojumu arī ir salīdzinoši vienkāršs. The part that is hard to replace is the data and configuration that your applications use. Daļas, ir grūti nomainīt, ir dati, un konfigurācijas, ka jūsu programmas izmantošanu. In case of failure this is the part that can not be replaced. Atteices gadījumā tā ir daļa, ko nevar aizstāt. So we backup that data on a regular basis. Tādēļ mēs backup, ka dati par regulāri. We back it up in such a way that it becomes easy to retrieve in case of emergency. Mēs back it up tādā veidā, ka tā kļūst viegli iegūt avārijas gadījumā.

I will guide you through writing a script to take daily backups on your system. Es jums ar rakstisku skriptu ikdienas backups jūsu sistēmā. 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. Šā aprēķina es izmantot serverī, kas darbojas Apache Web serveri, MySQL datu bāzes servera, un tā ir direktorija / home nodalījumu, kurā daži dati, kas vajadzības gadījumā papildina. We will also backup the previous day's Apache access logs in a separate backup directory. Mēs arī rezerves iepriekšējās dienas Apache piekļuve piesakās atsevišķu rezerves direktorijā. First, we need to create a directory where our backups will reside. Pirmkārt, mums ir jāizveido direktorijā, kur mūsu backups dzīvos. As part of the exercise I have created a directory called BACKUP on the /opt partition. Kā daļu no izmantošanas I have created directory aicināja BACKUP uz / opt nodalījumā. 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/ . Mēs organizējam backups šajā direktorijā, lai katru dienu rezerves dzīvo directory izveidots kā šis - / opt / BACKUP / gads / mēnesis / DATE /. So on the 11th of August 2008 the backups will go into /opt/BACKUP/2008/08/11/ . Tātad, par 11 no 2008 Augusts backups stāsies / opt/BACKUP/2008/08/11 /. Let's create these directories. Let's izveidot šo direktoriju. Run the following set of commands as the root user. Palaist šādu komplektu komandas kā root lietotājam.

# 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. Skatīt tālāk pirms šo pēdējo komandu.

# 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. Mums tagad ir divi rezerves katalogus - viens datu un citu log failus. With the backups directories created we are now ready to start writing the script. Note: I'm using the user calvin throughout this article. Ar backups katalogi izveidoja tagad mēs esam gatavi, lai sāktu rakstīt skriptu. Piezīme: Es esmu, izmantojot lietotāja Calvin visā šajā pantā. Please replace calvin with your own username. Lūdzu nomainīt Calvin ar savu lietotājvārdu.


########################################################### ################################################## #########
# CODE BEGINS HERE # CODE Sākas ŠEIT
########################################################### ################################################## #########

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

# SERVER DATA BACKUP V1.0 # SERVER DATA BACKUP V1.0

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

#Configure the month, date, and day # Konfigurēšana mēnesi, datumu un dienu
YEAR=`date +”%Y”` # 2008 GADS = "datums +"% Y "" # 2.008
MONTH=`date +”%m”`# 11 MONTH = "datums +"% m "" # 11
DAY=`date +”%d”` # 14 DAY = "datums +"% d "" # 14
YESTERDAY=`date –date=”yesterday” +%Y-%m-%d` # 2008-11-13 Vakar = "datums-date =" vakar "+% Y-% m-% d" # 2008/11/13
YESTERDAY_DATE=`date –date=yesterday +%d` # 13 YESTERDAY_DATE = "datums-date = Vakar +% d" # 13
YESTERDAY_MONTH=`date –date=yesterday +%m` # 11 YESTERDAY_MONTH = "datums-date = Vakar +% m" # 11
YESTERDAY_YEAR=`date –date=yesterday +%Y` # 2008 YESTERDAY_YEAR = "datums-date = Vakar +% Y" # 2.008
TODAY=`date +%Y-%m-%d` # 2008-11-14 ŠODIEN = "datums +% Y-% m-% d" # 2008/11/14
TODAY_DATE=`date +%d` # 14 TODAY_DATE = "datums +% d" # 14

# directories to backup to # Katalogus dublējumkopiju
DATA_BACKUP_PARENT_DIR=/opt/backup/DATA/ # <-- Change this your data backup dir DATA_BACKUP_PARENT_DIR = / opt / backup / data / # <- Mainiet šo datu backup dir
LOG_BACKUP_PARENT_DIR=/opt/backup/LOG/ # <-- Change this your log backup dir LOG_BACKUP_PARENT_DIR = / opt / backup / log / # <- Mainiet šo jūsu log backup dir

# Apache Document Root Directory # Apache Document saknes direktorijs
APACHE_DOCROOT=/var/www/html/ APACHE_DOCROOT = / var / www / html /

# MySQL Database config # MySQL Database config
DB_IP="localhost" # <-- Change this to the IP of your database server DB_IP = "localhost" # <- Maina to IP datu bāzes serveris
DB_USER="calvin" # <-- Change this to your database username DB_USER = "Calvin" # <- Mainiet šo jūsu datubāzes lietotājvārdu
DB_PASS="calvin_password" # <-- Change this to your database password DB_PASS = "calvin_password" # <- Mainiet šo jūsu datu bāzes paroli
DB_NAME="clientlist" # <-- Change this to your database name Db_name = "clientlist" # <- Mainiet šo jūsu nosaukums Datubāzes

# Apache Log Dir # Apache Log Dir
APACHE_LOG_DIR="/var/log/httpd/" # <-- Change this to the path data of your logs APACHE_LOG_DIR = "/ var / log / httpd /" # <- Maina to trajektorijas datus par Jūsu žurnāli

# Application Files' Dir # Application Files "Dir
APP_FILES="/home/calvin/application_files/" # <-- Change this to the path of the data directory APP_FILES = "/ home / Calvin / application_files /" # <- Maina to ceļu datu katalogs

# Config Dir # Config Dir
CONFIG_DIR="/etc/" # <-- Change this to the path data directory CONFIG_DIR = "/ etc /" # <- Maina to ceļu datu bāze

# Mail report address # Pasta ziņojumu adreses
EMAIL_ID="you@youremailid.com" # <-- Change this to your e-mail ID EMAIL_ID = "you@youremailid.com" # <- Maina šo uz savu e-pasta ID

########################################################### ################################################## #########
# DATA BACKUP # DATA BACKUP
########################################################### ################################################## #########

# Create and go into backup directory # Veidot un iedziļināties backup directory
cd $DATA_BACKUP_PARENT_DIR/$YEAR/$MONTH cd $ DATA_BACKUP_PARENT_DIR / $ GADS / $ month
mkdir $DAY mkdir $ day
cd $DAY cd $ day

# Database backup # Database 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 # Application 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 # LOG BACKUP
########################################################### ################################################## #########

# Create and go into backup directory # Veidot un iedziļināties backup directory
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 Backup
for i in ${APACHE_LOG_DIR}/access_log.${YESTERDAY}*; do cp $i .; done par i $ (APACHE_LOG_DIR) / access_log. $ (vakar) *; do cp $ i., parakstīts
for i in access_log.${YESTERDAY}*; do gzip $i; done par i access_log. $ (vakar) *; do gzip $ i, parakstīts

########################################################### ################################################## #########
# Send out mail notifications # Izsūtīt pasta paziņojumi
########################################################### ################################################## #########

# Mail notify # Mail paziņot
du -shc $DATA_BACKUP_PARENT_DIR/$YEAR/$MONTH/$DAY/* | mail -s “Backup for ${HOSTNAME}::DATA done du-SHC $ DATA_BACKUP_PARENT_DIR / $ GADS / $ mēnesis / $ day / * | mail-s "Backup for $ (hostname):: DATI izdarīt :) ” ${EMAIL_ID} "$ (EMAIL_ID)
du -shc $LOG_BACKUP_PARENT_DIR/$YESTERDAY_YEAR/$YESTERDAY_MONTH/$YESTERDAY_DATE/* | mail -s “Backup for ${HOSTNAME}::LOG done du-SHC $ LOG_BACKUP_PARENT_DIR / $ YESTERDAY_YEAR / $ YESTERDAY_MONTH / $ YESTERDAY_DATE / * | mail-s "Backup for $ (hostname):: LOG izdarīt :) ” ${EMAIL_ID} "$ (EMAIL_ID)

########################################################### ################################################## #########
# CODE ENDS HERE # CODE BEIGAS ŠEIT
########################################################### ################################################## #########

Save this script in a directory such as /home/calvin/scripts/backup.sh. Saglabāt šo skriptu direktorijā, piemēram / home / Calvin / skriptus / backup.sh. Give executable permissions to the file: Dot izpild atļaujas failu:

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

Now you are good to go. Tagad jums ir labi iet. Run the script form your command line: Palaist skriptu veidot savus komandrindu:

# /home/calvin/scripts/backup.sh # / Home / Calvin / skriptus / 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. Jūs varat arī izvēles pievienotu šo skriptu, kā cron darbu tā, ka tas tiks izpildīts par katru dienu pēc laika jūsu izvēles. If you want to run this script at 4 AM daily do the following: Ja jūs vēlaties palaist šo skriptu 4 dienas izdarīt šādi:

# crontab -e # Crontab-e

Create a new line in the file that opens and add the following: Izveidot jaunu līniju failu, kas atveras, un pievieno šo:

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

There you go. There you go. Your server will now backup your data, database, configuration files, application files, and log files to /opt/backup everyday at 4 AM. Jūsu serveris tagad backup datus, datu bāzes, konfigurācijas failus, pieteikuma dokumenti un log failus / opt / backup ikdienas 4 AM. 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. Smart, kas tagad jādara, būtu eksportēt šos backups uz tālvadības iekārtu, lai nepazaudētu savu dublējumu, ja serveris ir problēmas. We will cover how to do that in my next article. Mēs uz to, kā darīt, ka manā next article.

Related Posts: Related Posts:
  • How to encrypt your Linux backups Kā lai šifrētu savu Linux backups
  • Windows XP Recovery System Features Windows XP Recovery System Features
  • How to block an IP address in IPTables in Linux Kā nobloķēt IP adresi iptables Linux
  • How to format and mount a USB hard drive in Linux Kā formatēt un apsūdzību USB cieto disku Linux
  • How to Create a Multi Part Tar File with Linux Kā izveidot Multi daļas Darvas failu ar Linux
  • Get Simple Help tutorials just like this one in your email inbox every day - for free! Get Simple Palīdzība konsultācijas tāpat kā šo vienu savā e-pastā ik dienas - bez maksas! Just enter your email address below: Vienkārši ievadiet savu e-pasta adresi:

    You can always opt out of this email subscription at any time. Jūs vienmēr varat atteikties no šīs e-pasta abonementu jebkurā laikā.

    { 1 comment… read it below or (1 comment ... lasīt tālāk vai add one pievienot vienu } )

    1 1 Alberto 12.09.08 at 5:56 am At 5:56 12.09.08 Alberto am

    Hello Sveiki
    I'm testing the script, but I've errors with the lines: Es esmu pārbaudes skriptu, bet es esmu kļūdas ar līnijas:

    YESTERDAY=`date –date=”yesterday” +%Y-%m-%d` # 2008-11-13 Vakar = "datums-date =" vakar "+% Y-% m-% d" # 2008/11/13
    YESTERDAY_DATE=`date –date=yesterday+%d` # 13 YESTERDAY_DATE = "datums-date = Vakar +% d" # 13
    YESTERDAY_MONTH=`date –date=yesterday +%m` # 11 YESTERDAY_MONTH = "datums-date = Vakar +% m" # 11
    YESTERDAY_YEAR=`date –date=yesterday +%Y` # 2008 YESTERDAY_YEAR = "datums-date = Vakar +% Y" # 2.008

    The error I've is by example: Kļūda Man ir piemērs:

    mysystem# YESTERDAY=`date .date=.yesterday. mysystem # Vakar = "dienas. dienas =. vakar. +%Y-%m-%d` # 2008-11-13 +% Y-% m-% d "# 2008/11/13
    date: extra operator `+%Y-%m-%d' datums: papildu operators "+% Y-% m-% d '
    Try `date –help' for mor information Try `datums-help ', lai mor information

    I've tested it in : Ubuntu Linux 7.04 Esmu testē to: Ubuntu Linux 7,04

    Thanks (and good work !!) Paldies (un labu darbu!!)

    Leave a Comment Leave Comment

    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> Jūs varat izmantot šos HTML tagus un atribūtus: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>