Initial commit. Ready for further coding...

This commit is contained in:
Fliegerjohn 2023-08-26 11:01:17 +02:00
commit 6fcdf03fec
3 changed files with 450 additions and 0 deletions

14
LICENSE Normal file
View file

@ -0,0 +1,14 @@
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.

3
README.md Normal file
View file

@ -0,0 +1,3 @@
# abackup
A backupscript vor linux(servers).

433
abackup.sh Executable file
View file

@ -0,0 +1,433 @@
#!/bin/bash
#########################################################
#Script to create backups
#V0.1.0-1 -- 2021-12-05
#
#Fliegerjohn
#########################################################
#
# Description
#
#
# Usage
#
# To create a new repo
#
# ./abackup.sh
#
# variables
#
# Path to the configfile
configdir="$HOME/.abackup"
configfile="abackup.conf"
# Hier werden die zu sichernden Ordner gespeichert
backupdirsconf="backupdirs.conf"
# Hier werden die zu ignorierende Ordner gespeichert
excludebackupdirsconf="excludebackupdirs.conf"
#
# Passwortoption
#
#For the password, several options exists:
#1 - keyfile and backup password
#2 - only keyfile
#3 - only password
pwoption=1
# Storage location keyfile
pwdir="$HOME/.keys"
# Keyfile
keyfile="abackup-secret.key"
# Path of restic installation
resticbin=`type -tp restic`
# Path to the repository
repodir=`pwd`
# Get time
datum=`date`
########################
# Funktionen #
########################
# Generate abackup-secret.key
abackupkeygen() {
if [ ! -d "${pwdir}" ]
then
mkdir -p "${pwdir}"
fi
if [ ! -e "${pwdir}/${keyfile}" ]
then
echo "Generate abackup-secret.key at $pwdir/$keyfile"
dd if=/dev/urandom bs=512 skip=1 count=16 > $pwdir/$keyfile
chmod 600 $pwdir/$keyfile
else
echo "Keyfile $pwdir/$keyfile already exists"
fi
}
# Config in datei speichern
saveconfig() {
if [[ -e "$configdir/$configfile" ]]
then
time=`date +%+4Y-%m-%d-%H:%I:%S`
echo "The configfile already exists, it will be copied to $configdir/confbackup/!"
if [[ ! -d "$configdir/confbackup" ]]
then
mkdir $configdir/confbackup
fi
mv $configdir/$configfile $configdir/confbackup/abackup$time.conf
fi
# Check the configdir
if [[ ! -d "$configdir" ]]; then mkdir -p $configdir; fi
# Write config
touch "$configdir/$configfile"
echo "########################################
#Thats the configfile for abackup.
#Read the README.md vor more Informations.
#
########################################
#
#
## Location of keyfile
#
pwdir="$pwdir"
keyfile="$keyfile"
#
## Options for password
#
#For the password, several options exists:
#1 - keyfile and backup password
#2 - only keyfile
#3 - only password
pwoption="$pwoption"
#
## Path to the installation of restic
#
resticbin="$resticbin"
#
## Path to the repository
#
repodir="$repodir"
" >> $configdir/$configfile
echo "Config saved to $configdir/$configfile"
}
#
# Create new repository
#
resticrepoadd() {
# Check if restic is installed
if [[ ! -x ${resticbin} ]]
then
echo "Could not find restic."
exit 1
fi
# Ask user the for the location of the repository
echo "#
##########################################################
#"
printf "Where should we create the new repo?
Please enter the full path (default=$repodir): "
read answer_repodir
if [[ -n "$answer_repodir" ]]
then
repodir=$answer_repodir
fi
# Check if a repository is existing. If not it creates one.
if [[ ! -d "${repodir}" ]]
then
echo "Create repodir!"
mkdir -p "${repodir}"
else
echo "Repodir already exists!"
fi
# Ask which password should be used.
echo "#
##########################################################
#"
printf "For the password, several options exists:
1 - keyfile and backup password
2 - only keyfile
3 - only password
Please choose one (default=$pwoption): "
read answer_pwoption
if [[ -n "$answer_pwoption" ]]
then
pwoption=$answer_pwoption
fi
# Start creation of repository
echo "#
##########################################################
#"
echo "Start to preparing a new repository at $datum"
case "$pwoption" in
1)
abackupkeygen
$resticbin init --password-file $pwdir/$keyfile --repo $repodir
$resticbin -r $repodir key add --password-file $pwdir/$keyfile
$resticbin -r $repodir key list --password-file $pwdir/$keyfile
;;
2)
abackupkeygen
$resticbin init --password-file $pwdir/$keyfile --repo $repodir
;;
3)
$resticbin init --repo $repodir
;;
esac
# Save config if desired
echo "#
##########################################################
#"
printf "Do you want to save your config? (y/n)(default=y): "
read answer_saveconfig
if [[ ! -n "$answer_saveconfig" ]]
then
answer_saveconfig=y
fi
if [[ "$answer_saveconfig" = "y" ]]
then
echo ""
printf "Where do you want to save the config? (default=$configdir): "
read answer_configdir
if [[ -n "$answer_configdir" ]]
then
configdir=$answer_configdir
fi
saveconfig
elif [[ "$saveconfig" = "n" ]]
then
echo "Exit without saving!"
else
echo "Wrong argument. Exit without saving!"
exit 1
fi
}
savebackupdirs() {
# Check the configdir
if [[ ! -d "$configdir" ]]; then mkdir -p $configdir; fi
# Ask for the dirs to backup
printf "Please enter the dirs you want to backup: "
read answer_backupdirs
array_backupdirs=( $answer_backupdirs )
if [[ -e $configdir/$backupdirsconf ]]; then echo "Delete old $backupdirsconf and write a new one." && rm $configdir/$backupdirsconf; fi
touch $configdir/$backupdirsconf
echo "###############################################################
# Here you can write down which dirs you want to backup! #
###############################################################" >> $configdir/$backupdirsconf
for backupdir in ${array_backupdirs[*]}
do
if [[ ! -d "$backupdir" ]]; then echo "ERROR! Cant find dir $backupdir!" && exit 1; fi
echo $backupdir >> $configdir/$backupdirsconf
done
# Ask for the dirs to exclude
printf "Do you want tho exclude dirs? Write the paths down: "
read answer_excludebackupdirs
array_excludebackupdirs=( $answer_excludebackupdirs )
if [[ -e $configdir/$excludebackupdirsconf ]]; then echo "Delete old $excludebackupdirsconf and write a new one." && rm $configdir/$excludebackupdirsconf; fi
touch $configdir/$excludebackupdirsconf
echo "##########################################################
# Here you can write down which dirs you don't want to backup! #
##########################################################" >> $configdir/$excludebackupdirsconf
for excludebackupdir in ${array_excludebackupdirs[*]}
do
if [[ ! -d "$excludebackupdir" ]]; then echo "ERROR! Cant find dir $excludebackupdir!" && exit 1; fi
echo $excludebackupdir >> $configdir/$excludebackupdirsconf
done
}
#
## Create backup
#
backup() {
echo "Let's create a backup!"
# Check if a configfile exists in the backupdir
if [[ -e "${configdir}/${backupdirsconf}" ]]
then
echo "Take a backup of the dirs listed in $configdir/$backupdirsconf!"
# Ask for the passwordoption
# With keyfile
if [[ "$pwoption" < "3" ]]; then
$resticbin -r $repodir backup --files-from $configdir/$backupdirsconf --exclude-file=$configdir/$excludebackupdirsconf --password-file="$pwdir/$keyfile"; fi
# Without keyfile
if [[ "$pwoption" = "3" ]]; then
$resticbin -r $repodir backup --files-from $configdir/$backupdirsconf --exclude-file=$configdir/$excludebackupdirsconf; fi
else
printf "There is no config for backupdirs!
Do you want to make a list of backupdirs at $configdir/$backupdirsconf?
1 - yes
2 - no, want to get it manualy
(default=1): "
read answer_backupdirs
echo ""
# Set standard if input is empty
if [[ ! -n "$answer_backupdirs" ]]
then
answer_backupdirs=1
fi
# Ask for the folders to backup
if [[ "$answer_backupdirs" = "1" ]]
then
savebackupdirs
backup
elif [[ "$answer_backupdirs" = "2" ]]
then
printf "Please enter the dirs you want to backup: "
read backupdirs
printf "Do you want tho exclude dirs? Write the paths down: "
read excludebackupdirs
# Ask for the passwordoption
# With keyfile
if [[ "$pwoption" < "3" ]]; then
$resticbin -r $repodir backup $backupdirs --exclude=$excludebackupdirs --password-file="$pwdir/$keyfile"; fi
# Without keyfile
if [[ "$pwoption" = "3" ]]; then
$resticbin -r $repodir backup $backupdirs --exclude=$excludebackupdirs; fi
else
echo "Wrong argument. Exit!"
exit 1
fi
fi
}
#
## Read config
#
readconfig() {
if [[ -e "${configdir}/${configfile}" ]]
then
echo "There is an available config at $configdir/$configfile!"
. $configdir/$configfile
else
echo "There is no existing config at $configdir/$configfile!"
fi
}
#
printhelp() {
echo "
###########################
#Use of the Backupscript: #
###########################
#
## Add a new restic-repo
#
./abackup --add-restic-repo
#
## Take a backup
#
./abackup --backup
#
## Print this help
#
./abackup --help
"
}
########################
# Main programm #
########################
# What is to be done
case "$1" in
--add-restic-repo)
readconfig
resticrepoadd
exit 0
;;
--add-new-backupdirs)
readconfig
savebackupdirs
exit 0
;;
--backup)
readconfig
backup
exit 0
;;
--restore)
echo "Later that comand will restore your data!"
exit 0
;;
--help)
printhelp
exit 0
;;
esac
printhelp