понедельник, 27 августа 2018 г.

ABS for Auto BackUp System

Intro

  1. Today's software is buggy.
  2. Because of this my daily rule is "backup all twice and regularly".
This two points make me to write my own (because of 1) backup system (because of 2). I called it Auto Backup System (ABS for short).

In this article I am going to show you how to use it.

Installation

ABS is cross-platform. Because of this you need any installer for it.

Just download ABS. Don't forget to make the downloaded file executable using chmod +x.

This is a shell script written in Tenex C Shell. If you don't trust me, you can read all the script. It's less than 130 lines of code. Also it's available at GitLab.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
#!/bin/tcsh
# Automatic Backup System. Requires: zenity, pluma, caja.

# Get settings from env. vars. or assign the defaults to them
set default_list_dir = ~/abs/
if ( ${?LIST_FILE} == 0 ) then
       setenv LIST_FILE ${default_list_dir}abs.conf

       echo '(WW) The LIST_FILE environment variable not found'
       echo "     Default used: $LIST_FILE"
       echo '     This is a path to file with a list of files to backup'
       echo '     Use `setenv LIST_FILE /path/to/file` to override the default'
endif
if ( ${?BACKUPS_DIR} == 0 ) then
       setenv BACKUPS_DIR ${default_list_dir}backups/

       echo '(WW) The BACKUPS_DIR environment variable not found'
       echo "     Default used: $BACKUPS_DIR"
       echo '     This is a path to directory for storing backup archives'
       echo '     Use `setenv BACKUPS_DIR /path/to/dir/` to override the default'
endif
if ( ${?TEXT_EDITOR} == 0 ) then
       setenv TEXT_EDITOR pluma

       echo '(WW) The TEXT_EDITOR environment variable not found'
       echo "     Default used: $TEXT_EDITOR"
       echo '     This variable sets a text editor for editing the configuration'
       echo '     Use `setenv TEXT_EDITOR editor` to override the default'
endif
if ( ${?FILE_MANAGER} == 0 ) then
       setenv FILE_MANAGER caja

       echo '(WW) The FILE_MANAGER environment variable not found'
       echo "     Default used: $FILE_MANAGER"
       echo '     This variable sets a file manager to view backups directory'
       echo '     Use `setenv FILE_MANAGER nautilus` to override the default'
endif

# Getting dependencies tools
set DIALOG = zenity

# Build the name for the backup
set timestamp = `date +%Y_%m_%d-%H%M`
set targets = "/tmp/$timestamp.abs"
set archive = "$BACKUPS_DIR/$timestamp.cpio"

set found = ( )
set missing = ( )

# Add a warning for a user about unavailable list of files for backing up
set text = ''
if ( ! -r $LIST_FILE ) then
       mkdir -p $default_list_dir
       touch "$LIST_FILE"
       chmod g+w "$LIST_FILE"

       set text = "${text}"'\<span\ foreground=\"blue\"\>Unable\ to\ find\ $LIST_FILE!\ Use:\\n'
       set text = "${text}"'setenv\ LIST_FILE\ /path/to/file\\n'
       set text = "${text}"'to\ point\ to\ the\ list\ of\ files\ for\ backing\ up.\\n\\n\</span\>'
endif

# Building lists of found and missing targets for backing up
foreach target ( `cat "$LIST_FILE"` )
       if ( -e $target ) then
              set found = ( $found $target )
       else
              set missing = ( $missing $target )
       endif
end

# Expanding the list of found backup targets
# (list all files from dirs specified in abs.conf)
rm -f $targets
foreach found_target ( $found )
       find "$found_target" -print >> $targets
end

# Even there are no files to back up, I will create the directory for them
# for a case when user interested in viewing the empty directory for backups
mkdir -p $BACKUPS_DIR

if ( -r $targets ) then
       # Creating the archive
       cat $targets | cpio -o > $archive
       rm $targets

       # Build a message for users owning graphical displays
       set text = "${text}Backed\ up:\\n"
       foreach f ( $found )
              set text = "$text\ \ \ \ \ \ \ $f\\n"
       end
       set text = "$text\\nMissing:\\n"
       foreach m ( $missing )
              set text = "$text\ \ \ \ \ \ \ $m\\n"
       end
else
       set text = "${text}"'\<span\ foreground=\"red\"\>Nothing\ to\ backup.\ '
       set text = "${text}"'Check\ the\ invocation.\ The\ sample\ is\ below.\\n'
       set text = "${text}"'setenv\ LIST_FILE\ /etc/abc.conf\ \;\ setenv\ BACKUPS_DIR\ /mnt/bkup/\ \;\ ./abs\</span\>'
endif

