FreeBSD安装Rsnapshot文件系统快照备份实用程序

时间:2020-01-09 10:40:21  来源:igfitidea点击:

如何在FreeBSD服务器上安装基于rsync的rsnapshot文件系统快照实用程序,以备份本地/远程Linux和基于Unix的台式机,服务器和笔记本电脑系统?

rsnapshot是基于rsync命令的文件系统快照实用程序。

rsnapshot使通过ssh制作本地计算机和远程计算机的定期快照变得容易。
该代码尽可能使用硬链接,以大大减少所需的磁盘空间。
它是完全用perl编写的,没有模块依赖性,并且已经通过Perl版本5.004至5.8.1进行了测试。
在本教程中,您将学习如何创建基于FreeBSD的备份服务器,以每小时,每天,每月和每年的格式保存备份。

在FreeBSD服务器上安装rsnapshot

执行以下命令以更新pkg数据库:

# pkg update

输出示例:

Updating FreeBSD repository catalogue...
FreeBSD repository is up-to-date.
All repositories are up-to-date.

要安装二进制软件包,请输入以下pkg命令:

# pkg install rsnapshot

要添加软件包类型pkg install rsnapshot命令

另外,我们也可以使用FreeBSD端口集合安装它:

# cd /usr/ports/sysutils/rsnapshot/
# make install clean

在FreeBSD上配置rsnapshot

首先,使用cp命令将/usr/local/etc/rsnapshot.conf.default复制到/usr/local/etc/rsnapshot.conf中:

# cp -v /usr/local/etc/rsnapshot.conf.default /usr/local/etc/rsnapshot.conf
/usr/local/etc/rsnapshot.conf.default -> /usr/local/etc/rsnapshot.conf

接下来,编辑/usr/local/etc/rsnapshot.conf,执行:

注意!此文件需要在元素之间使用制表符,以避免语法错误。

# vi /usr/local/etc/rsnapshot.conf

更新和取消注释,如下所示:

# All snapshots will be stored under this root directory.
snapshot_root   /securebackup/root.user.only/rsnapshot/
 
#########################################
#     BACKUP LEVELS / INTERVALS         #
# Must be unique and in ascending order #
# e.g. alpha, beta, gamma, etc.         #
#########################################
# 6 hourly snapshots - daily (every 4 hour)
# 1 daily snapshot - daily 
# 4 weekly snapshots - weekly (Sunday)
# 12 monthly snapshots - 1st day of every month
################################################
 
retain  daily   7
retain  hourly  6
retain  monthly 12
retain  weekly  4
 
# If you enable this, data will be written to the file you specify. 
logfile /var/log/rsnapshot
 
# Exclude these files from backups
exclude_file    /usr/local/etc/rsnapshot.exclude.txt
 
###################################
### SET BACKUP POINTS / SCRIPTS ###
###################################
 
# LOCALHOST/nas04 Backup our own local freebsd box first
backup  /home/          nas04/
backup  /etc/           nas04/
backup  /usr/local/     nas04/
 
# Okay backup our remote web server called 10.12.249.194
backup  [email protected]:/etc/        wwwserver1/
backup  [email protected]:/root/       wwwserver1/
backup  [email protected]:/home/       wwwserver1/
backup  [email protected]:/scripts/    wwwserver1/
backup  [email protected]:/usr/share/lxc/      wwwserver1/
backup  [email protected]:/lib/ufw/    swwwserver1/
backup  [email protected]:/var/www/    swwwserver1/
 
# Backup mysql database too
backup_script   /usr/bin/ssh [email protected] "/scripts/mysql/backup.nas.sh"        unused1/
backup_script   /usr/bin/scp -r [email protected]:/backups/mysql/nas/ /securebackup/root.user.only/rsnapshot/tmp/    mysql/wwwserver1/
 
# Backup v.backup2 server too
backup  [email protected]:/root/   backup2/
backup  [email protected]:/etc/    backup2/
backup  [email protected]:/var/www/    backup2/

在10.12.249.194远程服务器中安装mysql备份帮助程序脚本

