How to create automated Linux/Unix backups如何创建自动化的Linux / Unix系统备份

by Sukrit Dhandhania on November 18, 2008 Sukrit Dhandhania 08年11月18号

Linux操作系统

“A stitch in time saves nine”, goes an old saying. “一个缝的时间节省了9个” ,不用一句老话。 In the world of computers we refer to that stitch as 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.当生产运行的IT基础设施,这是我们必须实现正常运行时间在我们的服务器和应用接近百分之百的。 While there are a number of ways to make an IT infrastructure solid, it is impossible to make it failure proof.虽然有一些办法,使一个坚实的IT基础设施,这是不可能使它未能举证责任。 So we make a plan B.所以,我们作计划湾

Servers are made of three broad layers - the hardware, the operating system and applications, and the data.服务器是三大层-硬件,操作系统和应用程序和数据。 In case of a hardware failure it is rather simple to replace the machine with a new one.如硬件故障,这是相当简单,以取代机一台新的。 Installing the operating system and applications too are relatively simple.安装操作系统和应用程序也相对简单。 The part that is hard to replace is the data and configuration that your applications use.这一部分是难以替代的数据和配置您的应用程序使用。 In case of failure this is the part that can not be replaced.万一发生事故,这是一部分,不能被取代。 So we backup that data on a regular basis.所以,我们备份数据,定期通报。 We back it up in such a way that it becomes easy to retrieve in case of emergency.我们它备份起来,以这样一种方式,它很容易检索在紧急情况下。

I will guide you through writing a script to take daily backups on your 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.此行使我将使用一个服务器上运行Apache Web服务器, MySQL数据库服务器,并有一个目录/家庭分区包含了一些数据,这些数据需要备份。 We will also backup the previous day’s Apache access logs in a separate backup directory.我们也将备份的是前一天的Apache的访问日志在一个单独的备份目录。 First, we need to create a directory where our backups will reside.首先,我们需要建立一个目录下的备份将居住。 As part of the exercise I have created a directory called BACKUP on the /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/ .我们将安排在这个备份目录,以使每个居住天的备份目录中形成这样的-/择/备份/年/月/日期/ 。 So on the 11th of August 2008 the backups will go into /opt/BACKUP/2008/08/11/ .因此,在08年8月11号的备份将进入/ opt/BACKUP/2008/08/11 / Let’s create these directories.让我们建立这些目录。 Run the following set of commands as the root user.运行下面的命令设置为root用户

# mkdir -p /opt/backup/DATA/2008目录一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目录01 02 03 04 05 06 07 08 09 10 10 11 12
# cd /opt/backup/DATA/的CD /选择/备份/数据/
# cp -r 2008 2009蛋白- R的2008年2009年
# cd /opt/backup/的CD /选择/备份/
# cp -r DATA LOG蛋白- R的数据日志

See note below before issuing this last command.见附注低于发行前的最后命令。

# chown -R calvin.calvin /opt/backupchown - R的calvin.calvin /选择/备份

We now have two sets of backup directories - one for data and another for log files.我们现在有两套备份目录-一个数据,另一个是为日志文件。 With the backups directories created we are now ready to start writing the script. Note: I’m using the user calvin throughout this article.随着创建备份目录我们现在正在准备开始写剧本。 注:我使用的用户卡尔文整个这一条。 Please replace calvin with your own username.请取代卡尔文您自己的用户名。


########################################################### ################################################## #########
# CODE BEGINS HERE #代码从这里开始
########################################################### ################################################## #########

#!/bin/bash # ! /斌/庆祝

# SERVER DATA BACKUP V1.0 #服务器数据备份1.0版

#########################
# BACKUP CONFIG #备份配置
#########################

#Configure the month, date, and day #配置月份,日期和一天
YEAR=`date +”%Y”` # 2008一年= `日期+ “ % Y ”型` # 2008年
MONTH=`date +”%m”`# 11月= `日期+ “ %米” ` # 11
DAY=`date +”%d”` # 14日期= `日期+ “ % d个” ` # 14
YESTERDAY=`date –date=”yesterday” +%Y-%m-%d` # 2008-11-13昨天= `日期日期= “昨天” + % Y型%米% d个` # 2008年十一月13日
YESTERDAY_DATE=`date –date=yesterday +%d` # 13 YESTERDAY_DATE = `日期日期=昨天+ % d个` # 13
YESTERDAY_MONTH=`date –date=yesterday +%m` # 11 YESTERDAY_MONTH = `日期日期=昨天+ %米` # 11
YESTERDAY_YEAR=`date –date=yesterday +%Y` # 2008 YESTERDAY_YEAR = `日期日期=昨天+ % Y ` # 2008年
TODAY=`date +%Y-%m-%d` # 2008-11-14今日= `日期+ % Y型%米% d个` # 08年11月14日
TODAY_DATE=`date +%d` # 14 TODAY_DATE = `日期+ % d个` # 14

