rsync expect_使用Expect和Rsync备份

rsync expect

Rsync alone is a powerful tool for moving data within and among local and remote servers. When combined with Expect, interactive sessions can be automated on the command line for very useful purposes.

单独使用Rsync是在本地和远程服务器之间以及之间移动数据的强大工具。 当与Expect结合使用时,交互式会话可以在命令行上实现非常有用的目的,从而实现自动化。

I find the most powerful application being the process of hot backups to remote servers each evening. While optical disk backups are run for offline archiving and data preservation, I find most the most useful backups being those held on secured remote servers managed via Expect and Rsync.

我发现最强大的应用程序是每天晚上热备份到远程服务器的过程。 运行光盘备份以进行脱机归档和数据保存时,我发现最有用的备份是保存在通过Expect和Rsync管理的安全远程服务器上的备份。

Expect – found at http://expect.nist.gov/ – simply enables automated responses to prompts at the command line when user intervention is not necessary or reasonable. Thus, overnight backups can be run, enabling remote authentication and a password entry when prompted (when ‘expected’ – hence the name). More information can be found by running man expect on the command line.

http://expect.nist.gov/上找到的Expect –仅在不需要或不合理地进行用户干预时,可以对命令行提示进行自动响应。 因此,可以运行通宵备份,从而在提示时(“预期”时,即名称)启用远程身份验证和密码输入。 在命令行上运行man Expect可以找到更多信息。

These files are appended with the .exp extension and as you will see in the scripts below, use a shebang at the top to call the Expect program. I set permissions on these scripts as executable by root only.

这些文件将附加.exp扩展名,如下面的脚本所示,请在顶部使用shebang调用Expect程序。 我将这些脚本的权限设置为只能由root用户执行。

Rsync – found at http://samba.anu.edu.au/rsync/ is a tool for data synchronization, copying of files and so on. More information can be found by running man rsync on the command line.

Rsync –可在http://samba.anu.edu.au/rsync/找到,它是用于数据同步,文件复制等的工具。 通过在命令行上运行man rsync可以找到更多信息。

Rsync can be advantageous as it will perform differential backups once a master copy is completed, backing up files that have changed by reviewing the datestamp and file size, minimizing bandwidth and time frames in which backups need to run. Running a tape or optical backup drive can be configured with the remote backup server to further free up overnight resources for your production web server.

Rsync具有优势,因为它会在完成主副本后执行差异备份,通过查看日期戳和文件大小来备份已更改的文件,从而最小化需要运行备份的带宽和时间范围。 可以使用远程备份服务器配置运行磁带或光盘备份驱动器,以进一步释放生产Web服务器的通宵资源。

When used in conjunction, a straight forward backup scheme can be implemented for web servers (among other systems).

结合使用时,可以为Web服务器(以及其他系统)实现直接备份方案。

Here are the scripts in use. They are called from the backup server to the target server and data is stored in ‘hot backup’ for easy access:

这是正在使用的脚本。 它们从备份服务器调用到目标服务器,并且数据存储在“热备份”中以便于访问:

Script 1 – MySQL Database backup

脚本1 – MySQL数据库备份

#!/usr/bin/expect set timeout 19900 spawn /bin/bash expect -re "]# " send "rsync -avzb -e ssh root@domain.com:/var/db/mysql/dumps/ /backup/servers/comain.com/mysqlr" expect -re "password:" sleep 2 send "rootpasswordr" expect -re "total size is" expect -re "]# " send "exitr"

#!/usr/bin/expect set timeout 19900 spawn /bin/bash expect -re "]# " send "rsync -avzb -e ssh root@domain.com :/var/db/mysql/dumps/ /backup/servers/comain.com/mysqlr" expect -re "password:" sleep 2 send "rootpasswordr" expect -re "total size is" expect -re "]# " send "exitr"

Now for backing up domain data (web sites), a second script is run:

现在用于备份域数据(网站),运行另一个脚本:

Script 2 – Domain backups

脚本2 –域备份

#!/usr/bin/expect set timeout 19900 spawn /bin/bash expect -re "]# " send "rsync -avzb -e ssh root@domain.com:/home/sites /backup/servers/domain.com/homer" expect -re "password:" sleep 2 send "rootpasswordr" expect -re "total size is" expect -re "]# " send "exitr"

#!/usr/bin/expect set timeout 19900 spawn /bin/bash expect -re "]# " send "rsync -avzb -e ssh root@domain.com :/home/sites /backup/servers/domain.com/homer" expect -re "password:" sleep 2 send "rootpasswordr" expect -re "total size is" expect -re "]# " send "exitr"

To deconstruct this:

要对此进行解构:

1) Expect is called (your path may be different – reveal it by issuing ‘whereis expect’ on the command line)

1)调用了Expect(您的路径可能有所不同–通过在命令行上发出“ whereis Expect”来显示它)

2) send the command to the remote server over ssh:

2)通过ssh将命令发送到远程服务器:

rsync options are -a (archive which preserves permissions and links among other items), v (verbose), z (compress) and b (backup – adds a ~ to preexisting destination files, a sort of versioning of backups)

rsync选项是-a(用于保留权限和其他项目之间链接的归档文件),v(详细),z(压缩)和b(备份–在现有目标文件中添加〜,是备份的一种版本)

3) an ssh password is ‘expected’, or in other cases, authorized keys can be copied over to the remote server to bypass a password prompt for ssh.

3)“期望” ssh密码,或者在其他情况下,可以将授权密钥复制到远程服务器上,以绕过ssh的密码提示。

4) a password is expected – which is sent via Expect to the server for authentication and the operation is carried out.

4)需要输入密码-通过密码将其发送到服务器进行身份验证,然后执行操作。

5) The script completes and closes.

5)脚本完成并关闭。

I have named these files mysql.backup.exp and domains.backup.exp and entered them into cron to run on a daily schedule.

我已将这些文件命名为mysql.backup.exp和domains.backup.exp并将它们输入cron以按日程运行。

While this should not be the only backup procedure – it can be very useful to have a live server available for quick restores or review of backup data.

尽管这不是唯一的备份过程,但拥有可用于快速还原或查看备份数据的实时服务器可能非常有用。

Additional scripts can also be written to backup critical configuration files (i.e. the /etc directory) and other areas of the server(s).

还可以编写其他脚本来备份关键的配置文件(即/ etc目录)和服务器的其他区域。

翻译自: https://www.sitepoint.com/backing-up-using-expect-and-rsync/

rsync expect

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值