From 7221c27a9096aa7d6e82665e8bc6d8a61c0152b5 Mon Sep 17 00:00:00 2001 From: TolaMironcenko Date: Wed, 19 Mar 2025 15:28:52 +0700 Subject: [PATCH] updated backup and added backup_disk --- backup | 139 ++++++++++++++++++++++++++++++++++++++++------------ backup_disk | 43 ++++++++++++++++ 2 files changed, 152 insertions(+), 30 deletions(-) create mode 100755 backup_disk diff --git a/backup b/backup index 69e4e91..3d1be48 100755 --- a/backup +++ b/backup @@ -5,39 +5,118 @@ if [ "$(id -u)" != "0" ]; then exit 1 fi -create_backup() { - disk=$1 - archive=$2 - dd if=$disk status=progress | gzip -9 > $archive -} - -restore_backup() { - archive=$1 - disk=$2 - gzip -dc $archive | dd of=$disk status=progress +needarg() { + if [ ! "$1" ]; then + echo ":: ERROR: argument is needed for this option!" + exit 1 + fi } print_help() { - printf "usage:\n\t$0 (create|restore)\nexample:\n\t$0 create (disk) (archive)\n\t$0 restore (archive) (disk)\n" + printf "usage:\n\t$0 \noptions:\n\t-c,--create - create backup\n\t-r,--restore - restore backup\n\t-d,--disk,--directory - backup or restore disk or directory\n\t-f,--file,--filename - filename to backup or restore\n\t-h,--help - this help message\n" } -case "$1" in - "create") - if [[ "$2" == "" || "$3" == "" ]]; then - print_help - exit 1 - fi - create_backup $2 $3 - ;; - "restore") - if [[ "$2" == "" || "$3" == "" ]]; then - print_help - exit 1 - fi - restore_backup $2 $3 - ;; - *) - print_help - ;; -esac +create_backup() { + if [ -d $1 ]; then + directory=$1 + filename=$2 + CREATOR=$(command -v mksquashfs) || CREATOR=$(command -v tar) + case $CREATOR in + /usr/bin/mksquashfs|/bin/mksquashfs) + $CREATOR $directory $filename + ;; + /usr/bin/tar|/bin/tar) + $CREATOR -cvf $filename $directory + ;; + esac + elif [ -b $1 ]; then + disk=$1 + filename=$2 + dd if=$disk status=progress | gzip -9 > $filename + elif [ -c $1 ]; then + disk=$1 + filename=$2 + dd if=$disk status=progress | gzip -9 > $filename + else + echo "Can't find '$1'" + exit 1 + fi +} +restore_backup() { + if [ ! -f $1 ]; then + echo "Can't find '$1' or it's not file" + exit 1 + fi + if [ -d $2 ]; then + filename=$1 + directory=$2 + RESTORER=$(command -v unsquashfs) || RESTORER=$(command -v tar) + case $RESTORER in + /usr/bin/unsquashfs|/bin/unsquashfs) + $RESTORER -f -d $directory $filename + ;; + /usr/bin/tar|/bin/tar) + $RESTORER -xvf $filename -C $directory + ;; + esac + elif [ -b $2 ]; then + archive=$1 + disk=$2 + gzip -dc $archive | dd of=$disk status=progress + elif [ -c $2 ]; then + archive=$1 + disk=$2 + gzip -dc $archive | dd of=$disk status=progress + else + echo "Can't find '$2'" + exit 1 + fi +} + +parse_opt() { + while [ $1 ]; do + case $1 in + -c|--create) + do=0 + ;; + -r|--restore) + do=1 + ;; + -d|--disk|--directory) + needarg $2 + from=$2 + shift 1 + ;; + -f|--file|--filename) + needarg $2 + destination=$2 + shift 1 + ;; + -h|--help) + print_help + exit 0 + ;; + *) + echo "ERROR: invalid option '$1'" + exit 1 + ;; + esac + shift + done +} + +main() { + parse_opt "$@" + [ "$from" = "" ] || [ "$destination" = "" ] && print_help && exit 1 + case $do in + 0) + create_backup $from $destination + ;; + 1) + restore_backup $destination $from + ;; + esac +} + +main "$@" diff --git a/backup_disk b/backup_disk new file mode 100755 index 0000000..69e4e91 --- /dev/null +++ b/backup_disk @@ -0,0 +1,43 @@ +#!/bin/sh + +if [ "$(id -u)" != "0" ]; then + echo "You must be root" + exit 1 +fi + +create_backup() { + disk=$1 + archive=$2 + dd if=$disk status=progress | gzip -9 > $archive +} + +restore_backup() { + archive=$1 + disk=$2 + gzip -dc $archive | dd of=$disk status=progress +} + +print_help() { + printf "usage:\n\t$0 (create|restore)\nexample:\n\t$0 create (disk) (archive)\n\t$0 restore (archive) (disk)\n" +} + +case "$1" in + "create") + if [[ "$2" == "" || "$3" == "" ]]; then + print_help + exit 1 + fi + create_backup $2 $3 + ;; + "restore") + if [[ "$2" == "" || "$3" == "" ]]; then + print_help + exit 1 + fi + restore_backup $2 $3 + ;; + *) + print_help + ;; +esac +