在MySQL服务器上的/scripts/mysql/backup.nas.sh位置创建如下脚本:

#!/bin/bash
# Name: backup.nas.sh
# Author:  <www.theitroad.local> under GPL v2.x+
# Purpose: Backup mysql databases for rsnapshot
# Tested on: RHEL/CentOS/Debian/Ubuntu only 
# --------------------------------------------------------
MUSER='YOUR-USERNAME-HERE'
MPASS='YOUR-PASSWORD-HERE'
MHOST='YOUR-HOSTNAME-HERE'
DEST="/backups/mysql/nas"
NOWFORMAT="%m_%d_%Y_%H_%M_%S%P"
MYSQL="/usr/bin/mysql"
MYSQLDUMP="/usr/bin/mysqldump"
MKDIR="/bin/mkdir"
RM="/bin/rm"
GZIP="/bin/gzip"
DATE="/bin/date"
SED="/bin/sed"
 
[  ! -d "$DEST" ] && $MKDIR -p "$DEST"
 
DBS="$($MYSQL -u $MUSER -h $MHOST -p$MPASS -Bse 'show databases')"
DBS="$($SED -e 's/performance_schema//' -e 's/information_schema//' <<<$DBS)"
$RM -f $DEST/* >/dev/null 2>&1
for db in $DBS
do
                tTime=$(date +"${NOWFORMAT}")
                FILE="$DEST/${db}.${tTime}.gz"
                $MYSQLDUMP -u $MUSER -h $MHOST -p$MPASS $db | $GZIP -9 > $FILE
done

在FreeBSD服务器上安装钥匙串

执行以下命令,将用户友好的前端安装到ssh-agent上,以减少登录远程服务器的密码:

# pkg install keychain

输出示例:

Updating FreeBSD repository catalogue...
FreeBSD repository is up to date.
All repositories are up to date.
The following 1 package(s) will be affected (of 0 checked):
 
New packages to be INSTALLED:
	keychain: 2.8.5
 
Number of packages to be installed: 1
 
35 KiB to be downloaded.
 
Proceed with this action? [y/N]: y
[rsnapshot] [1/1] Fetching keychain-2.8.5.txz: 100%   35 KiB  36.1kB/s    00:01    
Checking integrity... done (0 conflicting)
[rsnapshot] [1/1] Installing keychain-2.8.5...
[rsnapshot] [1/1] Extracting keychain-2.8.5: 100%

在shell启动文件中添加以下条目。
我以root用户在FreeBSD上使用默认csh。
对于rsa键,执行:

## repalce 'webserver' with actual `hostname` ##
echo '/usr/local/bin/keychain --clear $HOME/.ssh/id_rsa' >> /root/.cshrc 
echo 'source $HOME/.keychain/webserver-csh' >> /root/.cshrc

如果您使用的是ed25519键,请尝试:

## repalce 'webserver' with actual `hostname` ##
echo '/usr/local/bin/keychain --clear $HOME/.ssh/id_ed25519' >> /root/.cshrc 
echo 'source $HOME/.keychain/webserver-csh' >> /root/.cshrc

仅在FreeBSD盒上安装和配置ssh密钥

您需要设置公共密钥身份验证并使用钥匙串来管理您的密钥:

# ssh-keygen -t ed25519
## For rsa key type ##
# ssh-keygen -t rsa

输出示例:

Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):      
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase): {ENTER-YOUR-PASSPHRASE-HERE}
Enter same passphrase again: {again-ENTER-YOUR-PASSPHRASE-HERE}
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
5b:74:30:c8:4a:10:78:a3:eb:ce:bd:ee:87:40:82:7f root@webserver
The key's randomart image is:
+--[ RSA 2048]----+
|   .oo . .o      |
|  . o . o  o     |
|.  o o .  . .    |
|o o   .  . .     |
| + .    S .      |
|  + E    o       |
| . o .  .        |
| .... .          |
| .oo=o           |
+-----------------+

您需要设置密钥对的位置和名称。
如果那里没有其他密钥,建议您使用默认位置,例如:$HOME/.ssh/id_rsa。
系统将提示您提供私钥的密码(密码)。
我建议您在出现提示时设置密码。
将公用密钥安装在名为v.backup2和10.12.249.194的远程服务器中。
首先,安装一个小的脚本ssh-copy-id,它将您的ssh公钥复制到远程主机,运行:

### only needed on an older version of FreeBSD. Skip it on the FreeBSD 11.x/12.x server and above ###
# pkg install ssh-copy-id

输出示例:

Updating FreeBSD repository catalogue...
FreeBSD repository is up-to-date.
All repositories are up-to-date.
The following 1 package(s) will be affected (of 0 checked):
 
New packages to be INSTALLED:
	ssh-copy-id: 5.6p1
 
The process will require 2 KiB more space.
2 KiB to be downloaded.
 
Proceed with this action? [y/N]: y
[webserver] Fetching ssh-copy-id-5.6p1.txz: 100%    2 KiB   2.5kB/s    00:01    
Checking integrity... done (0 conflicting)
[webserver] [1/1] Installing ssh-copy-id-5.6p1...
[webserver] [1/1] Extracting ssh-copy-id-5.6p1: 100%

现在,安装发布密钥:

# ssh-copy-id -i $HOME/.ssh/id_rsa.pub [email protected]
# ssh-copy-id -i $HOME/.ssh/id_rsa.pub [email protected]

在FreeBSD上测试您的配置

执行以下命令:

# rsnapshot configtest
# rsnapshot -c /usr/local/etc/rsnapshot.conf configtest

输出示例:

Syntax OK

您可以看到此实用程序将执行的shell命令:

# rsnapshot -t hourly
# rsnapshot -t daily
# rsnapshot -t weekly
# rsnapshot -t monthly

运行rsnapshot并在FreeBSD上进行首次备份

执行以下命令进行每日备份:

# rsnapshot -v daily

例如,可以每小时进行一次备份,如下所示:

# rsnapshot -v daily

配置cron在FreeBSD上运行备份

执行以下命令以配置备份cron作业:

# crontab -e

追加以下条目:

30 */4 * * *        /usr/local/bin/rsnapshot hourly
40 21 * * *         /usr/local/bin/rsnapshot daily
20 22 * * 6         /usr/local/bin/rsnapshot weekly
10 23 1 * *         /usr/local/bin/rsnapshot monthly

