Skip to content Gå til innhold


How to create automated Linux/Unix backups Hvordan lage automatiserte Linux / Unix backup

Linux

“A stitch in time saves nine”, goes an old saying. "A stitch in time sparer ni", går et gammelt ordtak. In the world of computers we refer to that stitch as backups. I en verden av datamaskiner vi refererer til at masken som 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. Når du kjører en produksjon IT-infrastruktur, er det viktig at vi oppnå en oppetid på våre servere og applikasjoner i så nær hundre prosent som mulig. While there are a number of ways to make an IT infrastructure solid, it is impossible to make it failure proof. Mens det er flere måter å lage en IT-infrastruktur solid, er det umulig å gjøre det dårlig bevis. So we make a plan B. Så lager vi en plan B.

Servers are made of three broad layers – the hardware, the operating system and applications, and the data. Servere er laget av tre brede lag - maskinvaren, operativsystemet og programmene og dataene. In case of a hardware failure it is rather simple to replace the machine with a new one. Ved en maskinvarefeil det er ganske enkel å bytte ut maskinen med en ny. Installing the operating system and applications too are relatively simple. Installere operativsystem og programmer også er relativt enkle. The part that is hard to replace is the data and configuration that your applications use. Den delen som er vanskelig å erstatte er data og konfigurasjon som programmene bruker. In case of failure this is the part that can not be replaced. I tilfelle svikt dette er den delen som ikke kan erstattes. So we backup that data on a regular basis. Så vi backup at data regelmessig. We back it up in such a way that it becomes easy to retrieve in case of emergency. Vi sikkerhetskopiere det på en slik måte at det blir lett å hente i nødstilfeller.

I will guide you through writing a script to take daily backups on your system. Jeg vil lede deg gjennom å skrive et skript til å ta daglige sikkerhetskopier på systemet ditt. 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. For denne øvelsen vil jeg bruke en server som kjører Apache web server, en MySQL database server, og har en katalog på / home-partisjonen som inneholder noen data som må sikkerhetskopieres. We will also backup the previous day's Apache access logs in a separate backup directory. Vi vil også backup gårsdagens Apache tilgang logges i en egen backup katalog. First, we need to create a directory where our backups will reside. Først må vi opprette en katalog hvor vår backup vil ligge. As part of the exercise I have created a directory called BACKUP on the /opt partition. Som en del av øvelsen har jeg laget en katalog kalt BACKUP/ opt partisjonen. 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/ . Vi vil organisere sikkerhetskopier i denne mappen, slik at hver dager sikkerhetskopien ligger i en katalog formet som dette - / opt / BACKUP / år / måned / DATE /. So on the 11th of August 2008 the backups will go into /opt/BACKUP/2008/08/11/ . Så på den 11. august 2008 er backup vil gå inn i / opt/BACKUP/2008/08/11 /. Let's create these directories. La oss lage disse katalogene. Run the following set of commands as the root user. Kjør følgende sett med kommandoer som root bruker.

# 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. Se note nedenfor før utstedelse av denne siste kommando.

# 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. Vi har nå to sett med backup kataloger - ett for data og en annen for loggfiler. With the backups directories created we are now ready to start writing the script. Note: I'm using the user calvin throughout this article. Med backup kataloger skapte vi er nå klar til å begynne å skrive manuset. Merk: Jeg bruker brukeren Calvin gjennom denne artikkelen. Please replace calvin with your own username. Vennligst erstatte Calvin med ditt eget brukernavn.


########################################################### ################################################## #########
# CODE BEGINS HERE # CODE BEGINS HERE
########################################################### ################################################## #########

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

# SERVER DATA BACKUP V1.0 # SERVER Data Backup V1.0

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

