数据安全是做数据分析的人需要关注的一大问题。对于我们分析的关键数据、使用的关键脚本都需要定期备份。

scp

最简单的备份方式,就是使用cp (本地硬盘)或scp (远程硬盘)命令,给自己的结果文件新建一个拷贝;每有更新,再拷贝一份。具体命令如下:

cp -fur source_project project_bak

scp -r source_project user@remote_server_ip:project_bak

为了实现定期备份,我们可以把上述命令写入crontab程序中,设置每天的晚上23:00执行。对于远程服务器的备份,我们可以配置免密码登录,便于自动备份。后台输入免密码登录服务器,获取免密码登录服务器的方法。

# Crontab format

# MinuteHourDayMonthWeekcommand

# * 表示每分/时/天/月/周

# 每天23:00 执行cp命令

0          23      *       *       *      cp -fur source_project project_bak

# */2 表示每隔2分分/时/天/月/周执行命令

# 每隔24小时执行cp命令

0          */24      *       *       *      cp -fur source_project project_bak

0          0          */1     *        *     scp -r source_project user@remote_server_ip:project_bak

# 另外crotab还有个特殊的时间

# @reboot: 开机运行指定命令

@reboot cmd

rsync

cp或scp使用简单,但每次执行都会对所有文件进行拷贝,耗时耗力,尤其是需要拷贝的内容很多时,重复拷贝对时间和硬盘都是个损耗。

rsync则是一个增量备份工具,只针对修改过的文件的修改过的部分进行同步备份,大大缩短了传输的文件的数量和传输时间。具体使用如下 :

# 把本地project目录下的东西备份到远程服务器的/backup/project目录下

# 注意***个project后面的反斜线,表示拷贝目录内的内容,不在目标目录新建project文件夹。注意与第二个命令的比较,两者实现同样的功能。

# -a: archive mode, quals -rlptgoD

# -r: 递归同步

# -p: 同步时保留原文件的权限设置

# -u: 若文件在远端做过更新,则不同步,避免覆盖远端的修改

# -L: 同步符号链接链接的文件,防止在远程服务器出现文件路径等不匹配导致的软连接失效

# -t: 保留修改时间

# -v: 显示更新信息

# -z: 传输过程中压缩文件,对于传输速度慢时适用

rsync -aruLptvz --delete project/ user@remoteServer:/backup/project

rsync -aruLptvz --delete project user@remoteServer:/backup/

rsync所做的工作为镜像,保证远端服务器与本地文件的统一。如果本地文件没问题,远端也不会有问题。但如果发生误删或因程序运行错误,导致文件出问题,而在同步之前又没有意识到的话,远端的备份也就没了备份的意义,因为它也被损坏了。误删是比较容易发现的,可以及时矫正。但程序运行出问题,则不一定了。

rdiff-backup

