如何使用 backupninja 为 Debian 创建自定义备份计划如何使用 backupninja 为 Debian 创建自定义备份计划如何使用 backupninja 为 Debian 创建自定义备份计划如何使用 backupninja 为 Debian 创建自定义备份计划
  • 文章
  • 正则表达式
    • 工具
  • 登录
找到的结果: {phrase} (显示: {results_count} 共: {results_count_total})
显示: {results_count} 共: {results_count_total}

加载更多搜索结果...

搜索范围
模糊匹配
搜索标题
搜索内容
发表 admin at 2025年2月28日
类别
  • 未分类
标签

如何使用 backupninja 为 Debian 创建自定义备份计划

Backupninja 是一款功能强大且高度可配置的备份工具,适用于基于 Debian 的发行版。在上一篇教程中,我们探讨了如何安装 backupninja 以及如何设置程序执行的两个备份操作。然而,我们应该注意到,可以说这些例子只是“冰山一角”。在这篇文章中,我们将讨论如何利用自定义处理程序和帮助程序来自定义该程序,以完成您能想到的几乎所有备份需求。

相信我 - 这并不夸张,所以让我们开始吧。

Backupninja 快速回顾

backupninja 的显着特征之一是您只需将纯文本配置或操作文件放入 /etc/backup.d 中,程序就会处理剩下的事情。此外,我们可以编写自定义脚本(又名“处理程序”)并将它们放在 /usr/share/backupninja 中来处理每种类型的备份操作。此外,我们可以通过 ninjahelper 的基于 ncurses 的交互式菜单(又名“助手”)执行这些脚本,以指导我们创建前面提到的配置文件,从而最大限度地减少人为错误的可能性。

创建自定义处理程序和帮助程序

在本例中,我们的目标是创建一个脚本来处理将选定的主目录备份到使用 gzip 或 bzip2 压缩的 tarball 中,不包括音乐和视频文件。我们只需将此脚本命名为 home,并将其放置在 /usr/backup/ninja 下。

尽管您可以使用默认的 tar 处理程序(请参阅 /usr/share/backupninja/tar 和 /usr/share/backupninja/tar.helper)实现相同的目标,但我们将使用此方法来演示如何从头开始创建有用的处理程序脚本和基于 ncurses 的帮助程序。然后,您可以根据您的具体需求决定如何应用相同的原则。

请注意,由于处理程序源自主脚本,因此无需在顶部以 #!/bin/bash 开头。

我们建议的处理程序(/usr/share/backupninja/home)如下。它被大量评论以澄清。 getconf 函数用于读取备份操作的配置文件。如果您在此处指定变量的值,它将覆盖配置文件中存在的相应值:


# home handler script for backupninja

# Every backup file will identify the host by its FQDN
getconf backupname

# Directory to store backups
getconf backupdir

# Default compression
getconf compress

# Include /home directory
getconf includes

# Exclude files with *.mp3 and *.mp4 extensions
getconf excludes

# Default extension for the packaged backup file
getconf EXTENSION

# Absolute path to date binary
getconf  TAR `which tar`

# Absolute path to date binary
getconf    DATE    `which date`

# Chosen date format
DATEFORMAT="%Y-%m-%d"

# If backupdir does not exist, exit with fatal error
if [ ! -d "$backupdir" ]
then
   mkdir -p "$backupdir" || fatal "Can not make directory $backupdir"
fi

# If backupdir is not writeable, exit with fatal error as well
if [ ! -w "$backupdir" ]
then
   fatal "Directory $backupdir is not writable"
fi

# Set the right tar option as per the chosen compression format
case $compress in
   "gzip")
     compress_option="-z"
      EXTENSION="tar.gz"
    ;;
   "bzip")
      compress_option="-j"
      EXTENSION="tar.bz2"
   ;;
   "none")
      compress_option=""
    ;;
   *)
     warning "Unknown compress filter ($tar_compress)"
     compress_option=""
    EXTENSION="tar.gz"
    ;;
esac

# Exclude the following file types / directories
exclude_options=""
for i in $excludes
do
   exclude_options="$exclude_options --exclude $i"
done

# Debugging messages, performing backup
debug "Running backup: " $TAR -c -p -v $compress_option $exclude_options 
   -f "$backupdir/$backupname-"`$DATE "+$DATEFORMAT"`".$EXTENSION" 
   $includes

# Redirect standard output to a file with .list extension
# and standard error to a file with .err extension
$TAR -c -p -v $compress_option $exclude_options 
   -f "$backupdir/$backupname-"`$DATE "+$DATEFORMAT"`".$EXTENSION" 
   $includes 
   > "$backupdir/$backupname-"`$DATE "+$DATEFORMAT"`.list 
   2> "$backupdir/$backupname-"`$DATE "+$DATEFORMAT"`.err

