updated backup and added backup_disk

This commit is contained in:
wt committed 2025-03-19 15:28:52 +07:00
1 parent cd4a366d24
commit 7221c27a90
2 files changed
+152 -30

No files matched your search

+109 -30
View File
@@ -5,39 +5,118 @@ if [ "$(id -u)" != "0" ]; then
exit 1 exit 1
fi fi
create_backup() { needarg() {
disk=$1 if [ ! "$1" ]; then
archive=$2 echo ":: ERROR: argument is needed for this option!"
dd if=$disk status=progress | gzip -9 > $archive exit 1
} fi
restore_backup() {
archive=$1
disk=$2
gzip -dc $archive | dd of=$disk status=progress
} }
print_help() { 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 <options>\noptions:\n\t-c,--create - create backup\n\t-r,--restore - restore backup\n\t-d,--disk,--directory <disk or directory> - backup or restore disk or directory\n\t-f,--file,--filename <filename> - filename to backup or restore\n\t-h,--help - this help message\n"
} }
case "$1" in create_backup() {
"create") if [ -d $1 ]; then
if [[ "$2" == "" || "$3" == "" ]]; then directory=$1
print_help filename=$2
exit 1 CREATOR=$(command -v mksquashfs) || CREATOR=$(command -v tar)
fi case $CREATOR in
create_backup $2 $3 /usr/bin/mksquashfs|/bin/mksquashfs)
;; $CREATOR $directory $filename
"restore") ;;
if [[ "$2" == "" || "$3" == "" ]]; then /usr/bin/tar|/bin/tar)
print_help $CREATOR -cvf $filename $directory
exit 1 ;;
fi esac
restore_backup $2 $3 elif [ -b $1 ]; then
;; disk=$1
*) filename=$2
print_help dd if=$disk status=progress | gzip -9 > $filename
;; elif [ -c $1 ]; then
esac 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 "$@"
Executable
+43
View File
@@ -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