这里推荐一个工具rdiff-backup不只可以做增量备份,而且会保留每次备份的状态,新备份和上一次备份的差别,可以轻松回到之前的某个版本。***的要求就是,本地服务器和远端服务器需要安装统一版本的rdiff-backup。另外还有2款工具 duplicity和`Rsnapshot也可以做类似工作,但方法不一样,占用的磁盘空间也不一样,具体可查看原文链接中的比较。

具体的rdiff-backup安装和使用如下 (之前写的是英文,内容比较简单,就不再翻译了):

Install rdiff-backup at both local and remote computers

#installforubuntu, debian

sudo apt-get install python-dev librsync-dev

#self compile

#downlaod rsync-dev fromhttps://sourceforge.net/project/showfiles.php?group_id=56125

tar xvzf librsync-0.9.7.tar.gz

export CFLAGS="$CFLAGS -fPIC"

./configure --prefix=/home/user/rsync --with-pic

make

make install

Install rdiff-backup

#See Reference partfordownload link

# http://www.nongnu.org/rdiff-backup/

python setup.py install --prefix=/home/user/rdiff-backup

#If you complied rsync-dev yourself, please specify the location ofrsync-dev

python setup.py --librsync-dir=/home/user/rsync install --     prefix=/home/user/rdiff-backup

Add exeutable files and python modules to environmental variables

#Addthe following wordsinto.bashrcor.bash_profileoranyother config files

export PATH=${PATH}:/home/user/rdiff-backup/bin

export PYTHONPATH=${PYTHONPATH}:/home/user/rdiff-backup/lib/python2.x/site-packages

#pay attention tothe xinpython2.xofabove line which can be 6or7 dependingon

#the Python version used.

Test environmental variable when executing commands through ssh

sshuser@host'echo ${PATH}'#WhenI run this commandinmylocalcomputer,

#I found onlysystem environmetal variableisused

#andnoneofmy self-defined environmetal variableisused.

#Then, I modified the following linesinfile'SetConnections.py'in

#/home/user/rdiff-backup/lib/python2.x/site-packages/rdiff_backup

#tosetenvironmental explicitlywhenlogin.

#pay attention tothe single quote used insidedoublequote

__cmd_schema = "ssh -C %s 'source ~/.bash_profile; rdiff-backup --server'"

__cmd_schema_no_compress = "ssh %s 'source ~/.bash_profile; rdiff-backup --server'"

#choose the one containsenvironmental variableforrdiff-backupfrom.bash_profileand.bashrc.

Use rdiff-backup

Start backup

rdiff-backup --no-compression --print-statistics user@host::/home/user/source_dir destination_dir

If the destination_dir exists, please add --force like rdiff-backup --no-compression --force --print-statistics user@host::/home/user/source_dir destination_dir. All things in original destination_dir will be depleted.

If you want to exclude or include special files or dirs please specify like --exclude '**trash' or --include /home/user/source_dir/important.

Timely backup your data

Add the above command into crontab (hit 'crontab -e' in terminal to open crontab) in the format like 5 22 */1 * * command which means executing the command at 22:05 everyday.

Restore data

Restore the latest data by running rdiff-backup -r now destination_dir user@host::/home/user/source_dir.restore. Add --force if you want to restore to source_dir.

Restore files 10 days ago by running rdiff-backup -r 10D destination_dir user@host::/home/user/source_dir.restore. Other acceptable time formats include 5m4s (5 minutes 4 seconds) and 2014-01-01 (January 1st, 2014).

Restore files from an increment file by running rdiff-backup destination_dir/rdiff-backup-data/increments/server_add.2014-02-21T09:22:45+08:00.missing user@host::/home/user/source_dir.restore/server_add. Increment files are stored in destination_dir/rdiff-backup-data/increments/server_add.2014-02-21T09:22:45+08:00.missing.

Remove older records to save space

Deletes all information concerning file versions which have not been current for 2 weeks by running rdiff-backup --remove-older-than 2W --force destination_dir. Note that an existing file which has not changed for a year will still be preserved. But a file which was deleted 15 days ago can not be restored after this command. Normally one should use --force since it is used to delete multiple increments at the same time which --remove-older-thanrefuses to do by default.

Only keeps the last n rdiff-backup sessions by running rdiff-backup --remove-older-than 20B --force destination_dir.

Statistics

Lists increments in given golder by rdiff-backup --list-increments destination_dir/.

Lists of files changed in last 5 days by rdiff-backup --list-changed-since 5D destination_dir/.

Compare the difference between source and bak by rdiff-backup --compare user@host::source-dir destination_dir

Compare the sifference between source and bak (as it was two weeks ago) by rdiff-backup --compare-at-time 2W user@host::source-dir destination_dir.

A complete script (automatically sync using crontab)

#!/bin/bash

export PYTHONPATH=${PYTHONPATH}:/soft/rdiff_backup/lib/python2.7/site-packages/

rdiff-backup --no-compression -v5 --exclude '**trash' user@server::source/ bak_dir/

ret=$?

if test $ret -ne 0; then

echo "Wrong in bak"| mutt -s"Wrong in bak"bak@mail.com

else

echo "Right in bak"| mutt -s"Right in bak"bak@mail.com

fi

echo "Finish rdiff-backup $0 ---`date`---">>bak.log 2>&1

echo "`rdiff-backup --exclude '**trash' --compare-at-time 1D user@server::source/ bak_dir/`"| mutt -s"Lists of baked files"bak@mail.com

References

rdiff-backup

duplicity

rsnapshot

http://www.saltycrane.com/blog/2008/02/backup-on-linux-rsnapshot-vs-rdiff/

http://james.lab6.com/2008/07/09/rdiff-backup-and-duplicity/

http://bitflop.com/document/75

http://askubuntu.com/questions/2596/comparison-of-backup-tools

http://www.reddit.com/r/linux/comments/fgmbb/rdiffbackup_duplicity_or_rsnapshot_which_is/

http://serverfault.com/questions/491341/optimize-space-rdiff-backup

Another great post on usage of rdiff-backup

【编辑推荐】

【责任编辑:武晓燕 TEL:(010)68476606】

linux服务器数据同步,Linux服务器数据定期同步和备份方式相关推荐

  1. 运维之道 | Linux rsync 文件同步、Inotify远程实时同步

    Linux rsync 文件同步服务器 与传统的cp.scp.tar备份方式相比,rsync具有安全性高.备份迅速.支持增量备份等优点,通过rsync可以解决对实时性要求不高的数据备份需求,例如定期的 ...

  2. 解析Linux内核源码中数据同步问题丨C++后端开发丨Linux服务器开发丨Linux内核开发丨驱动开发丨嵌入式开发丨内核操作系统

    剖析Linux内核源码数据同步 1.pdflush机制原理 2.超级块同步/inode同步 3.拥塞及强制回写技术 视频讲解如下,点击观看: 解析Linux内核源码中数据同步问题丨C++后端开发丨Li ...

  3. liunx服务器项目迁移,linux服务器数据迁移

    linux服务器数据迁移 内容精选 换一换 华为云帮助中心,为用户提供产品简介.价格说明.购买指南.用户指南.API参考.最佳实践.常见问题.视频帮助等技术文档,帮助您快速上手使用华为云服务. 部分云 ...

  4. linux下用C语言实现TCP/IP服务器与客户端互相发送数据的socket编程

    linux下用C语言实现TCP/IP服务器与客户端互相发送数据的socket编程 server.c #include <sys/stat.h>#include <fcntl.h> ...

  5. linux服务器数据转发,Linux云服务器如何使用iptables做流量转发?

    在云服务器的日常使用过程中,从老服务器迁移数据到新服务器是不可避免的一项运维操作.在新老服务器交替的迁移过程中,由于域名解析生效需要一段时间,难免出现部分流量仍然请求到老服务器上的情况,造成数据出现衔 ...

  6. linux重启服务挂载盘消失,如何处理ECS Linux服务器重启服务器、初始化系统后数据盘不见了的情况...

    本文在介绍如何处理ECS Linux服务器重启服务器.初始化系统后数据盘不见了的情况的基础上,重点探讨了其具体步骤,本文内容紧凑,希望大家可以有所收获. ECS Linux服务器重启服务器.初始化系统 ...

  7. mysql在win服务器上安装linux_MySql数据安装Linux+Windows

    一.Linux系统安装mysql 1. Linux安装mysql服务分两种安装方法: 1.1  源码安装: 优点是安装包比较小,只有十多M,缺点是安装依赖的库多,安装编译时间长,安装步骤复杂容易出错: ...

  8. linux下rsync+inotify实现服务器之间文件实时同步

    先介绍一下rsync与inotify. 1.rsync 与传统的cp.tar备份方式相比,rsync具有安全性高.备份迅速.支持增量备份等优点,通过rsync可以解决对实时性要求不高的数据备份需求,例 ...

  9. Linux实战教学笔记21:Rsync数据同步工具

    原文地址:https://www.cnblogs.com/chensiqiqi/p/6514315.html 目录 第二十一节 Rsync数据同步工具 1.1 Rsync介绍 1.1.1 什么是Rsy ...

最新文章

  1. pytorch bert文本分类_一起读Bert文本分类代码 (pytorch篇 四)
  2. 如何在一个表达式中合并两个字典?
  3. 不会连PPPoE协议都不会配吧?
  4. 为了帮视障人士“看见”,阿里工程师做了哪些努力?
  5. C++类的构造函数和析构函数
  6. Titanic: Machine Learning from Disaster-kaggle入门赛-学习笔记
  7. 第二:Postman做各种类型的http接口测试
  8. python 归纳 (十八)_队列Queue在多线程中使用(二)
  9. 用户体验测试(UX测试)
  10. 2000款学校教师课件培训PPT模板免费下载网址
  11. 第三章 区块链率先敲开金融的大门
  12. python Excel xlsx file; not supported
  13. 自媒体运营转行做数据分析第1年零6个月
  14. android Q屏幕录制,设备音频录制无声
  15. 怎样查看Eclipse是32位还是64位
  16. 目标管理:SMART原则
  17. 令人头疼的背包九讲(1)0/1背包问题
  18. Oracle数据库对小数点的操作
  19. 中文医疗NLP榜单-CBLUE介绍
  20. Fabric创建通道流程解析

热门文章

  1. 项目应当经历的四种开发环境
  2. Android桌面老是跳广告,电脑桌面老是弹出广告怎么办?简单3步轻松屏蔽!
  3. 峰哥买房用的贝壳app,他们的大数据平台如何实现的?
  4. java doc转换docx_JAVA - 将doc文档转为docx文档
  5. 项目管理工具dhtmlxGantt甘特图入门教程(八):数据加载(二)
  6. Install Debian (Etch/testing) in a USB stick 在U 盘中安装 Debian(Etch/testing)(
  7. 湖南省如何参加政府采购网投标?
  8. 笔记本+显示器常用设置总结
  9. 博弈论中的零和对策和非零和对策
  10. “宅经济”催化下的泛娱乐行业,未来将引爆哪些增长点?