保存并关闭文件。

如何还原备份?

假设您要恢复wwwserver1的备份。
如下执行命令(从ls -l输出中选择日期和日期):

#############################
# Login to your FreeBSD server #
# cd to backup location #
# Select hourly, weekly etc #
# scp or rsync to destination #
###################################
# cd /securebackup/root.user.only/rsnapshot/
# ls -l
# cd hourly.0/wwwserver1/
# scp -r var/www/ [email protected]:/var/www/

如何查看rsnapshot日志文件?

执行以下命令:

# grep 'text-here' /var/log/rsnapshot
# tail -f /var/log/rsnapshot

如何查看备份磁盘的使用情况

# rsnapshot du

如何比较两个最新的快照/备份?

执行以下命令:

# rsnapshot diff

输出示例:

Comparing /securebackup/root.user.only/rsnapshot/hourly.1 to /securebackup/root.user.only/rsnapshot/hourly.0
Between /securebackup/root.user.only/rsnapshot/hourly.1 and /securebackup/root.user.only/rsnapshot/hourly.0:
  11483 were added, taking 25777440 bytes
  11483 were removed, saving 25776712 bytes

它还接受两个可选参数,它们可以是snapshot_root中的文件系统路径或备份目录:

# rsnapshot diff dir1 dir2

dir1和dir2是唯一的强制性参数,应为要比较的两个目录的名称。
它们的顺序无关紧要,rsnapshot-diff将始终将较新的文件与较旧的文件进行比较,因此仅显示在较旧文件中的文件将被报告为已删除,而仅显示在较新文件中的文件将被报告为已添加。