介绍
很多时候,我们会在需要备份之前忘记备份,例如在驱动器故障或意外删除文件或数据库的情况下。 服用 频繁备份 是一个可以遵循的好政策。 无论您选择使用其中一台服务器、提供 SFTP 访问的在线备份服务还是其他方法,拥有经济高效且可靠的备份都可以让您高枕无忧。
本文讨论了 InterWorx 上的远程备份、不同方法的优缺点,以及用于管理 SFTP 服务器上的远程备份的自定义包装脚本,并将提供使用脚本解决备份轮换问题的自定义编码解决方案。
什么是 SFTP?
安全文件传输协议 (SFTP),有时也称为 SSH 文件传输协议,既是一种协议(通过特定端口定义的通信标准),也是支持该协议的 Linux 文件传输工具的名称。
SFTP 允许通过 SSH 服务器服务中包含的加密 SSH 传输发送文件,这是 Linux 系统核心功能的一部分。 它被许多其他操作系统品种广泛使用和支持,例如用于 SFTP 服务器部分的 Microsoft Windows Server 版本。
大多数现代文件传输工具都支持 SFTP,例如 FileZilla 和 WinSCP 等等。 除了最新的具有 USB 连接的中端和高端家庭互联网路由器外,还允许连接外部 USB 驱动器用于 SFTP 存储。 大多数现代路由器为核心操作系统运行 Linux 的一些变体,而不管 Web 界面如何。
在 Internet 的早期,安全性不太受关注,文件传输协议 (FTP) 是通过 Internet 进行高效文件传输的标准。 随着越来越多的企业和消费者开始使用互联网,添加安全功能成为一个更加相关和紧迫的问题。
虽然更高版本的 FTP 扩展包含显式 FTP (FTPS),但并非所有服务器或客户端都支持它。 FTPS 使用非标准端口 990,而 SFTP 使用的标准端口是 21 端口。该端口与大多数服务器上的 SSH 服务使用的端口相同。
InterWorx 有两个 SFTP 端口:一个用于默认 SSH 服务,另一个由 专业FTP 端口 24 上的守护进程。两者之间的区别在于 ProFTP 守护进程通过 mod_SFTP 模块允许端口 24 上的 SFTP 服务通过 SiteWorx 控制面板管理(添加/删除)SFTP 用户。
cPanel 服务器会阻止您将其他 SFTP 用户添加到 cPanel 帐户。 在 InterWorx 上,您 SiteWorx FTP 帐户中的新 FTP 用户将自动作为 SFTP 用户工作。
先决条件
本教程将需要以下先决条件:
- Interworx 服务器
- SFTP 服务器凭据
- 基础知识 bash 脚本、变量、SSH 和故障排除
InterWorx 可用备份的优缺点
默认情况下,InterWorx 目前有几种不同的备份解决方案可用。
每个都有其优点和缺点。
包装脚本
包装脚本是一个脚本,其中在运行时为特定程序定义了所有命令行选项。 在这种情况下,它调用脚本将远程 SFTP 服务器挂载和卸载为本地目录,运行备份并处理备份保留。
使用包装脚本来轮换从内置 NodeWorx CLI 工具 /home/interworx/bin/backup.pex 和关联的 cron 创建的备份。 一个 example cron 将在本文后面提供。
查看表
优点 | 缺点 |
---|---|
可以设置自定义备份保留。 | 需要将 SFTP 服务器凭据存储在文本文件系统中。 |
使用额外的包装脚本支持 SFTP。 | 这取决于其他工具,特别是 sshfs 和 expect 的二进制文件。 |
可以自定义 cron 作业和包装器以满足您的需求。 | 恢复帐户(并知道如何使用 CLI 工具)需要挂载远程 SFTP 服务器目录或使用 SFTP 或 SCP 将帐户转移回服务器。 |
* 需要中等水平的技能来调试问题。 | |
属于 Liquid Web Beyond Scope 支持政策。 |
注意:* 其他注意事项,例如需要转义特定字符。 在 Linux 中 bash 解释器中,最常见的转义字符是反斜杠。 如果传递的字符串不在引号内,则特殊字符(如 $ 或 *)前面有反斜杠。 如果您想要密码中的特殊字符,例如 myepass,您必须告诉 bash 特殊字符是文字值。 除此以外, bash 将 e 读取为转义字符。 转义字符 用于颜色代码、终端铃声等等。
全局备份
可以使用 NodeWorx CLI 工具 /home/interworx/bin/backup.pex 直接配置所有 SiteWorx 帐户的全局备份,并通过 cron 进行计划。
查看表
优点 | 缺点 |
---|---|
创建所有帐户的备份。 | 它不能在 NodeWorx 控制面板中管理或修改。 |
高效运行。 | 它仅适用于本地文件系统。 |
没有备份轮换,因此需要手动删除或使用包装脚本删除较旧的备份。 |
SiteWorx 备份
可以在每个 SiteWorx 帐户中安排帐户备份。
查看表
优点 | 缺点 |
---|---|
每个用户都可以配置和安排他们自己的备份。 | 它需要为每个用户配置。 |
可以创建完整或部分备份(例如数据库)。 | 它不支持自动备份轮换,因此需要手动删除较旧的备份。 |
支持使用 FTP 和 SFTP 进行本地或远程备份。 |
NodeWorx 备份插件
可以通过启用新的 NodeWorx Backup 插件 NodeWorx > 插件 并选择 Siteworx-备份-Cron.
查看表
优点 | 缺点 |
---|---|
集成到 NodeWorx GUI 中。 | 还在 beta 测试。 该插件必须手动启用。 |
积极开发以提供额外的备份选项。 | 尚不支持远程备份目标。 |
支持能够配置每日、每周和每月备份保留值。 | |
如果备份目标磁盘空间低于设置的可用空间最小百分比,则可以选择禁用备份。 |
使用 SFTP 的新备份方法
要使用远程 SFTP 服务器备份包装器,我们有两个软件要求:
- 预计: 这个工具对于编写脚本很方便。 当您需要使用脚本回答程序的问题时,它最常用。 在这种情况下,它用于响应带有密码的 SFTP 服务器的登录请求。
- SSHFS (SSH 文件系统):此工具使用 FUSE 将远程 SFTP 服务器挂载到本地文件系统,以将备份视为位于本地驱动器上。
实施备份
安装
验证您的 InterWorx 服务器上是否预安装了 Expect 工具,然后安装 SSHFS。
任何 红帽企业 Linux (RHEL) 运行 InterWorx 的基于 CentOS 的服务器,我们可以使用 yum 或 rpm 来验证软件包是否已安装。
要确定 Expect 包是否在服务器上,请使用名为 expect 的包运行 rpm 命令。 下面的输出表明服务器上没有安装 Expect 包。
$ rpm -q expect
package expect is not installed
运行与以前相同的 rpm 命令,但使用 fuse-sshfs 而不是 expect。 根据输出,服务器上也没有安装 SSHFS。
$ rpm -q fuse-sshfs
package fuse-sshfs is not installed
如果安装了两个软件包,您应该会看到类似于以下内容的内容,但很可能具有不同的版本号。 在这种情况下,版本无关紧要。
$ rpm -q expect
expect-5.45-14.el7_1.x86_64
$ rpm -q fuse-sshfs
fuse-sshfs-2.10-1.el7.x86_64
很可能,您至少需要安装 SSHFS。 但是您可能想尝试安装提到的两个软件包。 这还将为 sshfs 安装一些额外的软件包。
$ yum install fuse-sshfs expect
什么时候 安装 SSFHS,您应该会看到类似于以下输出的内容。 只要不尝试删除任何软件包,通常可以安全地继续安装。
应用
安装 Expect 和 SSHFS 后,创建文件 /root/backup_mount_sshfs.sh,内容如下。
#!/usr/bin/expect
# If you need to debug this login change the above to end with expect -d
# Also worth mentioning if you add -d for debugging it WILL log the credentials. So please don't leave it enabled.
set env(LC_ALL) "C"
set env(PS1) "shell:"
# Set the login timeout
set timeout 30
# Set the login credentials for the remote SFTP server, hostname or IP, port, local and remote directories.
set user "$USERNAME"
set password "$PASSWORD"
set host "$HOSTNAME"
set port "22"
set local_dir "/backup/"
set remote_dir "backups/"
# The is to start a shell process and make sure it has a command prompt
spawn /bin/sh
expect "shell:"
# Next step is to call sshfs using the information we put above to mount the local "backup" directory. Currently I don't have it validate that but it's easy to add in.
# Also local_dir needs to match in /root/interworx-backup-wrapper.sh as well
# Send our login info above using expect and sshfs
send -- "sshfs $user@$host:$remote_dir $local_dir -p $port -o reconnect -o ServerAliveInterval=15 -o idmap=none -o nonempty -o StrictHostKeyChecking=Nor"
# Check for a password prompt and respond by entering the password
expect "password:"
send -- "$passwordr"
# Delay one second for the login to complete and return to the shell prompt
sleep 1
# This checks to make sure we returned to the shell and that the sshfs mount completed basically
expect "shell:"
从这里,执行以下操作:
- 将 $USERNAME、$PASSWORD 和 $HOSTNAME 值设置为 SFTP 服务器信息。
- 如果不是 22 端口,请更改端口。
- 如果适用,更改 remote_dir。 该位置需要一个相对路径。
注意:如果密码包含反斜杠或您通常需要转义的特殊字符 bash,你也需要在这里。 为了 example, 如果 e 在密码中的某处,需要将其更改为 e.
接下来,使脚本可执行。
chmod 700 /root/backup_mount_sshfs.sh
测试 SFTP 登录和挂载过程以确保其正常工作。
BKDIR=/backup
mountpoint -q $BKDIR || /root/backup_mount_sshfs.sh ; [[ $? -eq 0 ]] && echo -e "nnSFTP server login completed."|| echo -e "nnSFTP server login failed <<<<<<<<"
上面的代码指示成功和失败的登录消息。 如果输出以 SFTP服务器登录完成,则登录成功。 如果说 SFTP 服务器登录失败,请参阅故障排除部分。
验证备份目录是否已安装。
mount|grep $BKDIR
如果看起来不错,则卸载它。
umount $BKDIR
使用以下内容创建文件 /root/interworx-backup-wrapper.sh。
#!/bin/bash
# InterWorx backup wrapper script
#
# Add to root crontab at preferred time. Packaged compressed, local backups only at this time. NFS or something like CBS for remote backups only.
#
# Configurations are required to use this script!
# How many Daily copies to keep.
# Daily Backups are currently Needed for this Script
DC=4
# How many Weekly copies to keep. 0 is disabled.
WC=0
# How many Monthly copies to keep. 0 is disabled.
MC=0
# Which Day to run Weekly Backups (0..6); 0 is Sunday
DAY_WEEKLY="0"
# Which Day to run Monthly Backups (1..31) 1 or 15 is best
DAY_MONTHLY="1"
# Local Backup directory. This is where SSHFS is mounting the remote location to
BKDIR=/backup/
#Ensure /backup folder exists before running the script
mkdir -p $BKDIR
#Backup Reporting - Pull the current Server Admin Email, replace $ADEMAIL needed.
ADEMAIL="$(nodeworx -u -n --controller Users --action listMasterUser | grep email | awk '{print $2}')"
# Just setting the date format here which will look like 20210310
date=`date +'%Y%m%d'`
# Create a file stub about this too about the SSHFS mount so people don't get confused
touch $BKDIR/"This is FUSE mounted as needed-See the MOTD"
# Set a variable for the full path and name of this script when run. This is used to update the MOTD dynamically below
_self="$(realpath $0)"
# Add an MOTD pointing to this script. Check if the MOTD already has something. If not add it in
if [ -f /etc/motd ]; then echo; else touch /etc/motd; fi; if grep -q FUSE /etc/motd; then echo; else echo -e "nThis server is using a custom FUSE script for Interworx backups that mounts SSHFS from the backup script $_selfnn" >> /etc/motd ; fi
# Mount the remote SFTP directory via # Mount the remote SFTP directory via FUSE using sshfs so we can treat it like a local directory. This requires sshfs be installed via YUM
echo "Running login to SFTP server standby"
mountpoint -q $BKDIR || /root/backup_mount_sshfs.sh ; [[ $? -eq 0 ]] && echo -e "nnSFTP server login completed. Starting backup run"|| echo -e "nnSFTP server login failed <<<<<<<<"
function daily {
# Ensure Daily Folder Exists
mkdir -p $BKDIR/daily/
# Check if Daily Backups are Enabled
if [ $DC -gt "0" ]
then
# Ensure Backup Retention is kept
while [ $(ls -p $BKDIR/daily/ | grep '^Daily_[0-9]{8}/$' | wc -l) -ge $DC ]
do ionice -c3 rm -rf $BKDIR/daily/Daily$(ls -p $BKDIR/daily/ | grep 'Daily_[0-9]{8}/$' | head -n1 | sed 's/.*_/_/')
done
# Create New Backup's Folder
mkdir -p $BKDIR/daily/Daily_$date
# Create New Backup
/home/interworx/bin/backup.pex --backup-options all --domains all --output-dir $BKDIR/daily/Daily_$date/ --email $ADEMAIL --filename-format %D > $BKDIR/daily/Daily_$date/backup.log
fi
}
function weekly {
# Ensure Weekly Folder Exists
mkdir -p $BKDIR/weekly/
# Check if Weekly Backups are Enabled
if [ $WC -gt "0" ]
then
# Check if the current day is a Weekly Backup Day
if [ $(date '+%u') -eq $DAY_WEEKLY ]
then
# Cleanup all old Weekly backups over the set Weekly Count and create weekly snapshot.
while [ $(ls -p $BKDIR/weekly/ | grep '^Weekly_[0-9]{8}/$' | wc -l) -ge $WC ]
do ionice -c3 rm -rf $BKDIR/weekly/Weekly$(ls -p $BKDIR/weekly/ | grep 'Weekly_[0-9]{8}/$' | head -n1 | sed 's/.*_/_/')
done
# Copy Daily backup to the Weekly Folder
ionice -c3 rsync -aH --delete --link-dest=$BKDIR/daily/Daily_$date/ $BKDIR/daily/Daily_$date/ $BKDIR/weekly/Weekly_$date/
fi
fi
}
function monthly {
# Ensure Monthly Folder Exists
mkdir -p $BKDIR/monthly/
# Check if Monthly disabled
if [ $MC -gt "0" ]
then
# Check if the current day is a Monthly Backup Day
if [ $(date '+%d') -eq $DAY_MONTHLY ]
then
# Cleanup all old Monthly backups over the set Monthly Count and create Monthly snapshot.
while [ $(ls -p $BKDIR/monthly/ | grep '^Monthly_[0-9]{8}/$' | wc -l) -ge $MC ]
do ionice -c3 rm -rf $BKDIR/monthly/Monthly$(ls -p $BKDIR/monthly/ | grep 'Monthly_[0-9]{8}/$' | head -n1 | sed 's/.*_/_/')
done
# Copy Daily backup to the Monthly Folder
ionice -c3 rsync -aH --delete --link-dest=$BKDIR/daily/Daily_$date/ $BKDIR/daily/Daily_$date/ $BKDIR/monthly/Monthly_$date/
fi
fi
}
daily
weekly
monthly
# Backups are done by this point or should be. Unmount the remote backup directory
echo "running umount $BKDIR"
umount $BKDIR ; [[ $? -eq 0 ]] || echo "Warning umount $BKDIR failed"
echo "Backup script finished"
如下所示设置每日、每周和每月所需的保留值。
# Daily Backups are currently Needed for this Script
DC=4
# How many Weekly copies to keep. 0 is disabled.
WC=0
# How many Monthly copies to keep. 0 is disabled.
MC=0
# Which Day to run Weekly Backups (0..6); 0 is Sunday
DAY_WEEKLY="0"
# Which Day to run Monthly Backups (1..31) 1 or 15 is best
DAY_MONTHLY="1"
验证您的电子邮件地址是否配置正确以接收备份通知。
nodeworx -u -n --controller Users --action listMasterUser | grep email | awk '{print $2}'
如果没有,则需要在 NodeWorx 中进行设置,或者您可以通过修改以下行手动配置电子邮件地址。
ADEMAIL="$(nodeworx -u -n --controller Users --action listMasterUser | grep email | awk '{print $2}')"
一个 example 将是备份通知@example.com,如下图所示。
ADEMAIL="[email protected]"
通过运行 chmod 命令使备份包装脚本可执行。
chmod 700 /root/interworx-backup-wrapper.sh
测试备份脚本。 您可能希望在屏幕会话中运行它,因为它需要一些时间来运行。
/root/interworx-backup-wrapper.sh
如果一切正常且备份已完成,请设置所需的 cron 作业。
为了 example,我们希望备份在服务器时间的周日和周三凌晨 1:00 运行,并输出带有时间戳的日志。
首先,我们必须为备份日志创建该目录。
mkdir -p /var/log/backup
根 cron 条目将与此类似。
# Remote backup cron
0 1 * * 0,3 /root/interworx-backup-wrapper.sh > /var/log/backup/backup_$(date +%F).log 2>&
故障排除
您可能会在备份过程中遇到问题。 查看以下提示以帮助您 解决任何错误.
无法登录 SFTP 服务器
检查事项:
- SSH 端口是否正确?
- /etc/csf/csf.conf 中 TCP_OUT 中的 SSH 端口?
- 您可以使用 SFTP 手动登录吗?
- 密码是否有需要转义的字符?
备份未运行/工作
检查事项:
- 验证 cron 是否已作为 root 用户添加。
- /var/log/backup/ 中的备份日志显示什么? 备份出现问题的原因可能有多种。
如果您仍然不确定,请通过工单与我们联系,我们将很乐意为您提供帮助。
注意:完全支持 InterWorx 备份,但请记住,此脚本确实属于我们的超出范围支持政策,因此调查需要一些时间。
结论
定期备份可以让您高枕无忧,以防您的服务器发生问题。 获取备份的方法有多种,但每种方法都有其优缺点。 要了解有关 InterWorx 的更多信息并开始使用,请访问我们的 InterWorx 产品页面。