Skip to content

Instantly share code, notes, and snippets.

@paseto
Created April 26, 2016 23:05
Show Gist options
  • Save paseto/c453b8e54148005518a41ab9e25ad91a to your computer and use it in GitHub Desktop.
Save paseto/c453b8e54148005518a41ab9e25ad91a to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# File: fileuploader_mega.sh
#
# Author: Giovani Paseto
#
# Created on Apr 25, 2016, 08:20:00
#
# Require: bzip
# https://megatools.megous.com/
# http://blog.hbautista.com/linux/megatools-en-debian-jessie/
#
# Config file
VERSION="0.1"
CONFIG_FILE=~/.uploadercte_config
echo "Init setup"
if [[ -e $CONFIG_FILE ]]; then
source "$CONFIG_FILE" 2>/dev/null || {
sed -i'' 's/:/=/' "$CONFIG_FILE" && source "$CONFIG_FILE" 2>/dev/null
}
if [[ $FOLDER_NAME == "" || $CNPJS == "" || $CLIENT == "" ]]; then
echo -e "Ops, there's something wrong with you config file, please run again."
unlink $CONFIG_FILE
exit 1
fi
else
while (true); do
echo -ne "\n ----- Uploader v$VERSION - [SETUP] ----- \n"
echo -ne "\n Please follow instructions above:\n"
echo -n " Please choose a folder name to store zipped db [UPLOADER]:"
read FOLDER_NAME
if [[ $FOLDER_NAME == "" ]]; then
FOLDER_NAME="UPLOADER"
fi
echo -n " CLIENT:"
read CLIENT
echo -n " CNPJ(s) (,) comma separated:"
read CNPJS
# Save config file
echo -ne "OK\n"
echo "FOLDER_NAME=$FOLDER_NAME" > "$CONFIG_FILE"
echo "CLIENT=$CLIENT" >> "$CONFIG_FILE"
echo "CNPJS=$CNPJS" >> "$CONFIG_FILE"
echo -ne "\n Done!\n"
break
done;
fi
# Params
DATE=$(date +"%d/%m/%Y")
TIME=$(date +"%H:%M")
DATA=$(date +"%Y%m%d%H%M")
#Create dirs
megamkdir /Root/"${CLIENT}"/
megamkdir /Root/"${CLIENT}"/"CTE_MDFE"/
FINALDIR=/Root/"${CLIENT}"/"CTE_MDFE"/
#Create Database backup
DATABASES=$CNPJS;
array=$(echo $DATABASES | tr "," "\n");
for x in $array
do
CDB="$(echo -e "${x//[^0-9]/}" | tr -d '[[:space:]]')"
echo -e "..."
echo -e "Generating backup files from store $CDB"
FD_ARRAY=( "/var/www/transweb_resources/stores/${CDB}/cte/producao/enviadas/" "/var/www/sgw_resources/stores/${CDB}/mdfe/producao/enviadas/" )
#Loop directories array
for DIR_1 in "${FD_ARRAY[@]}"
do
#DIR_1="/var/www/sgw_resources/stores/${CDB}/NFe/producao/enviadas/"
DIR_2="./NFE_TRASHBAG/${CDB}/"
for file_1 in $( find $DIR_1* -type f | sed 's/\.\.\///g' ) # For each file in DIR_1
do
file_2=$( echo $file_1 | sed "s $DIR_1 $DIR_2 g" ) # Getting file path in DIR_2
dir_2=$( dirname $file_2 )
if [ ! -d $dir_2 ] # Checking if sub-dir exists in DIR_2
then
echo -e "Dir: $dir_2 does not exist. Creating...\c"
mkdir -p $dir_2 # Creating if sub-dir missing
echo "Done"
fi
if [ -f $file_2 ] # Checking if file exists in DIR_2
then
cksum_file_1=$( cksum $file_1 | cut -f 1 -d " " ) # Get cksum of file in DIR_1
cksum_file_2=$( cksum $file_2 | cut -f 1 -d " " ) # Get cksum of file in DIR_2
if [ $cksum_file_1 -ne $cksum_file_2 ] # Check if cksum matches
then
echo -e "File: $file_1 is modified. Copying..\c"
cp $file_1 $file_2 # Copy if cksum mismatch
echo "Done"
fi
else
echo -e "File: $file_2 does not exist. Copying...\c"
cp $file_1 $file_2 # Copy if file does not exist.
echo "Done"
fi
done
echo -e "Read/Write permission...\c"
chmod -R 777 $DIR_2
echo "Done"
done #FD_ARRAY loop
done
echo "Backup terminated"
echo -e "--------------------------------------------------------------------------"
echo -e "2 - Starting upload..."
echo -e "--------------------------------------------------------------------------"
array=$(echo $DATABASES | tr "," "\n");
for x in $array
do
CDB="$(echo -e "${x//[^0-9]/}" | tr -d '[[:space:]]')"
echo -e "..."
echo -e "Upload backup files from store $CDB"
#Config params
DIRUP_1="./NFE_TRASHBAG/${CDB}/"
DIRUP_2="./CTE_MDFE/${CDB}/"
if [ ! -d $DIRUP_2 ] # Checking if sub-dir exists in DIRUP_2
then
echo -e "Dir: $DIRUP_2 does not exist. Creating...\c"
mkdir -p $DIRUP_2 # Creating if sub-dir missing
echo "Done"
fi
echo "START $DATA"
for file_1 in $( find $DIRUP_1* -type f | sed 's/\.\.\///g' ) # For each file in DIRUP_1
do
file_2=$( echo $file_1 | sed "s $DIRUP_1 $DIRUP_2 g" ) # Getting file path in DIRUP_2
dir_2=$( dirname $file_2 )
if [ ! -d $dir_2 ] # Checking if sub-dir exists in DIRUP_2
then
echo -e "Dir: $dir_2 does not exist. Creating...\c"
mkdir -p $dir_2 # Creating if sub-dir missing
echo $dir_2
echo "Done"
exit 1
fi
if [ -f $file_2 ] # Checking if file exists in DIRUP_2
then
cksum_file_1=$( cksum $file_1 | cut -f 1 -d " " ) # Get cksum of file in DIRUP_1
cksum_file_2=$( cksum $file_2 | cut -f 1 -d " " ) # Get cksum of file in DIRUP_2
# if [ $cksum_file_1 -ne $cksum_file_2 ] # Check if cksum matches
if cmp -s "$cksum_file_1" "$cksum_file_2"
then
echo "Files match"
else
echo -e "File: $file_1 is modified."
echo -e "Copying..\c"
cp $file_1 $file_2 # Copy if cksum mismatch
filename=$(basename "$file_2")
fyear=${filename:2:2}
fmonth=${filename:4:2}
#Create remote dirs month and year
megamkdir "${FINALDIR}20${fyear}"/
megamkdir "${FINALDIR}20${fyear}"/"${fmonth}"/
pathname=${file_2%/*}
nome="${file_2##*/}"
megaput --path "${FINALDIR}20${fyear}"/"${fmonth}"/ "$pathname/$nome"
echo "Done"
fi
else
echo -e "File: $file_2 does not exist."
echo -e "Copying...\c"
cp $file_1 $file_2 # Copy if file does not exist.
pathname=${file_2%/*}
nome="${file_2##*/}"
megaput --path $FINALDIR "$pathname/$nome"
echo "Done"
fi
done
echo -e "Read/Write permission...\c"
chmod -R 777 $DIRUP_2
echo "Done"
done
echo "Upload terminated"
echo "Done " $(date +"%d/%m/%Y") $(date +"%H:%M")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment