Selaa lähdekoodia

Initial commit

Bernhard Fröhlich 8 kuukautta sitten
commit
9c00da8e3b
Allekirjoittanut: Bernhard Froehlich <decke@bluelife.at> GPG Key ID: 4DD88C3F9F3B8333
9 muutettua tiedostoa jossa 683 lisäystä ja 0 poistoa
  1. 56
    0
      LICENSE
  2. 34
    0
      Makefile
  3. 82
    0
      README.md
  4. 42
    0
      lib/base
  5. 87
    0
      lib/cmd
  6. 104
    0
      lib/config
  7. 205
    0
      lib/util
  8. 28
    0
      rc.d/ssbt
  9. 45
    0
      ssbt

+ 56
- 0
LICENSE Näytä tiedosto

@@ -0,0 +1,56 @@
1
+Copyright 2018 Bernhard Froehlich <decke@bluelife.at>
2
+All rights reserved
3
+
4
+Redistribution and use in source and binary forms, with or without
5
+modification, are permitted provided that the following conditions
6
+are met:
7
+
8
+1. Redistributions of source code must retain the above copyright
9
+   notice, this list of conditions and the following disclaimer.
10
+
11
+2. Redistributions in binary form must reproduce the above copyright
12
+   notice, this list of conditions and the following disclaimer in the
13
+   documentation and/or other materials provided with the distribution.
14
+
15
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16
+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17
+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18
+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19
+HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20
+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
21
+TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22
+PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23
+LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24
+NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25
+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
+
27
+
28
+========================================================================
29
+
30
+Parts of the code were copied from vm-bhyve under the BSD License:
31
+https://github.com/churchers/vm-bhyve
32
+
33
+
34
+Copyright (c) 2015-2016, churchers
35
+All rights reserved.
36
+
37
+Redistribution and use in source and binary forms, with or without
38
+modification, are permitted provided that the following conditions are met:
39
+
40
+* Redistributions of source code must retain the above copyright notice, this
41
+  list of conditions and the following disclaimer.
42
+
43
+* Redistributions in binary form must reproduce the above copyright notice,
44
+  this list of conditions and the following disclaimer in the documentation
45
+  and/or other materials provided with the distribution.
46
+
47
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
48
+AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
50
+DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
51
+FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52
+DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
53
+SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
54
+CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
55
+OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
56
+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

+ 34
- 0
Makefile Näytä tiedosto