#Configure the month, date, and day # Konfigurer måned, dato og dag
YEAR=`date +”%Y”` # 2008 ÅR = `date +"% Y "` # 2008
MONTH=`date +”%m”`# 11 MÅNED = `date +"% m "` # 11
DAY=`date +”%d”` # 14 DAG = `date +"% d "` # 14
YESTERDAY=`date –date=”yesterday” +%Y-%m-%d` # 2008-11-13 YESTERDAY = `date-date =" yesterday "+% Y-% m-% d` # 2008-11-13
YESTERDAY_DATE=`date –date=yesterday +%d` # 13 YESTERDAY_DATE = `date-date = igår +% d` # 13
YESTERDAY_MONTH=`date –date=yesterday +%m` # 11 YESTERDAY_MONTH = `date-date = igår +% m` # 11
YESTERDAY_YEAR=`date –date=yesterday +%Y` # 2008 YESTERDAY_YEAR = `date-date = igår +% Y` # 2008
TODAY=`date +%Y-%m-%d` # 2008-11-14 Today = `date +% Y-% m-% d` # 2008-11-14
TODAY_DATE=`date +%d` # 14 TODAY_DATE = `date +% d` # 14

# directories to backup to # Kataloger til backup til
DATA_BACKUP_PARENT_DIR=/opt/backup/DATA/ # <-- Change this your data backup dir DATA_BACKUP_PARENT_DIR = / opt / backup / data / # <- Endre dette til sikkerhetskopiering av data dir
LOG_BACKUP_PARENT_DIR=/opt/backup/LOG/ # <-- Change this your log backup dir LOG_BACKUP_PARENT_DIR = / opt / backup / log / # <- Endre dette din log backup dir

# Apache Document Root Directory # Apache rotkatalogen
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" # <- Endre dette til IP på databaseserveren
DB_USER="calvin" # <-- Change this to your database username DB_USER = "Calvin" # <- Endre dette til databasen brukernavn
DB_PASS="calvin_password" # <-- Change this to your database password DB_PASS = "calvin_password" # <- Endre dette til databasen passord
DB_NAME="clientlist" # <-- Change this to your database name DB_Name = "clientlist" # <- Endre dette til databasen navn

# 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 /" # <- Endre dette til banen dataene dine logger

# Application Files' Dir # Program Files 'Dir
APP_FILES="/home/calvin/application_files/" # <-- Change this to the path of the data directory APP_FILES = "/ home / calvin / application_files /" # <- Endre dette til banen av data katalogen

# Config Dir # Config Dir
CONFIG_DIR="/etc/" # <-- Change this to the path data directory CONFIG_DIR = "/ etc /" # <- Endre dette til banen data katalogen

# Mail report address # Mail rapporten adresse
EMAIL_ID="you@youremailid.com" # <-- Change this to your e-mail ID EMAIL_ID = "you@youremailid.com" # <- Endre dette til din e-ID

########################################################### ################################################## #########
# DATA BACKUP # Data Backup
########################################################### ################################################## #########

# Create and go into backup directory # Lag og går inn backup-katalog
cd $DATA_BACKUP_PARENT_DIR/$YEAR/$MONTH cd $ DATA_BACKUP_PARENT_DIR / $ ÅR / $ MÅNED
mkdir $DAY mkdir $ DAG
cd $DAY cd $ DAG

# 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 tjære-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 tjære-zcf etc.tar.gz / etc

# Application backup # Søknad backup
tar -zcf Apache_Doc_Root.tar.gz ${APACHE_DOCROOT} tjære-zcf Apache_Doc_Root.tar.gz $ (APACHE_DOCROOT)
tar -zcf App_Files.tar.gz ${APP_FILES} tjære-zcf App_Files.tar.gz $ (APP_FILES)

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

# Create and go into backup directory # Lag og går inn backup-katalog
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 for i in $ (APACHE_LOG_DIR) / access_. $ (YESTERDAY) *; do cp $ i.; gjort
for i in access_log.${YESTERDAY}*; do gzip $i; done for i in access_log. $ (YESTERDAY) *; do gzip $ i; gjort

########################################################### ################################################## #########
# Send out mail notifications # Send ut postvarslinger
########################################################### ################################################## #########

