Use backupninja to customize backup plans for Debian
Backupninja is a powerful and highly configurable backup software in the Debian system (as well as the Debian-based release. In the previous article, we discussed how to install backupninja and how to configure and execute two backup operations. However, those are just the tip of the iceberg. This time, we will discuss how to customize Handler and Helper to use these features to customize policies for any backup needs.
Review backupninja
One unique feature of backupninja is that it can completely discard the plain text configuration files and operation files in/etc/backup. d, and the software will do it by itself. In addition, we can write a custom script (also called "handler") and put it in the/usr/share/backupninja directory to complete different types of backup operations. In addition, you can use ninjahelper's ncurses-based interactive menu (also known as "helper") to guide us to create some configuration files to minimize manual errors.
Create Custom Handler and Helper
This section aims to create a script to back up the home directory in the form of a gzip or bzip2 package, excluding music and video files. We named this file home and put it in the/usr/backup/ninja directory.
Although you can use the default tar handler (refer to/usr/share/backupninja/tar and/usr/share/backupninja/tar. helper) to achieve this effect, but we use this method to demonstrate how to create a practical handler script and ncurses-based helper. You can decide how to use this method based on your needs.
Since handlers comes from the main script, you do not need to use #! Shebang line starting with/bin/bash ).
The handler (/usr/share/backupninja/home) We wrote is as follows. It has been commented out in detail. The getconf function is used to read the configuration file of the backup operation. If you specify a variable value, it overwrites the value of the corresponding variable in the configuration file:
#/Home directory handler script
# Each backup file uses FQDN to identify the host
getconf backupname
# Backup file storage directory
getconf backupdir
# Default Compression
getconf compress
# Include/home Directory
getconf includes
# Does not contain *. mp3 and *. mp4 files
getconf excludes
# Default extension of the backup file to be packaged
getconf EXTENSION
# Absolute path of the tar Program
getconf TAR `which tar`
# Absolute path of the date program
getconf DATE `which date`
# Date Format
DATEFORMAT="%Y-%m-%d"
# Exit with a fatal error if the Backup Directory does not exist
if[!-d "$backupdir"]
then
mkdir -p "$backupdir"|| fatal "Can not make directory $backupdir"
fi
# Exit with a fatal error if the backup directory cannot be written
if[!-w "$backupdir"]
then
fatal "Directory $backupdir is not writable"
fi
# Select the corresponding tar option based on the 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
# Does not contain some file types/Directories
exclude_options=""
for i in $excludes
do
exclude_options="$exclude_options --exclude $i"
done
# Debug information and perform backup operations
debug "Running backup: " $TAR -c -p -v $compress_option $exclude_options \
-f "$backupdir/$backupname-"`$DATE "+$DATEFORMAT"`".$EXTENSION" \
$includes
# Redirect the standard output to a file extended by. list
# Redirect the standard error output 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"
Next we will create a helper file (/usr/share/backupninja/home. helper ). In this way, hendlers will display in ninjahelper as a menu:
# Backup operation description, separated by the following line
HELPERS="$HELPERS home:backup_of_home_directories"
home_wizard(){
home_title="Home action wizard"
backupname=`hostname --fqdn`
# Specify the backup operation time
inputBox "$home_title""When to run this action?""everyday at 01"
[ $?=1]&&return
home_when_run="when = $REPLY"
# Specify the backup file name
inputBox "$home_title""\"Name\" of backups""$backupname"
[ $?=1]&&return
home_backupname="backupname = $REPLY"
backupname="$REPLY"
# Specify the default path for saving the backup file
inputBox "$home_title""Directory where to store the backups""/var/backups/home"
[ $?=1]&&return
home_backupdir="backupdir = $REPLY"
# Specify the default value of the check box
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]||return1
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 multiple "Exclude" text boxes as needed to specify other excluded content
formItem "Exclude:"
formItem "Exclude:"
formDisplay
[ $?=0]||return1
home_excludes="excludes = "
for i in $REPLY;do
[-n "$i"]&& home_excludes="$home_excludes $i"
done
done
# Save Configuration
get_next_filename $configdirectory/10.home
cat > $next_filename <<EOF
$home_when_run
$home_backupname
$home_backupdir
$home_compress
$home_includes
$home_excludes
# The path of the tar program, which must be GNU tar
TAR `which tar`
DATE `which date`
DATEFORMAT "%Y-%m-%d"
EXTENSION tar
EOF
# Change the configuration file permission to 600
chmod 600 $next_filename
}
Run ninjahelper
After the handler script named home and corresponding home. helper are created, run the ninjahelper command to create a new backup operation.
#ninjahelper
Select create a new backup action to create a new backup operation ).
Next we will see the optional operation types. Here we select "backup of home directories" (backup home directory ):
The default values set in helper are displayed (only three are displayed here ). You can edit the value in the text box. NOTE: For the syntax of the "when" variable, refer to the Schedule section of the document.
After the backup is created, it is displayed in the ninjahelper initialization menu:
Press enter to display the backup operation options. Because it is very simple, we can perform some experiments on it.
Note that the "run this action now" option immediately backs up data regardless of the Schedule:
Some errors may occur in the backup operation. debug provides some useful information to help you locate and correct the errors. For example, if a backup operation has an error and is not corrected, the following error message is printed when it is running.
The figure above tells us that the connection used to complete the backup operation is not established, because the remote host that it needs to connect seems to be down. In addition, the target directory specified in the helper file does not exist. After these problems are rectified, start the backup operation again.
Things to remember:
- When you create a custom script (such as foobar) under/usr/share/backupninja to handle special backup operations, then you need to write the corresponding helper (foobar. file, ninjahelper will generate 10. foobar (the next operation is 11, and so on) files are stored in/etc/backup. d directory, and this file is the real configuration file for backup operations.
- You can use ninjahelper to set the backup operation execution time or follow the frequency set in the "when" variable.
Summary
In this article, we explored how to create our own backup operations from scratch and how to add relevant menus to ninjahelper to generate corresponding configuration files. Through the previous article and this article, I hope I have provided enough reasons for you to continue your research, or at least try it.
This article permanently updates the link address: