Linux中国 Linux中国门户站!
设为主页 设为主页
收藏本站 收藏本站
 
当前位置 :首页 ->Linux技术 ->Linux安全 ->正文

使用Rsync和SSH实现Snapshot型增量备份

来源:howtoforge 作者:Mike Rubel  时间:2007-04-22 点击: [收藏] [投稿]

Author: Stephan Jau <tutorials [at] roleplayer [dot] org>

Based upon the works of: Falko Timme <ft [at] falkotimme [dot] com> & Mike Rubel <webmaster [at] www [dot] mikerubel [dot] org>

Introduction

As neither human nor computers are perfect (humans err / computers may fail) it is quite obvious that a good backup system will prevent too much damage once the computer may go down. This could be either because the harddrive is failing, because of hackers, because you accidentally deleted something important, ...

In this tutorial I will show you how to automate backups in an incremental snapshot-style way by using rSync.

1. Setting up rSync over SSH

First of all you need a running rsync server and client that connect to each other without being required to enter a password. More suitable even to have it run through SSH (you might transfer sensitive data). For this, Falko Timme has already written an excellen howto. You can find it here Mirror Your Web Site With rsync
Since that howto is already excellent there's no point in writing another one about this subject. Follow this howto until Step 6 (6 Test rsync On mirror.example.com) and test whether your setup works.
As I will use two different methods of making this incremental snapshot-style backups it is necessary for one that that the backup server can access to production server without being prompted for a password and for the other one it's vice-versa.
Note: In my case I do backup my data on a friends server and he backs up his data on mine. So in my case I needed to set both anyway.

2. Non-Rotating Backups

In this setup I will tell you how you just keep making backups without rotating them hence never delete anything. For this setup it is mandatory, that the production server can access the backup server without being prompted for a password.

Once you have ensured, that your production server can connect to your backup server without being asked for a password then all you need is a small shell script and a cronjob to actually accomplish the backup.

backup.sh (backup shell script)

#!/bin/bash
unset PATH
# USER VARIABLES
BACKUPDIR=/backup	# Folder on the backup server
KEY=/root/.ssh/id_rsa
MYSQLUSER=root
MYSQLPWD=**********************
MYSQLHOST=localhost
MYSQLBACKUPDIR=/mysql_backup
BACKUP_USER=root@backup.server.com
EXCLUDES=/backup/backup_exclude	# File containing exludes
# PATH VARIABLES
CP=/bin/cp;
MK=/bin/mkdir;
SSH=/usr/bin/ssh;
DATE=/bin/date;
RM=/bin/rm;
GREP=/bin/grep;
MYSQL=/usr/bin/mysql;
MYSQLDUMP=/usr/bin/mysqldump;
RSYNC=/usr/bin/rsync;
TOUCH=/bin/touch;
##                                                      ##
##      --       DO NOT EDIT BELOW THIS HERE     --     ##
##                                                      ##
# CREATING CURRENT DATE / TIME
NOW=`$DATE '+%Y-%m'-%d_%H:%M`
MKDIR=$BACKUPDIR/$NOW/
# CREATE MYSQL BACKUP
# Remove existing backup dir
$RM -Rf $MYSQLBACKUPDIR
# Create new backup dir
$MK $MYSQLBACKUPDIR
#Dump new files
for i in $(echo 'SHOW DATABASES;' | $MYSQL -u$MYSQLUSER -p$MYSQLPWD -h$MYSQLHOST|$GREP -v '^Database$'); do
  $MYSQLDUMP                                                    \
  -u$MYSQLUSER -p$MYSQLPWD -h$MYSQLHOST                         \
  -Q -c -C --add-drop-table --add-locks --quick --lock-tables   \
  $i > $MYSQLBACKUPDIR/$i.sql;
done;
# CREATE NEW BACKUPDIR
$SSH -i $KEY $BACKUP_USER "$MK $MKDIR"
# RUN RSYNC INTO CURRENT
$RSYNC                                                          \
        -avz --delete --delete-excluded                         \
        --exclude-from="$EXCLUDES"                              \
        -e "$SSH -i $KEY"                                       \
        / $BACKUP_USER:/$BACKUPDIR/current ;
# UPDATE THE MTIME TO REFELCT THE SNAPSHOT TIME
$SSH -I $KEY $BACKUP_USER "$TOUCH $$BACKUPDIR/current"
# MAKE HARDLINK COPY
$SSH -i $KEY $BACKUP_USER "$CP -al $BACKUPDIR/current/* $MKDIR"

Explanations:

#!/bin/bash
unset PATH
# USER VARIABLES
BACKUPDIR=/backup	# Folder on the backup server
KEY=/root/.ssh/id_rsa
MYSQLUSER=root
MYSQLPWD=**********************
MYSQLHOST=localhost
MYSQLBACKUPDIR=/mysql_backup
BACKUP_USER=root@backup.server.com
EXCLUDES=/backup/backup_exclude	# File containing exludes
# PATH VARIABLES
CP=/bin/cp;
MK=/bin/mkdir;
SSH=/usr/bin/ssh;
DATE=/bin/date;
RM=/bin/rm;
GREP=/bin/grep;
MYSQL=/usr/bin/mysql;
MYSQLDUMP=/usr/bin/mysqldump;
RSYNC=/usr/bin/rsync;
TOUCH=/bin/touch;

Just set the according variables above. No much explanation needed I think

# CREATING CURRENT DATE / TIME
NOW=`$DATE '+%Y-%m'-%d_%H:%M`
MKDIR=$BACKUPDIR/$NOW/
[...]
# CREATE NEW BACKUPDIR
$SSH -i $KEY $BACKUP_USER "$MK $MKDIR"

This will create a current folder for the backup YYYY-MM-DD_HH:MM - if you want to you can alter the format of this... I just think this ist easy to read.

 如果您对本文有任何疑问或者建议,请到讨论区发表您的意见: >> 论坛入口 <<



上一篇:Linux 引导过程内幕   下一篇:OpenVPN 配置和使用

文章评论】 【收藏本文】 【推荐好友】 【打印本文】 【我要投稿】 【论坛讨论
更多相关文章
Power by linux-cn.com 粤ICP备05006655号