# Mail notify # Mail varsle
du -shc $DATA_BACKUP_PARENT_DIR/$YEAR/$MONTH/$DAY/* | mail -s “Backup for ${HOSTNAME}::DATA done du-SHC $ DATA_BACKUP_PARENT_DIR / $ ÅR / MÅNED $ / $ DAY / * | mail-s "Backup for $ (HOSTNAME):: DATA gjort :) ” ${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 gjort :) ” ${EMAIL_ID} "$ (EMAIL_ID)

########################################################### ################################################## #########
# CODE ENDS HERE # CODE ender her
########################################################### ################################################## #########

Save this script in a directory such as /home/calvin/scripts/backup.sh. Lagre dette skriptet i en katalog, for eksempel / home / calvin / scripts / backup.sh. Give executable permissions to the file: Gi kjørbare tillatelser til filen:

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

Now you are good to go. Nå er du god til å gå. Run the script form your command line: Kjør skriptet skjemaet kommandolinje:

# /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. Du kan også eventuelt legge dette skriptet som en cron-jobb slik at det vil bli utført på en daglig basis på et tidspunkt etter eget valg. If you want to run this script at 4 AM daily do the following: Hvis du vil kjøre dette skriptet ved 4 AM daglige gjøre følgende:

# crontab -e # Crontab-e

Create a new line in the file that opens and add the following: Opprett en ny linje i filen som åpnes og legge til følgende:

0 4 * * * /home/calvin/scripts/backup.sh > /dev/null 0 4 * * * / home / calvin / scripts / 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. Serveren vil nå ta backup av dine data, database, konfigurasjonsfiler, program filer og loggfiler til / opt / backup hver dag at 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. En smart ting å gjøre nå ville være å eksportere disse sikkerhetskopiene til en ekstern maskin, slik at du ikke mister din sikkerhetskopier hvis serveren ansikter problemer. We will cover how to do that in my next article. Vi vil dekke hvordan du gjør dette i min neste artikkel.

Posted in Posted in Guest Blogger Guest Blogger , , Linux Linux . .

Related Posts: Relaterte innlegg:

How to encrypt your Linux backups Hvordan kryptere Linux sikkerhetskopier
Windows XP Recovery System Features Windows XP Recovery System Features
How to format and mount a USB hard drive in Linux Hvordan du formaterer og montere en USB-harddisk i Linux
How to block an IP address in IPTables in Linux Slik blokkerer en IP-adresse i iptables på Linux
How to Create a Multi Part Tar File with Linux Opprette en Multi Part Tar Arkiv med Linux

One Response One Response

Stay in touch with the conversation, subscribe to the Hold kontakten med samtalen, abonnere på RSS feed for comments on this post RSS feed for kommentarer til dette innlegget . .

  1. Alberto says Alberto sier

    Hello Hallo
    I'm testing the script, but I've errors with the lines: Jeg tester manuset, men jeg har feil med linjene:

    YESTERDAY=`date –date=”yesterday” +%Y-%m-%d` # 2008-11-13 YESTERDAY = `date-date =" yesterday "+% Y-% m-% d` # 2008-11-13
    YESTERDAY_DATE=`date –date=yesterday+%d` # 13 YESTERDAY_DATE = `date-date = igår +% d` # 13
    YESTERDAY_MONTH=`date –date=yesterday +%m` # 11 YESTERDAY_MONTH = `date-date = igår +% m` # 11
    YESTERDAY_YEAR=`date –date=yesterday +%Y` # 2008 YESTERDAY_YEAR = `date-date = igår +% Y` # 2008

    The error I've is by example: Feilen jeg har er med eksempel:

    mysystem# YESTERDAY=`date .date=.yesterday. mysystem # YESTERDAY = `date. dato =. går. +%Y-%m-%d` # 2008-11-13 +% Y-% m-% d `# 2008-11-13
    date: extra operator `+%Y-%m-%d' dato: ekstra operatør `+% Y-% m-% d '
    Try `date –help' for mor information Prøv `date-help 'for mor informasjon

    I've tested it in : Ubuntu Linux 7.04 Jeg har testet det her: Ubuntu Linux 7.04

    Thanks (and good work !!) Takk (og godt arbeid!)



Some HTML is OK Some HTML is OK

or, reply to this post via eller svare på dette innlegget via trackback styrekule . .