# directories to backup to #目录备份
DATA_BACKUP_PARENT_DIR=/opt/backup/DATA/ # <-- Change this your data backup dir DATA_BACKUP_PARENT_DIR = /选择/备份/数据/ # “ -改变这一您的数据备份目录
LOG_BACKUP_PARENT_DIR=/opt/backup/LOG/ # <-- Change this your log backup dir LOG_BACKUP_PARENT_DIR = /选择/备份/日志/ # “ -改变这一您的日志备份目录

# Apache Document Root Directory # Apache的文件根目录
APACHE_DOCROOT=/var/www/html/ APACHE_DOCROOT =是/ var /的www /的HTML /

# MySQL Database config # MySQL数据库配置
DB_IP="localhost" # <-- Change this to the IP of your database server DB_IP = “ localhost ”的# “ -这一变化的I P你的数据库服务器
DB_USER="calvin" # <-- Change this to your database username DB_USER = “卡尔文” # “ -改变你的这个数据库的用户名
DB_PASS="calvin_password" # <-- Change this to your database password DB_PASS = “ calvin_password ” # “ -改变这一到您的数据库密码
DB_NAME="clientlist" # <-- Change this to your database name DB_NAME = “ clientlist ” # “ -改变这一到您的数据库名称

# Apache Log Dir # Apache日志目录
APACHE_LOG_DIR="/var/log/httpd/" # <-- Change this to the path data of your logs APACHE_LOG_DIR = “是/ var /日志/的httpd / ” # “ -这一变化的路径数据的记录

# Application Files' Dir #应用程序文件的目录
APP_FILES="/home/calvin/application_files/" # <-- Change this to the path of the data directory APP_FILES = “ /家庭/卡尔文/ application_files / ” # “ -改变这一路径的数据目录

# Config Dir #配置迪尔
CONFIG_DIR="/etc/" # <-- Change this to the path data directory CONFIG_DIR = “的/ etc / ” # “ -这一变化的路径数据目录

# Mail report address #邮件地址的报告
EMAIL_ID="you@youremailid.com" # <-- Change this to your e-mail ID EMAIL_ID = “ you@youremailid.com ” # “ -改变这种电子邮件编号

########################################################### ################################################## #########
# DATA BACKUP #数据备份
########################################################### ################################################## #########

# Create and go into backup directory #创建并进入备份目录
cd $DATA_BACKUP_PARENT_DIR/$YEAR/$MONTH裁谈会$ DATA_BACKUP_PARENT_DIR / $年/ $一个月
mkdir $DAY目录$天
cd $DAY裁谈会$天

# Database backup #数据库备份
mysqldump -h ${DB_IP} -u ${DB_USER} -p${DB_PASS} ${DB_NAME} > ${DB_NAME}.db mysqldump小时$ ( DB_IP )聚氯乙烯$ ( DB_USER )一p $ ( DB_PASS ) $ ( DB_NAME ) “ $ ( DB_NAME ) 。分贝
tar -zcvf ${DB_NAME}.tar.gz ${DB_NAME}.db焦油zcvf $ ( DB_NAME ) 。 tar.gz $ ( DB_NAME ) 。分贝
rm -f ${DB_NAME}.db九龙口$ ( DB_NAME ) 。分贝

# /etc backup # /等备份
tar -zcf etc.tar.gz /etc焦油zcf etc.tar.gz /等

# Application backup #应用备份
tar -zcf Apache_Doc_Root.tar.gz ${APACHE_DOCROOT}焦油zcf Apache_Doc_Root.tar.gz $ ( APACHE_DOCROOT )
tar -zcf App_Files.tar.gz ${APP_FILES}焦油zcf App_Files.tar.gz $ ( APP_FILES )

########################################################### ################################################## #########
# LOG BACKUP #日志备份
########################################################### ################################################## #########

# Create and go into backup directory #创建并进入备份目录
cd $LOG_BACKUP_PARENT_DIR/$YESTERDAY_YEAR/$YESTERDAY_MONTH裁谈会$ LOG_BACKUP_PARENT_DIR / $ YESTERDAY_YEAR / $ YESTERDAY_MONTH
mkdir $YESTERDAY_DATE目录$ YESTERDAY_DATE
cd $YESTERDAY_DATE裁谈会$ YESTERDAY_DATE

