#!/bin/bash ##! Project: Run Sync for IMAP Sync ##! Version: 1.2.20 ##! Author: Ze'ev Schurmann ##! Website: https://git.zaks.web.za/thisiszeev/imapsync-runsync ##! Creat a textfile called {filename}.list ##! ##! This script depends on the Perl script IMAPSync being in the same folder ##! and set as executable. It allows you to run IMAPSync jobs in batches. ##! ##! HOW TO USE: ##! ##! First create a text file with the extension .list ##! (see sample.list for an example) ##! ##! The first part of the first line is the originating server ##! (not the client domain, but the originating server host.domain.tld) ##! ##! The second line of the first line is the destination server ##! (not the client domain, but the originating server host.domain.tld) ##! ##! If the script is running on either the originating or destination ##! server then substitute the relevant servername with localhost ##! ##! Subsequent lines are as follows: ##! First part is the originating email address ##! Second part is the originating password ##! Third part is the destination email address ##! Fourth part is the destination password ##! -- Third and fourth parth can be omitted if they are both identical ##! to the first and second part. ##! ##! Execute script as: ##! ./rynsync {filename}.list {move/sync/nossl1/nossl2/loop=600/dryrun} ##! ##! filename.list is the file that contains the credentials for the sync job ##! move is for the script to move the mails from originating server to ##! destination server ##! copy is for the script to copy the mails from originating server to ##! destination server {default} ##! nossl1 is to instruct the script not to use SSL or TLS encryption when ##! connecting to the originating server. Omitting this will result ##! in the script auto detecting for SSL and/or TLS. Use this if the ##! originating server doesn't have a valid certificate. ##! nossl2 is to instruct the script not to use SSL or TLS encryption when ##! connecting to the destination server. Omitting this will result ##! in the script auto detecting for SSL and/or TLS. Use this if the ##! destination server doesn't have a valid certificate. ##! loop=3600 Define the script to run in loop mode. In this case the pause ##! between iterations will be 3600 seconds (1 hour). Omitting this ##! will result in the script iterating through the list file once. ##! dryrun This will cause the script to iterate through the list file once ##! but only log onto each server and then log off, no mails will be ##! synced. This is useful to test that all credentials are working. ##! help Will display this text! ##! version Display the version number only ##! ##! ##! # Put your Telegram Bot API Key here (excluding the bot prefix) telegram_api="" # Put your Telegram Chat ID here telegram_chat="" # Do not edit anything below this point move=false loop=false dry_run="" if [[ $1 == "version" ]] || [[ $2 == "version" ]] || [[ $3 == "version" ]] || [[ $4 == "version" ]] || [[ $5 == "version" ]] || [[ $6 == "version" ]]; then cat $(realpath "$0") | grep "^##! Version:" | sed 's/##! Version: //g' exit 0 fi if [[ -z $1 ]] || [[ $1 == "help" ]] || [[ $2 == "help" ]] || [[ $3 == "help" ]] || [[ $4 == "help" ]] || [[ $5 == "help" ]] || [[ $6 == "help" ]]; then cat $(realpath "$0") | grep "^##!" | sed 's/##!//g' exit 0 fi if [[ $1 == *.list ]]; then if [[ ! -f "$1" ]]; then echo "$1 does not exist!" exit 1 fi if [[ ! -z $2 ]]; then if [[ $2 == "move" ]]; then move=true elif [[ $2 == "sync" ]]; then move=false elif [[ $2 == "nossl1" ]]; then no_ssl1="--nossl1 --notls1" elif [[ $2 == "nossl2" ]]; then no_ssl2="--nossl2 --notls2" elif [[ ${2:0:5} == "loop=" ]]; then declare $2 elif [[ $2 == "dryrun" ]]; then dry_run="--justlogin" fi fi if [[ ! -z $3 ]]; then if [[ $3 == "move" ]]; then move=true elif [[ $3 == "sync" ]]; then move=false elif [[ $3 == "nossl1" ]]; then no_ssl1="--nossl1 --notls1" elif [[ $3 == "nossl2" ]]; then no_ssl2="--nossl2 --notls2" elif [[ ${3:0:5} == "loop=" ]]; then declare $3 elif [[ $3 == "dryrun" ]]; then dry_run="--justlogin" fi fi if [[ ! -z $4 ]]; then if [[ $4 == "move" ]]; then move=true elif [[ $4 == "sync" ]]; then move=false elif [[ $4 == "nossl1" ]]; then no_ssl1="--nossl1 --notls1" elif [[ $4 == "nossl2" ]]; then no_ssl2="--nossl2 --notls2" elif [[ ${4:0:5} == "loop=" ]]; then declare $4 elif [[ $4 == "dryrun" ]]; then dry_run="--justlogin" fi fi if [[ ! -z $5 ]]; then if [[ $5 == "move" ]]; then move=true elif [[ $5 == "sync" ]]; then move=false elif [[ $5 == "nossl1" ]]; then no_ssl1="--nossl1 --notls1" elif [[ $5 == "nossl2" ]]; then no_ssl2="--nossl2 --notls2" elif [[ ${5:0:5} == "loop=" ]]; then declare $5 elif [[ $5 == "dryrun" ]]; then dry_run="--justlogin" fi fi if [[ ! -z $6 ]]; then if [[ $6 == "move" ]]; then move=true elif [[ $6 == "sync" ]]; then move=false elif [[ $6 == "nossl1" ]]; then no_ssl1="--nossl1 --notls1" elif [[ $6 == "nossl2" ]]; then no_ssl2="--nossl2 --notls2" elif [[ ${6:0:5} == "loop=" ]]; then declare $6 elif [[ $6 == "dryrun" ]]; then dry_run="--justlogin" fi fi while true; do size=$( wc -l < $1 ) ((size++)) temp=$( cat $1 | head -1 ) error_count=() error_max=$((size-2)) server=( $temp ) echo "From Server: ${server[0]}" echo "Dest Server: ${server[1]}" for ((pos=2; pos<$size; pos++)); do email=($( cat $1 | head -n $pos | tail -1 )) if [[ ${#email[@]} == 2 ]]; then email[2]=${email[0]} email[3]=${email[1]} fi if [[ ${#email[@]} == 4 ]]; then echo "Number: $((pos-1)) of $((size-2))" echo "From Email: ${email[0]}" echo "Password: ${email[1]}" echo "To Email: ${email[2]}" echo "Password: ${email[3]}" echo "Number: $((pos-1)) of $((size-2))" > status.tmp echo "Email: ${email[2]}" >> status.tmp echo "Password: ${email[3]}" >> status.tmp echo "" if [[ $move == true ]]; then echo "Moving mails..." ./imapsync --host1 "${server[0]}" --host2 "${server[1]}" --password1 "${email[1]}" --password2 "${email[3]}" --user1 "${email[0]}" --user2 "${email[2]}" --noemailreport1 --noemailreport2 --delete --noexpunge ${no_ssl1} ${no_ssl2} ${dry_run} error_code=$? elif [[ $move == false ]]; then echo "Copying mails..." ./imapsync --host1 "${server[0]}" --host2 "${server[1]}" --password1 "${email[1]}" --password2 "${email[3]}" --user1 "${email[0]}" --user2 "${email[2]}" --noemailreport1 --noemailreport2 ${no_ssl1} ${no_ssl2} ${dry_run} error_code=$? else echo "ERROR" exit 1 fi echo "" if [[ ${error_code} == 0 ]]; then telegram_text="${email[0]}%20>%20${email[2]}%20Synced!" echo echo "${email[0]} > ${email[2]} Synced!" echo sleep 5 else telegram_text="${email[0]}%20>%20${email[2]}%20Failed!" echo echo "${email[0]} > ${email[2]} Failed!" echo error_count+=("${email[0]}>${email[2]}") sleep 5 fi if [[ ${telegram_api} != "" ]] && [[ ${telegram_chat} != "" ]]; then wget -O mailsync.json "https://api.telegram.org/bot${telgram_api}/sendMessage?chat_id=${telegram_chat}&text=${telegram_text}" rm mailsync.json fi else echo "ERROR reading line $pos from $1" exit 1 fi done if [[ ${#error_count[@]} -ge ${error_max} ]]; then echo "" echo "" echo "" echo "" echo "" echo "Failed on all email addresses!" echo "Check log of details..." echo "" echo "" echo "" exit 1 fi if [[ $loop == false ]]; then rm -f status.tmp exit 0 else echo "Waiting for next run..." echo "Last run had ${#error_count} errors!" for ((ec=0; ec<${#error_count[@]}; ec++)); do echo "${error_count[$ec]} had an error - view log file for details!" done sleep $loop fi done else cat $(realpath "$0") | grep "^##!" | sed 's/##!//g' fi