Changeset - 42155469350e
[Not reviewed]
0 0 1
Branko Majic (branko) - 4 years ago 2020-06-29 04:18:59
branko@majic.rs
Noticket: Added a small script for creating quick backups of files before making changes to them.
1 file changed with 106 insertions and 0 deletions:
0 comments (0 inline, 0 general) First comment
backup/bakit.sh
Show inline comments
 
new file 100755
 
#!/bin/bash
 
#
 
# bakit.sh
 
#
 
# Copyright (C) 2010, Branko Majic <branko@majic.rs>
 
#
 
# This program is free software: you can redistribute it and/or modify
 
# it under the terms of the GNU General Public License as published by
 
# the Free Software Foundation, either version 3 of the License, or
 
# (at your option) any later version.
 
#
 
# This program is distributed in the hope that it will be useful,
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
# GNU General Public License for more details.
 
#
 
# You should have received a copy of the GNU General Public License
 
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
#
 

	
 
program="bakit.sh"
 
version="0.1"
 

	
 
function usage() {
 
    cat <<EOF
 
$program $version, a non-interactive utility for creating local on-system
 
backups of files.
 

	
 
Usage: $program [OPTIONS] file
 

	
 
$program is a non-interactive utility for creating local on-system backups of
 
files. Its primary use is for the various configuration files found on most of
 
the systems. It allows you to easily create a timestamped backup of files
 
specified as input arguments.
 

	
 
$program accepts the following options:
 

	
 
    -v        show script version and licensing information
 
    -h        show usage help
 

	
 

	
 
Please report bugs and send feature requests to <branko@majic.rs>.
 
EOF
 
}
 

	
 
function version() {
 
        cat <<EOF
 
$program, version $version
 

	
 
+-----------------------------------------------------------------------+
 
| Copyright (C) 2010, Branko Majic <branko@majic.rs>                    |
 
|                                                                       |
 
| This program is free software: you can redistribute it and/or modify  |
 
| it under the terms of the GNU General Public License as published by  |
 
| the Free Software Foundation, either version 3 of the License, or     |
 
| (at your option) any later version.                                   |
 
|                                                                       |
 
| This program is distributed in the hope that it will be useful,       |
 
| but WITHOUT ANY WARRANTY; without even the implied warranty of        |
 
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         |
 
| GNU General Public License for more details.                          |
 
|                                                                       |
 
| You should have received a copy of the GNU General Public License     |
 
| along with this program.  If not, see <http://www.gnu.org/licenses/>. |
 
+-----------------------------------------------------------------------+
 

	
 
EOF
 
}
 

	
 
# If no arguments were given, just show usage help.
 
if [[ -z $1 ]]; then
 
    usage
 
    exit 0
 
fi
 

	
 
# Parse the arguments
 
while getopts "vh" opt; do
 
    case "$opt" in
 
        v) version
 
           exit 0;;
 
        h) usage
 
           exit 0;;
 
        *) usage
 
           exit 1;;
 
    esac
 
done
 
i=$OPTIND
 
shift $(($i-1))
 

	
 
# Create a backup of each provided file
 
while [[ -n $1 ]]; do
 
    source="$(echo "$1" | sed -e 's#/\+$##' )"
 
    bakFile="${source}.bak.$(date +%Y%m%d)"
 

	
 
    # Generate a new name for the backup, making sure the old ones do not get
 
    # overwritten
 
    let i=1
 
    while [[ -e "$bakFile" ]]; do
 
        bakFile="${source}.bak.$(date +%Y%m%d).$i"
 
        let i=i+1
 
    done
 

	
 
    cp -pr "$source" "$bakFile"
 
    shift
 
done
 

	
0 comments (0 inline, 0 general) First comment
You need to be logged in to comment. Login now