热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

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

文章标题:使用Rsync和SSH实现Snapshot型增量备份。Linux是中国IT实验室的一个技术频道。包含桌面应用,Linux系统管理,内核研究,嵌入式系统和开源等一些基本分类

Author: Stephan Jau

Based upon the works of: Falko Timme & Mike Rubel

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.

 

[1] [2] 下一页


推荐阅读
  • 本文介绍了在开发Android新闻App时,搭建本地服务器的步骤。通过使用XAMPP软件,可以一键式搭建起开发环境,包括Apache、MySQL、PHP、PERL。在本地服务器上新建数据库和表,并设置相应的属性。最后,给出了创建new表的SQL语句。这个教程适合初学者参考。 ... [详细]
  • 本文介绍了如何使用php限制数据库插入的条数并显示每次插入数据库之间的数据数目,以及避免重复提交的方法。同时还介绍了如何限制某一个数据库用户的并发连接数,以及设置数据库的连接数和连接超时时间的方法。最后提供了一些关于浏览器在线用户数和数据库连接数量比例的参考值。 ... [详细]
  • 在说Hibernate映射前,我们先来了解下对象关系映射ORM。ORM的实现思想就是将关系数据库中表的数据映射成对象,以对象的形式展现。这样开发人员就可以把对数据库的操作转化为对 ... [详细]
  • 本文由编程笔记小编整理,介绍了PHP中的MySQL函数库及其常用函数,包括mysql_connect、mysql_error、mysql_select_db、mysql_query、mysql_affected_row、mysql_close等。希望对读者有一定的参考价值。 ... [详细]
  • 本文介绍了Oracle数据库中tnsnames.ora文件的作用和配置方法。tnsnames.ora文件在数据库启动过程中会被读取,用于解析LOCAL_LISTENER,并且与侦听无关。文章还提供了配置LOCAL_LISTENER和1522端口的示例,并展示了listener.ora文件的内容。 ... [详细]
  • Oracle分析函数first_value()和last_value()的用法及原理
    本文介绍了Oracle分析函数first_value()和last_value()的用法和原理,以及在查询销售记录日期和部门中的应用。通过示例和解释,详细说明了first_value()和last_value()的功能和不同之处。同时,对于last_value()的结果出现不一样的情况进行了解释,并提供了理解last_value()默认统计范围的方法。该文对于使用Oracle分析函数的开发人员和数据库管理员具有参考价值。 ... [详细]
  • Java学习笔记之使用反射+泛型构建通用DAO
    本文介绍了使用反射和泛型构建通用DAO的方法,通过减少代码冗余度来提高开发效率。通过示例说明了如何使用反射和泛型来实现对不同表的相同操作,从而避免重复编写相似的代码。该方法可以在Java学习中起到较大的帮助作用。 ... [详细]
  • 本文介绍了在MacOS系统上安装MySQL的步骤,并详细说明了如何设置MySQL服务的开机启动和如何修改MySQL的密码。通过下载MySQL的macos版本并按照提示一步一步安装,在系统偏好设置中可以找到MySQL的图标进行设置。同时,还介绍了通过终端命令来修改MySQL的密码的具体操作步骤。 ... [详细]
  • Oracle Database 10g许可授予信息及高级功能详解
    本文介绍了Oracle Database 10g许可授予信息及其中的高级功能,包括数据库优化数据包、SQL访问指导、SQL优化指导、SQL优化集和重组对象。同时提供了详细说明,指导用户在Oracle Database 10g中如何使用这些功能。 ... [详细]
  • 本文介绍了一个在线急等问题解决方法,即如何统计数据库中某个字段下的所有数据,并将结果显示在文本框里。作者提到了自己是一个菜鸟,希望能够得到帮助。作者使用的是ACCESS数据库,并且给出了一个例子,希望得到的结果是560。作者还提到自己已经尝试了使用"select sum(字段2) from 表名"的语句,得到的结果是650,但不知道如何得到560。希望能够得到解决方案。 ... [详细]
  • 本文介绍了关于apache、phpmyadmin、mysql、php、emacs、path等知识点,以及如何搭建php环境。文章提供了详细的安装步骤和所需软件列表,希望能帮助读者解决与LAMP相关的技术问题。 ... [详细]
  • 高质量SQL书写的30条建议
    本文提供了30条关于优化SQL的建议,包括避免使用select *,使用具体字段,以及使用limit 1等。这些建议是基于实际开发经验总结出来的,旨在帮助读者优化SQL查询。 ... [详细]
  • 本文介绍了通过mysql命令查看mysql的安装路径的方法,提供了相应的sql语句,并希望对读者有参考价值。 ... [详细]
  • SpringBoot整合SpringSecurity+JWT实现单点登录
    SpringBoot整合SpringSecurity+JWT实现单点登录,Go语言社区,Golang程序员人脉社 ... [详细]
  • LVS实现负载均衡的原理LVS负载均衡负载均衡集群是LoadBalance集群。是一种将网络上的访问流量分布于各个节点,以降低服务器压力,更好的向客户端 ... [详细]
author-avatar
噬血--男爵_380_203
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有