# HTTPD Log Backup #的httpd日志备份
for i in ${APACHE_LOG_DIR}/access_log.${YESTERDAY}*; do cp $i .; done我在$ ( APACHE_LOG_DIR ) / access_log 。 $ (昨天) * ;做基因$ Ⅰ 。 ;做
for i in access_log.${YESTERDAY}*; do gzip $i; done我在access_log 。 $ (昨天) * ;做gzip格式压缩$一;做

########################################################### ################################################## #########
# Send out mail notifications #发送电子邮件通知
########################################################### ################################################## #########

# Mail notify #邮件通知
du -shc $DATA_BACKUP_PARENT_DIR/$YEAR/$MONTH/$DAY/* | mail -s “Backup for ${HOSTNAME}::DATA done :)” ${EMAIL_ID}杜特困$ DATA_BACKUP_PARENT_DIR / $年/ $月/ $日/ * |邮件的“备份主机$ ( ) : :数据做: ) ” $ ( EMAIL_ID )
du -shc $LOG_BACKUP_PARENT_DIR/$YESTERDAY_YEAR/$YESTERDAY_MONTH/$YESTERDAY_DATE/* | mail -s “Backup for ${HOSTNAME}::LOG done :)” ${EMAIL_ID}杜特困$ LOG_BACKUP_PARENT_DIR / $ YESTERDAY_YEAR / $ YESTERDAY_MONTH / $ YESTERDAY_DATE / * |邮件的“备份主机$ ( ) : :登录完成: ) ” $ ( EMAIL_ID )

########################################################### ################################################## #########
# CODE ENDS HERE #代码在这里结束
########################################################### ################################################## #########

Save this script in a directory such as /home/calvin/scripts/backup.sh.此脚本保存在一个目录中,如/家庭/卡尔文/脚本/ backup.sh 。 Give executable permissions to the file:给予可执行权限的文件:

# chmod +x /home/calvin/scripts/backup.sh属性+ x的/家庭/卡尔文/脚本/ backup.sh

Now you are good to go.现在,你是好去。 Run the script form your command line:运行该脚本形式的命令行:

# /home/calvin/scripts/backup.sh / 家庭/卡尔文/脚本/ 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.您还可以随意添加此脚本为Cron Job如此,这将是执行日常的时候您的选择。 If you want to run this script at 4 AM daily do the following:如果您想运行此脚本在凌晨4点每天做到以下几点:

# crontab -ecrontab娥

Create a new line in the file that opens and add the following:创建一个新的生产线中的文件,打开并加入以下内容:

0 4 * * * /home/calvin/scripts/backup.sh > /dev/null 0 4 * * * /家庭/卡尔文/脚本/ backup.sh “的/ dev /无效

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.您的服务器现在将您的数据备份,数据库,配置文件,申请文件和日志文件/退出/备份每天凌晨4点。 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.智能事情现在要做的是将出口这些备份到远程机器,让您不会失去您的备份,如果您的服务器所面临的问题。 We will cover how to do that in my next article.我们将涵盖该怎么做我的下篇文章。

Related Posts: 相关文章:
  • Increase your Linux/Unix Productivity: How to use crontab提高您的Linux / Unix的生产力:如何使用crontab
  • Walkthrough: Time Machine for OS X 10.5 Leopard演练:时间机器针对OS X 10.5 Leopard的
  • How to backup your files to your Dreamhost account (OS X)如何备份您的文件到您的Dreamhost帐户( OS X的)
  • How to create a custom Windows Vista Installation DVD如何创建自定义的Windows Vista安装的DVD
  • Review: BackBlaze backup service - 25 Beta Invites for Simple Help readers修改: BackBlaze备份服务-2 5贝塔请简单说明读者
  • Get Simple Help tutorials just like this one in your email inbox every day - for free!获取简单的帮助教程就这样一个在您的电子邮件收件匣每一天-是免费的! Just enter your email address below:只要输入您的电子邮件地址如下:

    You can always opt out of this email subscription at any time.您可以随时退出这个邮件订阅在任何时候。


    Bookmark and Share 书签和共享

    { 0 comments… 0评论... add one now现在添加一个 }

    Leave a 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> 您可以使用这些HTML标记和属性: href="" title="">的<a <abbr title=""> <acronym title="">的<b> <blockquote cite=""> <cite>的<code> <del datetime="">的<em> <i> <q cite=""> <strike>的<strong>