频道直达 - 专题 - 新闻 - 技巧 - 组网 - 开发 - 安全 - web编程 - 图像 - 操作系统 - 数据库 - 教育 - 旅游 - 健康 - 时尚 - 驱动 - 软件 - 游戏 - 多媒体 - ERP - 讨论组

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

来源: 作者: 出处:巧巧读书 2006-08-26 进入讨论组
关 键 词:access  d-link  excel  ie  mysql  
下一页 1 2 

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.

# 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;

This will first remove all files in your previous mysql-backup-dir. Then it will re-create it (I chose to do it this way because one does not have to worry about an existing folder or not...). Then it will loop (as root) through all the databases and create an own .sql file for each database. You may want to adjust the parameters for the backup of the databases or you may just want to use a mysqldump --all-databases which is probably quicker than the looping. However I prefer having single .sql files for all DBs

# RUN RSYNC INTO CURRENT
$RSYNC                                                          \
        -avz --delete --delete-excluded                         \
        --exclude-from="$EXCLUDES"                              \
        -e "$SSH -i $KEY"                                       \
        / $BACKUP_USER:/$BACKUPDIR/current ;
# UPDATE THE TIME TO REFLECT THE SNAPSHOT TIME
$SSH -I $KEY $BACKUP_USER "$TOUCH $$BACKUPDIR/current"
# MAKE HARDLINK COPY
$SSH -i $KEY $BACKUP_USER "$CP -al $BACKUPDIR/current/* $MKDIR"

This now makes a an incremental sync of the files of your production server to the backup server. It will all be stored in the "current" folder, afterwards it will create a hardlink copy to the previously created new "timestamp" folder.

--exclude-from="$EXCLUDES"
EXCLUDES=/backup/backup_exclude

This will act as exclusion for the backup. I attach here my current content of this file.

/backup/
/bin/
/boot/
/dev/
/lib/
/lost+found/
/mnt/
/opt/
/proc/
/sbin/
/sys/
/tmp/
/usr/
/var/log/
/var/spool/
/var/lib/php4/
/var/lib/mysql/

The last thing now needed is a cron that will do all the backups. You can use something like this:

cron.txt (cron control file)

# Make Backups
0 0,6,12,18 * * * sh /backup/backup.sh

The above would make a backup every 6 hours.

更多文章 更多内容请看SSH安全技术  SSH技术手册  SSH实战应用专题,或进入讨论组讨论。
下一页 1 2 
收藏此文】【 】【打印】【关闭
相关图文阅读
频道图文推荐
健 康 咨 询
时 尚 咨 询
巧巧读书宗旨
相关专题
·SSH技术手册 (118篇文章)
·系统备份专题 (14105篇文章)
·SSH安全技术 (7041篇文章)
·城域网专题 (6805篇文章)
·SSH实战应用 (32篇文章)
·SSH相关文章 (153篇文章)
热点标签: access  d-link  excel  ie  mysql  
最新论坛文章
站内各频道最新更新文档
站内最新制作专题
热门关键字导读
Photoshop教 程照片处理 照片制作 PS快捷键 抠图
计 算 机 故 障XP系统修复
艺 术 与 设 计设计 流媒体 设计欣赏 边框
计 算 机 安 全ARP
站内频道文章精选
百度推荐,商机无限
搜索您感兴趣的内容
Web 全站
巧巧电脑频道编辑信箱  告诉我们您想看的专题或文章