@@ -0,0 +1,34 @@
1
+#
2
+# ssbt Makefile
3
+#
4
+
5
+PREFIX?=/usr/local
6
+BINDIR=$(DESTDIR)$(PREFIX)/sbin
7
+LIBDIR=$(DESTDIR)$(PREFIX)/lib/ssbt
8
+MANDIR=$(DESTDIR)$(PREFIX)/man/man8
9
+RCDIR=$(DESTDIR)$(PREFIX)/etc/rc.d
10
+
11
+CP=/bin/cp
12
+INSTALL=/usr/bin/install
13
+MKDIR=/bin/mkdir
14
+
15
+PROG=ssbt
16
+MAN=$(PROG).8
17
+
18
+install:
19
+	$(MKDIR) -p $(BINDIR)
20
+	$(INSTALL) -m 544 $(PROG) $(BINDIR)/
21
+
22
+	$(MKDIR) -p $(LIBDIR)
23
+	$(INSTALL) lib/* $(LIBDIR)/
24
+
25
+	$(MKDIR) -p $(RCDIR)
26
+	$(INSTALL) -m 555 rc.d/* $(RCDIR)/
27
+
28
+	$(MKDIR) -p $(MANDIR)
29
+	gzip -fk $(MAN)
30
+	$(INSTALL) $(MAN).gz $(MANDIR)/
31
+	rm -f -- $(MAN).gz
32
+
33
+.MAIN: clean
34
+clean: ;

+ 82
- 0
README.md Näytä tiedosto

@@ -0,0 +1,82 @@
1
+## ssbt (Simple Stupid Backup Tool)
2
+
3
+You create a backup on your machine and ssbt will periodically
4
+collect and archive it.
5
+
6
+* Simple (no database, just filesystem and a config)
7
+* Secure (using chrooted sftp)
8
+* Archive old backups
9
+* First class FreeBSD support
10
+
11
+
12
+## But why yet another backup solution?
13
+
14
+Backups should be simple and reliable to make sure they are as painless as
15
+possible. I know there are a lot of tools out there with fancy webinterfaces,
16
+agents for whatever service you might run, multiuser, cross platform and much
17
+more. You will not find any of those features here.
18
+
19
+What you will find is a simple shellscript which will periodically log into
20
+your machines with sftp(1) and pull tar archives from there which get archived.
21
+
22
+You can continue to create your backups with tar(1) and be sure those are
23
+archived on your backup master.
24
+
25
+
26
+## Quick-Start
27
+
28
+### Client
29
+
30
+A account is needed which will be used by the master to login and fetch the
31
+backups. It is recommended to create a separate account with a separate
32
+SSH certificate. This account can also be limited to only allow sftp to
33
+reduce the risk.
34
+
35
+This example assumes that you backup to `/backup/data` but this can be any
36
+directory.
37
+
38
+    # adduser -d "" -D -s nologin -w no
39
+    Username: backup
40
+    Full name: 
41
+    Uid (Leave empty for default): 
42
+    Login group [backup]: 
43
+    Login group is backup. Invite backup into other groups? []: 
44
+    Login class [default]: 
45
+    Shell (sh csh tcsh nologin) [nologin]: 
46
+    Home directory [/backup]: 
47
+    Home directory permissions (Leave empty for default): 
48
+    Use password-based authentication? [no]: 
49
+    Lock out the account after creation? [no]: 
50
+    Username   : backup
51
+    Password   : <disabled>
52
+    Full Name  : 
53
+    Uid        : 1001
54
+    Class      : 
55
+    Groups     : backup 
56
+    Home       : /backup
57
+    Home Mode  : 
58
+    Shell      : /usr/sbin/nologin
59
+    Locked     : no
60
+    OK? (yes/no): yes
61
+    adduser: INFO: Successfully added (backup) to the user database.
62
+    Add another user? (yes/no): no
63
+    Goodbye!
64
+
65
+    # mkdir -m 0755 /backup
66
+    # install -d -m 0700 -g backup -o backup /backup/.ssh
67
+    # install -d -m 0700 -g backup -o backup /backup/data
68
+
69
+    # install -m 0400 -g backup -o backup /dev/null /backup/.ssh/authorized_keys
70
+    # echo "...your-public-key-here..." > /backup/.ssh/autorized_keys
71
+
72
+    # printf "\nMatch User backup\n" >> /etc/ssh/sshd_config
73
+    # printf "\tChrootDirectory %%h\n" >> /etc/ssh/sshd_config
74
+    # printf "\tForceCommand internal-sftp\n" >> /etc/ssh/sshd_config
75
+    # printf "\tX11Forwarding no" >> /etc/ssh/sshd_config
76
+
77
+    # /etc/rc.d/sshd reload
78
+
79
+
80
+### Backup master
81
+
82
+

+ 42
- 0
lib/base Näytä tiedosto

@@ -0,0 +1,42 @@
1
+#!/bin/sh
2
+#
3
+# Copyright 2018 Bernhard Froehlich <decke@bluelife.at>
4
+# All rights reserved
5
+#
6
+# Redistribution and use in source and binary forms, with or without
7
+# modification, are permitted provided that the following conditions
8
+# are met:
9
+#
10
+# 1. Redistributions of source code must retain the above copyright
11
+#    notice, this list of conditions and the following disclaimer.
12
+#
13
+# 2. Redistributions in binary form must reproduce the above copyright
14
+#    notice, this list of conditions and the following disclaimer in the
15
+#    documentation and/or other materials provided with the distribution.
16
+#
17
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18
+# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19
+# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20
+# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21
+# HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22
+# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
23
+# TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24
+# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25
+# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26
+# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27
+# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
+
29
+VERSION=0.1
30
+
31
+. /etc/rc.subr
32
+load_rc_config "ssbt"
33
+
34
+# create directories as needed
35
+[ ! -d "${ssbt_dir}" ] && util::err "\$ssbt_dir has not been configured or is not a valid directory"
36
+[ ! -d "${ssbt_dir}/.config" ] && mkdir "${ssbt_dir}/.config"
37
+
38
+# load core configuration
39
+config::core::load
40
+
41
+# run the requested command
42
+cmd::parse "$@"

+ 87
- 0
lib/cmd Näytä tiedosto

@@ -0,0 +1,87 @@
1
+#!/bin/sh
2
+#-------------------------------------------------------------------------+
3
+# Copyright (C) 2016 Matt Churchyard (churchers@gmail.com)
4
+# All rights reserved
5
+#
6
+# Redistribution and use in source and binary forms, with or without
7
+# modification, are permitted providing that the following conditions
8
+# are met:
9
+# 1. Redistributions of source code must retain the above copyright
10
+#    notice, this list of conditions and the following disclaimer.
11
+# 2. Redistributions in binary form must reproduce the above copyright
12
+#    notice, this list of conditions and the following disclaimer in the
13
+#    documentation and/or other materials provided with the distribution.
14
+#
15
+# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16
+# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17
+# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18
+# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
19
+# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20
+# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21
+# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22
+# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
23
+# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
24
+# IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25
+# POSSIBILITY OF SUCH DAMAGE.
26
+
27
+CMD_VALID_LIST="cron,pull,status,usage,version"
28
+
29
+# cmd: ssbt ...
30
+#
31
+# process the command line to see which function is requested
32
+#
33
+# @param string _cmd the command right after 'ssbt '
34
+#
35
+cmd::parse(){
36
+    local _cmd
37
+
38
+    # try to find a matching command
39
+    cmd::find "_cmd" "$1" "${CMD_VALID_LIST}" || util::usage
40
+    shift
41
+
42
+    case "${_cmd}" in
43
+        cron)      core::cron "$@" ;;
44
+        pull)      core::pull "$@" ;;
45
+        status)    core::status "$@" ;;
46
+        usage)     util::usage ;;
47
+        version)   util::version && exit ;;
48
+        *)         util::err "unknown command '${_user_cmd}'. please run 'ssbt usage' or view the manpage for help" ;;
49
+    esac
50
+}
51
+
52
+# try to match part of a command name against a list of valid commands
53
+# if we find more than one match we return an error
54
+# if we only get one match, return the full command name
55
+#
56
+# @param string _var variable to put full command name into
57
+# @param string _user_cmd the value provided by the user
58
+# @param string _valid comma-separated list of valid choices
59
+# @return success if we find one match
60
+#
61
+cmd::find(){
62
+    local _var="$1"
63
+    local _user_cmd="$2"
64
+    local _valid="$3"
65
+    local _opt _choice _found=""
66
+    local IFS=","
67
+
68
+    [ -n "${_user_cmd}" ] || util::err "no command specified"
69
+
70
+    for _opt in ${_valid}; do
71
+        # exact match?
72
+        if [ "${_user_cmd}" = "${_opt}" ]; then
73
+            setvar "${_var}" "${_opt}"
74
+            return 0
75
+        fi
76
+
77
+        if echo "${_opt}" | grep -iqs "^${_user_cmd}"; then
78
+           [ -n "${_found}" ] && util::err "ambiguous command '${_user_cmd}'"
79
+
80
+           _found=1
81
+           _choice="${_opt}"
82
+        fi
83
+    done
84
+
85
+    [ -z "${_found}" ] && return 1
86
+    setvar "${_var}" "${_choice}"
87
+}

+ 104
- 0
lib/config Näytä tiedosto

@@ -0,0 +1,104 @@
1
+#!/bin/sh
2
+#-------------------------------------------------------------------------+
3
+# Copyright (C) 2016 Matt Churchyard (churchers@gmail.com)
4
+# All rights reserved
5
+#
6
+# Redistribution and use in source and binary forms, with or without
7
+# modification, are permitted providing that the following conditions
8
+# are met:
9
+# 1. Redistributions of source code must retain the above copyright
10
+#    notice, this list of conditions and the following disclaimer.
11
+# 2. Redistributions in binary form must reproduce the above copyright
12
+#    notice, this list of conditions and the following disclaimer in the
13
+#    documentation and/or other materials provided with the distribution.
14
+#
15
+# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16
+# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17
+# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18
+# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
19
+# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20
+# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21
+# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22
+# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
23
+# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
24
+# IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25
+# POSSIBILITY OF SUCH DAMAGE.
26
+
27
+# load a configuration file
28
+# this reads the specfied file into the global SSBT_CONFIG variable.
29
+# we have very basic parsing that uses # for comments and requires
30
+# all variables to be at the beginning of the line in lowercase.
31
+# Note also that a # within double quotes will still be treated
32
+# as the start of a comment.
33
+#
34
+# @param string _file full path of the file to read
35
+# @modifies SSBT_CONFIG
36
+#
37
+config::load(){
38
+    local _file="$1"
39
+
40
+    # read config file
41
+    # we kick out any lines that don't start with a letter,
42
+    # scrap anything after a # character, and remove double-quotes
43
+    SSBT_CONFIG=$(grep '^[a-z]' "${_file}" 2>/dev/null | awk -F# '{print $1}' | sed -e 's@ *$@@' | tr -d '"')
44
+}
45
+
46
+# get a configuration value from the current config file
47
+#
48
+# @param string _var the variable to put value into
49
+# @param string _key the name of the config key to retrieve
50
+# @param optional string _def default value to return if setting not found
51
+# @return true if setting found
52
+#
53
+config::get(){
54
+    local _c_var="$1"
55
+    local _c_key="$2"
56
+    local _c_def="$3"
57
+    local _c_line
58
+    local IFS=$'\n'
59
+
60
+    for _c_line in ${SSBT_CONFIG}; do
61
+        if [ "${_c_key}" = "${_c_line%%=*}" ]; then
62
+            setvar "${_c_var}" "${_c_line#*=}"
63
+            return 0
64
+        fi
65
+    done
66
+
67
+    # not found
68
+    setvar "${_c_var}" "${_c_def}"
69
+    return 1
70
+}
71
+
72
+# load core configuration file
73
+#
74
+# @modifies SSBT_CORE_CONFIG
75
+#
76
+config::core::load(){
77
+    SSBT_CORE_CONFIG=$(grep '^[a-z]' "${ssbt_dir}/.config/system.conf" 2>/dev/null | awk -F# '{print $1}' | sed -e 's@ *$@@' | tr -d '"')
78
+}
79
+
80
+# get a value from core config
81
+#
82
+# @param string _c_var variable name to put value into
83
+# @param string _c_key config key to look for
84
+# @param string _c_def default value if not value
85
+# @return 0 if found
86
+#
87
+config::core::get(){
88
+    local _c_var="$1"
89
+    local _c_key="$2"
90
+    local _c_def="$3"
91
+    local _c_line
92
+    local IFS=$'\n'
93
+
94
+    for _c_line in ${SSBT_CORE_CONFIG}; do
95
+        if [ "${_c_key}" = "${_c_line%%=*}" ]; then
96
+            setvar "${_c_var}" "${_c_line#*=}"
97
+            return 0
98
+        fi
99
+    done
100
+
101
+    # not found
102
+    setvar "${_c_var}" "${_c_def}"
103
+    return 1
104
+}

+ 205
- 0
lib/util Näytä tiedosto

@@ -0,0 +1,205 @@
1
+#!/bin/sh
2
+#-------------------------------------------------------------------------+
3
+# Copyright (C) 2016 Matt Churchyard (churchers@gmail.com)
4
+# All rights reserved
5
+#
6
+# Redistribution and use in source and binary forms, with or without
7
+# modification, are permitted providing that the following conditions
8
+# are met:
9
+# 1. Redistributions of source code must retain the above copyright
10
+#    notice, this list of conditions and the following disclaimer.
11
+# 2. Redistributions in binary form must reproduce the above copyright
12
+#    notice, this list of conditions and the following disclaimer in the
13
+#    documentation and/or other materials provided with the distribution.
14
+#
15
+# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16
+# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17
+# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18
+# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
19
+# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20
+# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21
+# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22
+# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
23
+# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
24
+# IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25
+# POSSIBILITY OF SUCH DAMAGE.
26
+
27
+# show version
28
+#
29
+util::version(){
30
+    echo "ssbt: simple stupid backup tool v${VERSION}"
31
+}
32
+
33
+# show version & usage information
34
+# we exit after running this
35
+#
36
+util::usage(){
37
+    util::version
38
+    cat << EOT
39
+Usage: ssbt ...
40
+    cron
41
+    pull <host>
42
+    status <host>
43
+    usage
44
+    version
45
+EOT
46
+    exit 1
47
+}
48
+
49
+# err
50
+# display an error message and exit immediately
51
+#
52
+# @param string - the message to display
53
+#
54
+util::err(){
55
+    echo "${0}: ERROR: $1" >&2
56
+    exit 1
57
+}
58
+
59
+# err_inline
60
+# display an error inline with informational output
61
+#
62
+# @param string - message to display
63
+#
64
+util::err_inline(){
65
+    echo "  ! $1"
66
+    exit 1
67
+}
68
+
69
+# warn
70
+# display warning, but do not exit
71
+#
72
+# @param string - the message to display
73
+#
74
+util::warn(){
75
+    echo "${0}: WARNING: $1" >&2
76
+}
77
+
78
+# log_rotate
79
+# simple rotation of log files
80
+# if we hit 1MB, which should cover a fair amount of history,
81
+# we move existing log and and create a new one.
82
+# one keep 1 previous file, as that should be enough
83
+#
84
+# @param string _type whether to rotate host or main log
85
+#
86
+util::log_rotate(){
87
+    local _type="$1"
88
+    local _lf="ssbt.log"
89
+    local _file _size _host
90
+
91
+    case "${_type}" in
92
+        host)
93
+            _host="$2"
94
+            _file="${ssbt_dir}/${_host}/${_lf}"
95
+            ;;
96
+        system)
97
+            _file="${ssbt_dir}/${_lf}"
98
+            ;;
99
+    esac
100
+
101
+    if [ -e "${_file}" ]; then
102
+        _size=$(stat -f %z "${_file}")
103
+
104
+        if [ -n "${_size}" -a "${_size}" -ge 1048576 ]; then
105
+            unlink "${_file}.0.gz" >/dev/null 2>&1
106
+            mv "${_file}" "${_file}.0"
107
+            gzip "${_file}.0"
108
+        fi
109
+    fi
110
+}
111
+
112
+# log to file
113
+# writes the date and a message to the specified log
114
+# the global log is in $ssbt_dir/ssbt.log
115
+# host logs are $ssbt_dir/{host}/ssbt.log
116
+#
117
+# @param string _type=host|system log to global ssbt log or host
118
+# @param optional string _host if _type=host, the hostname, otherwise do not provide at all
119
+# @param string _message the message to log
120
+#
121
+util::log(){
122
+    local _type="$1"
123
+    local _lf="ssbt.log"
124
+    local _host _message _file _date
125
+
126
+    case "${_type}" in
127
+        host)
128
+            _host="$2"
129
+            _file="${ssbt_dir}/${_host}/${_lf}"
130
+            shift 2
131
+            ;;
132
+        system)
133
+            _file="${ssbt_dir}/${_lf}"
134
+            shift 1
135
+            ;;
136
+    esac
137
+
138
+    while [ -n "$1" ]; do
139
+      echo "$(date +'%b %d %T'): $1" >> "${_file}"
140
+      shift
141
+    done
142
+}
143
+
144
+# write content to a file, and log what we
145
+# did to the host log file
146
+# it's useful to be able to see what files ssbt is creating
147
+# and the contents so we write that to the log.
148
+# The file is created in $ssbt_dir/{host}
149
+#
150
+# @param string _type=write|appnd create file or append to it
151
+# @param string _host the hostname
152
+# @param string _file the file name to write to
153
+# @param string _message the data to write
154
+#
155
+util::log_and_write(){
156
+    local _type="$1"
157
+    local _host="$2"
158
+    local _file="${ssbt_dir}/${_host}/$3"
159
+    local _message="$4"
160
+
161
+    if [ "${_type}" = "write" ]; then
162
+        util::log "host" "${_host}" "create file ${_file}"
163
+        echo "${_message}" > "${_file}"
164
+    else
165
+        echo "${_message}" >> "${_file}"
166
+    fi
167
+
168
+    util::log "host" "${_host}" " -> ${_message}"
169
+}
170
+
171
+# confirm yes or no
172
+#
173
+# @param string _msh message to display
174
+# @return int success if confirmed
175
+#
176
+util::confirm(){
177
+    local _msg="$1"
178
+    local _resp
179
+
180
+    while read -p "${_msg} (y/n)? " _resp; do
181
+        case "${_resp}" in
182
+            y*) return 0 ;;
183
+            n*) return 1 ;;
184
+        esac
185
+    done
186
+}
187
+
188
+# our own checkyesno copy
189
+# doesn't warn for unsupported values
190
+# also returns as 'yes' unless value is specifically no/off/false/0 
191
+#
192
+# @param _value the value to test
193
+# @return int 1 if set to "off/false/no/0", 0 otherwise
194
+#
195
+util::checkyesno(){
196
+    local _value="$1"
197
+
198
+    [ -z "${_value}" ] && return 1
199
+
200
+    case "$_value" in
201
+        [Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0)
202
+            return 1 ;;
203
+        *)  return 0 ;;		
204
+    esac
205
+}

+ 28
- 0
rc.d/ssbt Näytä tiedosto

@@ -0,0 +1,28 @@
1
+#!/bin/sh
2
+#
3
+# $FreeBSD$
4
+
5
+# PROVIDE: ssbt
6
+# REQUIRE: NETWORKING SERVERS dmesg
7
+# BEFORE: 
8
+# KEYWORD: shutdown nojail
9
+
10
+. /etc/rc.subr
11
+
12
+: ${ssbt_enable="NO"}
13
+
14
+name=ssbt
15
+desc="Pull backups automatically on boot"
16
+rcvar=ssbt_enable
17
+
18
+load_rc_config $name
19
+
20
+command="/usr/local/sbin/${name}"
21
+start_cmd="${name}_start"
22
+
23
+ssbt_start()
24
+{
25
+    ${command} cron >/dev/null &
26
+}
27
+
28
+run_rc_command "$1"

+ 45
- 0
ssbt Näytä tiedosto

@@ -0,0 +1,45 @@
1
+#!/bin/sh
2
+#
3
+# Copyright 2018 Bernhard Froehlich <decke@bluelife.at>
4
+# All rights reserved
5
+#
6
+# Redistribution and use in source and binary forms, with or without
7
+# modification, are permitted provided that the following conditions
8
+# are met:
9
+#
10
+# 1. Redistributions of source code must retain the above copyright
11
+#    notice, this list of conditions and the following disclaimer.
12
+#
13
+# 2. Redistributions in binary form must reproduce the above copyright
14
+#    notice, this list of conditions and the following disclaimer in the
15
+#    documentation and/or other materials provided with the distribution.
16
+#
17
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18
+# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19
+# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20
+# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21
+# HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22
+# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
23
+# TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24
+# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25
+# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26
+# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27
+# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
+
29
+# get libs
30
+if [ -e "./lib" ]; then
31
+   LIB="./lib"
32
+elif [ -e "/usr/local/lib/ssbt" ]; then
33
+   LIB="/usr/local/lib/ssbt"
34
+else
35
+   echo "unable to locate ssbt libraries"
36
+   exit 1
37
+fi
38
+
39
+# load libs
40
+
41
+. "${LIB}/cmd"
42
+. "${LIB}/config"
43
+. "${LIB}/util"
44
+
45
+. "${LIB}/base"

Loading…
Peruuta
Tallenna