# Loop over all users logged in to graphical sessions
foreach name ( `users` )
       # Get login information and remove all except the display from it
       set display = `lastlogin "$name"`
       set display = `echo "$display" | sed "s/$name//g"`
       set display = `echo "$display" | sed 's/pts\/[0-9]*//g'`
       set display = `echo "$display" | sed 's/ttyv[0-9]*//g'`
       set display = `echo "$display" | sed 's/[A-Z][a-z][a-z][ ]*[A-Z][a-z][a-z][ ]*[0-9]*[ ]*[0-9]*:[0-9][0-9]:[0-9][0-9][ ]*[0-9]*//g'`

       # Skip notifying this user if the display wasn't found
	   if ( '0' == "${%display}" ) then
			  continue
	   endif

       # Inform a user that backups are done
       # and allow users to view backups and
       # add new files to the list of backing up files
       set action = `su $name -c $DIALOG\ --timeout=22\ --info\ --text="$text"\ --display="$display"\ --extra-button='View'\ --title='Automatic\ Backup\ System'\ --extra-button='Configure'`
       switch ("$action")
              case 'View': # View all backups
                     su $name -c $FILE_MANAGER\ --display="$display"\ $BACKUPS_DIR\ &
              breaksw
              case 'Configure': # Configure backing up files
                     su $name -c $TEXT_EDITOR\ --display="$display"\ $LIST_FILE\ &
              breaksw
       endsw
end

If you are ready to install ABS, download the script and save it somewhere. For example here: /root/abs/abs

Before configuring ABS, please install Zenity and put it into the directory under the PATH.

 

Configuration

ABS doesn't have a config file. Instead of this it obtains all the configuration from the environment variables.

LIST_FILE

This variable must be a path to a file lists files and directories to put into backups. If you didn't set this variable before running ABS, ABS will create a list file in your home directory. Sample of list file is below.

/usr/home/vblinkov/projects/cdialog4php/
/etc/rc.conf
/boot/loader.conf

 

BACKUPS_DIR

This variable contains a path to directory for storing your backups. If you didn't set it before the first running of ABS, ABS will create such directory inside your home directory too.

 

TEXT_EDITOR

This variable must contain the name (from the PATH) or full path to a text editor. By default it set to pluma. But because it's going to dead I would like to recommend you something like Editor from the DeforaOS project. It's tiny but works and does not crash on Ctrl+S.

 

FILE_MANAGER

Use this variable to set the file manager you want to use to view the BACKUPS_DIR. Defaulted to caja.

If some of described variable is unset ABS will inform you with (WW) warnings in stdout.

If you think that you will be happy with defaults, you may jump right to the Scheduling section. But, to customize ABS, read the next section.

 

Wrapper script

If you want to customize the ABS you, of course, may set the described environment variables right in crontab. But as for me, I don't like long lines in the system cron file. Moreover, it's useful to setup logging for ABS (ABS itself doesn't has logging for simplicity).

I suggest the next wrapper script.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#!/bin/tcsh

setenv LIST_FILE   /root/abs/abs.conf
setenv BACKUPS_DIR /mnt/barracuda/backups/
setenv TEXT_EDITOR /usr/local/bin/editor

set LOG_FILE     = ${BACKUPS_DIR}.log

echo "`date`: run" >> ${LOG_FILE}

set TYPESCRIPT_DIR = /tmp/abs/
set TYPESCRIPT_FILE = ${TYPESCRIPT_DIR}abs.log

mkdir -p ${TYPESCRIPT_DIR}
script ${TYPESCRIPT_FILE} /root/abs/abs
cat -n ${TYPESCRIPT_FILE} >> ${LOG_FILE}

echo "`date`: end" >> ${LOG_FILE}


Download and save it, for example to /root/abs/abs.run.sh. It's super easy. It sets the file with a list of files and directories for backing up on a regular manner, a directory to storing backup archives and a more stable text editor. Also, abs.run.sh introduces a lightweight logging.

Scheduling

There is nothing easier for you that configuring of running the abs.run.sh on a periodic basis using cron. For example.

2       */2  *    *     *    root /root/abs/abs.run.sh

I think this is super easy. ABS will run every even hour at the 2nd minute.

Using 

If you configured ABS properly, you will see the next dialog periodically for 22 seconds.

  

In the previous picture you may see the list of directories added to created backup. The Backed up section shows files and directories successfully added to the last backup. The Missing section lists the directories and files listed in LIST_FILE, but missing in your file system.

In case of error or missing some critical environment variables you will see red text. Warnings are blue. Note, that ABS know how to created files and directories. So, some errors, once appeared, may not visit you the second time. Such a auto-repair system. (So, who say that TCSH is too dump for doing complicated work? :-) )

Press OK to dismiss the dialog.

Press View to open the directory with all created backup archives (using FILE_MANAGER).

Press Configure to open the file with the list of directories and files you want to backup using ABS (in TEXT_EDITOR).

That's all

Please, do not hesitate to contact me in case of questions and suggestions concerning the script.
 



Комментариев нет:

Отправить комментарий