updated backup and added backup_disk
This commit is contained in:
1 parent
cd4a366d24
commit
7221c27a90
2 files changed
+152
-30
No files matched your search
@@ -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 <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")
|
||||
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 "$@"
|
||||
Reference in new issue
Block a user