Use backupninja to customize backup plans for Debian

Source: Internet
Author: User
Tags dateformat

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:

  1. #/Home directory handler script
  2. # Each backup file uses FQDN to identify the host
  3. getconf backupname
  4. # Backup file storage directory
  5. getconf backupdir
  6. # Default Compression
  7. getconf compress
  8. # Include/home Directory
  9. getconf includes
  10. # Does not contain *. mp3 and *. mp4 files
  11. getconf excludes
  12. # Default extension of the backup file to be packaged
  13. getconf EXTENSION
  14. # Absolute path of the tar Program
  15. getconf TAR `which tar`
  16. # Absolute path of the date program
  17. getconf DATE `which date`
  18. # Date Format
  19. DATEFORMAT="%Y-%m-%d"
  20. # Exit with a fatal error if the Backup Directory does not exist
  21. if[!-d "$backupdir"]
  22. then
  23. mkdir -p "$backupdir"|| fatal "Can not make directory $backupdir"
  24. fi
  25. # Exit with a fatal error if the backup directory cannot be written
  26. if[!-w "$backupdir"]
  27. then
  28. fatal "Directory $backupdir is not writable"
  29. fi
  30. # Select the corresponding tar option based on the compression format
  31. case $compress in
  32. "gzip")
  33. compress_option="-z"
  34. EXTENSION="tar.gz"
  35. ;;
  36. "bzip")
  37. compress_option="-j"
  38. EXTENSION="tar.bz2"
  39. ;;
  40. "none")
  41. compress_option=""
  42. ;;
  43. *)
  44. warning "Unknown compress filter ($tar_compress)"
  45. compress_option=""
  46. EXTENSION="tar.gz"
  47. ;;
  48. esac
  49. # Does not contain some file types/Directories
  50. exclude_options=""
  51. for i in $excludes
  52. do
  53. exclude_options="$exclude_options --exclude $i"
  54. done
  55. # Debug information and perform backup operations
  56. debug "Running backup: " $TAR -c -p -v $compress_option $exclude_options \
  57. -f "$backupdir/$backupname-"`$DATE "+$DATEFORMAT"`".$EXTENSION" \
  58. $includes
  59. # Redirect the standard output to a file extended by. list
  60. # Redirect the standard error output to a file with. err Extension
  61. $TAR -c -p -v $compress_option $exclude_options \
  62. -f "$backupdir/$backupname-"`$DATE "+$DATEFORMAT"`".$EXTENSION" \
  63. $includes \
  64. >"$backupdir/$backupname-"`$DATE "+$DATEFORMAT"`.list \
  65. 2>"$backupdir/$backupname-"`$DATE "+$DATEFORMAT"`.err
  66. [ $?-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:

  1. # Backup operation description, separated by the following line
  2. HELPERS="$HELPERS home:backup_of_home_directories"
  3. home_wizard(){
  4. home_title="Home action wizard"
  5. backupname=`hostname --fqdn`
  6. # Specify the backup operation time
  7. inputBox "$home_title""When to run this action?""everyday at 01"
  8. [ $?=1]&&return
  9. home_when_run="when = $REPLY"
  10. # Specify the backup file name
  11. inputBox "$home_title""\"Name\" of backups""$backupname"
  12. [ $?=1]&&return
  13. home_backupname="backupname = $REPLY"
  14. backupname="$REPLY"
  15. # Specify the default path for saving the backup file
  16. inputBox "$home_title""Directory where to store the backups""/var/backups/home"
  17. [ $?=1]&&return
  18. home_backupdir="backupdir = $REPLY"
  19. # Specify the default value of the check box
  20. radioBox "$home_title""Compression" \
  21. "none""No compression" off \
  22. "gzip""Compress with gzip" on \
  23. "bzip""Compress with bzip" off
  24. [ $?=1]&&return;
  25. result="$REPLY"
  26. home_compress="compress = $REPLY "
  27. REPLY=
  28. while[-z "$REPLY"];do
  29. formBegin "$home_title: Includes"
  30. formItem "Include:"/home/gacanepa
  31. formDisplay
  32. [ $?=0]||return1
  33. home_includes="includes = "
  34. for i in $REPLY;do
  35. [-n "$i"]&& home_includes="$home_includes $i"
  36. done
  37. done
  38. REPLY=
  39. while[-z "$REPLY"];do
  40. formBegin "$home_title: Excludes"
  41. formItem "Exclude:"*.mp3
  42. formItem "Exclude:"*.mp4
  43. # Add multiple "Exclude" text boxes as needed to specify other excluded content
  44. formItem "Exclude:"
  45. formItem "Exclude:"
  46. formDisplay
  47. [ $?=0]||return1
  48. home_excludes="excludes = "
  49. for i in $REPLY;do
  50. [-n "$i"]&& home_excludes="$home_excludes $i"
  51. done
  52. done
  53. # Save Configuration
  54. get_next_filename $configdirectory/10.home
  55. cat > $next_filename <<EOF
  56. $home_when_run
  57. $home_backupname
  58. $home_backupdir
  59. $home_compress
  60. $home_includes
  61. $home_excludes
  62. # The path of the tar program, which must be GNU tar
  63. TAR `which tar`
  64. DATE `which date`
  65. DATEFORMAT "%Y-%m-%d"
  66. EXTENSION tar
  67. EOF
  68. # Change the configuration file permission to 600
  69. chmod 600 $next_filename
  70. }

 

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:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.