Skip to content

Instantly share code, notes, and snippets.

@izderadicka
Last active February 7, 2022 04:53
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save izderadicka/6991711f3524cac2e668 to your computer and use it in GitHub Desktop.
Save izderadicka/6991711f3524cac2e668 to your computer and use it in GitHub Desktop.
backup script for megatools
#!/bin/bash
BACKUP_DIR="/Root/Backup/"
PIECE_SIZE=10M
ACTION=store
trap "exit 2" SIGINT
if [[ $1 =~ -h|--help ]]; then
cat <<EOF
$0 [OPTIONS] DIRECTORY
Backups or restores directory to/from mega.co.nz. Requires metatools to be installed.
Default action is to backup given directory.
-u|--user USER mega user name (email)
-p|--password PWD mega user password
-s|--secret SECRET secret to be used for encryption/decryption of the backup
--manifest FILE backup manifest - can be created during backup creation
manifest can be then used to download and restore backup
without knowing your mega login
--piece SIZE size of backup piece (10M, 1G ... argument to split)
-r|--restore restore backup from mega to given directory
-d|--download download and restore directory from manifest
-h|--help shows this help
EOF
exit 0
fi
while [[ $# > 1 ]]
do
key="$1"
case $key in
-u|--user) # mega user
USER="$2"
shift
;;
-p|--password) # mega user password
PWD="$2"
shift
;;
-s|--secret) # encryption password
ENC_PWD="$2"
shift
;;
--piece) # size of piece
PIECE_SIZE="$2"
shift
;;
--manifest) # backup manifest - cant be used to download files by anybody
MANIFEST="$2"
shift
;;
-d|--download) # download using manifest
ACTION=download
;;
-r|--restore) # download and restore data
ACTION=restore
;;
*)
# unknown option
;;
esac
shift
done
function store {
if [[ -n "$1" && -d "$1" ]]; then
NAME=`basename $1`
MEGA_PATH=$BACKUP_DIR$NAME
megamkdir -u $USER -p $PWD $BACKUP_DIR 2>/dev/null
megarm -u $USER -p $PWD $MEGA_PATH 2>/dev/null
megamkdir -u $USER -p $PWD $MEGA_PATH
tar -C $1 -czv . | split --filter "openssl aes-256-cbc -e -k \"$ENC_PWD\" -md sha256 | tee >(sha1sum -b > $TMP_DIR/\$FILE.check) | cat > $TMP_DIR/\$FILE; megaput -u $USER -p $PWD --disable-previews --path $MEGA_PATH $TMP_DIR/\$FILE.check; megaput -u $USER -p $PWD --disable-previews --path $MEGA_PATH $TMP_DIR/\$FILE; rm $TMP_DIR/\$FILE; rm $TMP_DIR/\$FILE.check" -b $PIECE_SIZE - $NAME.
if [[ -n $MANIFEST ]]; then
echo $ENC_PWD > $MANIFEST
megals -e -n $MEGA_PATH >> $MANIFEST
fi
else
echo "Please specify directory!" >&2
exit 1
fi
}
function restore {
if [[ -n "$1" ]]; then
NAME=`basename $1`
MEGA_PATH=$BACKUP_DIR$NAME
mkdir -p $1
FILES=`megals -u $USER -p $PWD -n $MEGA_PATH | grep -v "\.check$"`
if [[ -z "$FILES" ]]; then
echo "Sorry no backup find in $MEGA_PATH" >&2
exit 2
fi
(for f in $FILES; do
megaget --no-progress -u $USER -p $PWD --path $TMP_DIR/$f.check $MEGA_PATH/$f.check
if [[ ! -f $TMP_DIR/$f.check ]]; then
echo "Checksum file is missing for $f" >&2
exit 4
fi
megaget -u $USER -p $PWD --path - $MEGA_PATH/$f | tee >(sha1sum --status -c $TMP_DIR/$f.check ; if [[ $? != 0 ]]; then echo "ERROR $?">${TMP_DIR}ERROR; fi ) | openssl aes-256-cbc -d -k "$ENC_PWD" -md sha256
rm $TMP_DIR/$f.check
if [[ -f ${TMP_DIR}ERROR ]]; then
echo "Checksum error for file $f" >&2
exit 6
fi
done) | tar -C $1 -xzv
else
echo "Please specify directory!" >&2
exit 1
fi
}
function download {
if [[ -z "$MANIFEST" ]]; then
echo "Must provide backup manifest file" >&2
exit 3
fi
if [[ -n "$1" ]]; then
mkdir -p $1
{
read ENC_PWD
while { read PIECE_LINK PIECE_NAME; read CHECK_LINK CHECK_NAME; }; do
if [[ $CHECK_NAME != *.check || $PIECE_NAME != ${CHECK_NAME%.check} || -z $CHECK_NAME ]]; then
echo "Invalid manifest file" >&2
exit 8
fi
megadl --no-progress --path $TMP_DIR/$CHECK_NAME $CHECK_LINK
if [[ ! -f $TMP_DIR/$CHECK_NAME ]]; then
echo "Checksum file is missing $CHECK_NAME" >&2| tar | tar -C $1 -xzv| tar -C $1 -xzv| tar -C $1 -xzv-C $1 -xzv
exit 4
fi
megadl --path - $PIECE_LINK | tee >(sha1sum --status -c $TMP_DIR/$CHECK_NAME ; if [[ $? != 0 ]]; then echo "ERROR $?">${TMP_DIR}ERROR; fi ) | openssl aes-256-cbc -d -k "$ENC_PWD" -md sha256
rm $TMP_DIR/$CHECK_NAME
if [[ -f ${TMP_DIR}ERROR ]]; then
echo "Checksum error for file $PIECE_NAME" >&2
exit 6
fi
done | tar -C $1 -xzv
} <$MANIFEST
else
echo "Please specify directory!" >&2
exit 1
fi
}
if [[ ( -z "$USER" || -z "$PWD" || -z "$ENC_PWD" ) && $ACTION != "download" ]]; then
echo "You must provide --user --pasword and --secret" >&2
exit 3
fi
function cleanup {
rm -rf $TMP_DIR
}
trap cleanup EXIT
TMP_DIR=`mktemp -d`
case $ACTION in
restore)
restore $1
;;
store)
store $1
;;
download)
download $1
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment