#!/bin/bash # # Author: Georg Voell - georg.voell@standby.cloud # Version: @(#)export-folder 3.2.0 13.09.2024 (c)2024 Standby.cloud # Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/ # # This script can be used free of charge. Use it as is or customize as needed. It is not guaranteed to be # error free and you will not be reimbursed for any damage it may cause. # #@ Export all files from given folder to Object Storage #@ #@Usage: export-keys [options] folder #@ Options: -h, --help : Displays helptext. #@ -v, --version: Displays the version of the script. #@ Folder: : Directory to select. #@ #@Examples: #@ export-folder ~/.ssh #@ Zips all the content under ~/.ssh and exports it to Object Storage # # Exit codes: # 01: Unknown parameter or first parameter was empty or not a directory. # 02: Cloud tools not initialized # 03: No write or read URI in JSON project file. # 04: No curl or zip in PATH. # 05: Error while zipping. # 06: Unable to upload file to ObjectStorage. # 99: User interrupt. # # See also: # **install-scripts**(1) # # ToDo: # # - More objects. # # Update history: # # V 1.0.0 22.03.2021 First version # V 1.0.1 23.03.2021 Bugfix for Write- and Read-URI is empty # V 3.0.0 15.11.2021 Export any directory # V 3.1.0 05.06.2023 New copyright # V 3.2.0 13.09.2024 New minor version # # Find executable bash library and source it lib=`which lib.bash 2>/dev/null | sed 's|^no 'lib.bash' in .*||'` if [ "$lib" != "" ]; then source "$lib" else progdir=`dirname "$0"` if [ -r "${progdir}/lib.bash" ]; then source "${progdir}/lib.bash" else echo "Unexpected error: Unable to locate bash library 'lib.bash'." exit 1 fi fi # Preset param="" # Check parameters: Loop until all parameters are used up while [ $# -gt 0 ]; do pname=${1} case "$pname" in -v | --version) shift showversion=true ;; -h | --help) shift showhelp=true ;; *) shift paramck=`echo "$pname" | grep '^-'` # Keys don't begin with '-' if [ "$paramck" != "" ]; then errstr="Unknown option '$pname'." else if [ "$param" = "" ]; then param="$pname" else errstr="Folder was already specified '$param'. Unknown additional parameter: '$pname'." fi fi esac done # Plausibility check if [ "$param" = "" ]; then errstr="No folder specified in first parameter." else if [ ! -d "$param" ]; then errstr="Folder '$param' not found." fi fi # Display help or error message DisplayHelp # Main projectjson="$HOME/.oci/project.json" # Check if we have a project definition if [ -r "$projectjson" ]; then writeuri=`browse-json project/writeuri --quiet --raw --select 1 --import "$projectjson"` readuri=`browse-json project/readuri --quiet --raw --select 1 --import "$projectjson"` parentdir=`dirname "$param"` basedir=`basename "$param"` tempfile="tmp/${basedir}.zip" if [ "$writeuri" = "" -o "$readuri" = "" ]; then exitcode=3 errormsg $exitcode "Object Storage URI not found" else # Check if he have curl and zip in path curl=`filecheck -x curl` zip=`filecheck -x zip` if [ "$curl" != "" -a "$zip" != "" ]; then cd "$parentdir" $zip -r -q -y $scratchfile "$basedir" -x \*.DS_Store stat=$? if [ $stat -eq 0 ]; then $curl -T $scratchfile "${writeuri}$tempfile" stat=$? if [ $stat -eq 0 ]; then printf "\nFiles were uploaded and stored for one day. To download them, please use this command:\n\n" printf "curl -s ${readuri}$tempfile -o ${basedir}.zip\n" else exitcode=6 errormsg $exitcode "Unable to upload file to ObjectStorage" fi else exitcode=5 errormsg $exitcode "Unable to zip '$HOME/.ssh'" fi else exitcode=4 errormsg $exitcode "Tool 'curl' or 'zip' not found in PATH '$PATH'" fi fi else exitcode=2 errormsg $exitcode "File '$projectjson' not found. Please init cloud tools first" fi # Cleanup and exit Cleanup exit $exitcode