[ $? -ne 0 ] && fatal "Tar backup failed"

接下来,我们将创建帮助程序文件 (/usr/share/backupninja/home.helper),以便我们的处理程序在 ninjahelper 中显示为菜单:


# Backup action's description. Separate words with underscores.
HELPERS="$HELPERS home:backup_of_home_directories"

home_wizard() {
   home_title="Home action wizard"

   backupname=`hostname --fqdn`

# Specify default value for the time when this backup actions is supposed to run
   inputBox "$home_title" "When to run this action?" "everyday at 01"
   [ $? = 1 ] && return
   home_when_run="when = $REPLY"

# Specify default value for backup file name
   inputBox "$home_title" ""Name" of backups" "$backupname"
   [ $? = 1 ] && return
   home_backupname="backupname = $REPLY"
   backupname="$REPLY"

# Specify default directory to store the backups
   inputBox "$home_title" "Directory where to store the backups" "/var/backups/home"
   [ $? = 1 ] && return
   home_backupdir="backupdir = $REPLY"

# Specify default values for the radiobox
   radioBox "$home_title" "Compression" 
     "none"    "No compression" off 
       "gzip"    "Compress with gzip" on 
    "bzip"    "Compress with bzip" off
   [ $? = 1 ] && return;
   result="$REPLY"
   home_compress="compress = $REPLY "

   REPLY=
   while [ -z "$REPLY" ]; do
    formBegin "$home_title: Includes"
         formItem "Include:" /home/gacanepa
    formDisplay
     [ $? = 0 ] || return 1
      home_includes="includes = "
   for i in $REPLY; do
         [ -n "$i" ] && home_includes="$home_includes $i"
    done
   done

   REPLY=
   while [ -z "$REPLY" ]; do
      formBegin "$home_title: Excludes"
         formItem "Exclude:" *.mp3
         formItem "Exclude:" *.mp4
         # Add as many “Exclude” text boxes as needed to specify other exclude options
formItem "Exclude:"
      formItem "Exclude:"
   formDisplay
     [ $? = 0 ] || return 1
      home_excludes="excludes = "
   for i in $REPLY; do
         [ -n "$i" ] && home_excludes="$home_excludes $i"
    done
   done

# Save the config
   get_next_filename $configdirectory/10.home
   cat > $next_filename <<EOF
$home_when_run
$home_backupname
$home_backupdir
$home_compress
$home_includes
$home_excludes

# tar binary - have to be GNU tar
TAR    `which tar`
DATE    `which date`
DATEFORMAT     "%Y-%m-%d"
EXTENSION    tar

EOF
# Backupninja requires that configuration files be chmoded to 600
   chmod 600 $next_filename
}

运行忍者助手

创建名为 home 的处理程序脚本和名为 home.helper 的相应帮助程序后,让我们运行 ninjahelper 命令来创建新的备份操作:


# ninjahelper

并选择创建新的备份操作。

现在我们将看到可用的操作类型。让我们选择“备份主目录”:

接下来的屏幕将显示助手中设置的默认值(此处仅显示其中的 3 个)。请随意编辑文本框中的值。特别是,请参阅文档的调度部分,了解 when 变量的正确语法。

创建备份操作后,它将显示在 ninjahelper 的初始菜单中:

然后您可以按 ENTER 键显示可用于此操作的选项。请随意尝试它们,因为它们的描述非常简单。

特别是,“立即运行此操作”将立即在调试模式下执行备份操作,无论计划时间如何:

如果备份操作由于某种原因失败,调试将显示一条信息性消息,以帮助您找到错误并更正它。例如,考虑运行备份操作后显示的以下错误消息,其中的错误尚未得到纠正:

上图告诉您,完成备份操作所需的连接无法完成,因为远程主机似乎已关闭。此外,帮助程序文件中指定的目标目录不存在。纠正问题后,重新运行备份操作。

有几点需要记住:

  • 如果您在 /usr/share/backupninja 中创建自定义脚本(例如 foobar)来处理特定的备份操作,则还需要编写相应的帮助程序(例如 foobar.helper),以便通过 ninjahelper 在 /etc/back 中创建名为 10.foobar 的文件(11 及以后的操作也适用于进一步的操作) up.d,这是备份操作的实际配置文件。

  • 您可以按照前面的说明通过 ninjahelper 在任何给定时间执行备份,或者让它们按照 when 变量中指定的频率运行。

概括

在这篇文章中,我们讨论了如何从头开始创建自己的备份操作,以及如何在 ninjahelper 中添加相关菜单以方便创建配置文件。通过上一篇备份忍者文章和当前的一篇文章,我希望我已经给了您足够好的理由来继续并至少尝试一下。

备份愉快!

©2015-2025 艾丽卡 support@alaica.com