Initial commit

This commit is contained in:
Christoph 2020-09-26 12:59:23 +02:00
commit 7c96847ce6
7 changed files with 2503 additions and 0 deletions

171
OLD/sync_from_old_d.mx.sh Executable file
View File

@ -0,0 +1,171 @@
#!/usr/bin/env bash
_source_host=d-alt.mx.oopen.de
_databases="sympa"
_sync_dirs="/data/sympa /usr/local/sympa/arc /usr/local/sympa/bounce /usr/local/sympa/etc /usr/local/sympa/spool /usr/local/sympa/static_content"
#_sync_dirs="/data/sympa /usr/local/sympa"
sympa_user=sympa
sympa_group=sympa
remote_mysqldump_binary=/usr/local/mysql/bin/mysqldump
# -------------
# --- Some functions
# -------------
echononl(){
echo X\\c > /tmp/shprompt$$
if [ `wc -c /tmp/shprompt$$ | awk '{print $1}'` -eq 1 ]; then
echo -e -n "$*\\c" 1>&2
else
echo -e -n "$*" 1>&2
fi
rm /tmp/shprompt$$
}
error(){
echo ""
echo -e "\t[ \033[31m\033[1mFehler\033[m ]: $*"
echo ""
}
warn (){
echo ""
echo -e "\t[ \033[33m\033[1mWarning\033[m ]: $*"
echo ""
}
info (){
echo ""
echo -e "\t[ \033[32m\033[1mInfo\033[m ]: $*"
echo ""
}
echo_done() {
echo -e "\033[80G[ \033[32mdone\033[m ]"
}
echo_ok() {
echo -e "\033[80G[ \033[32mok\033[m ]"
}
echo_warning() {
echo -e "\033[80G[ \033[33m\033[1mwarn\033[m ]"
}
echo_failed(){
echo -e "\033[80G[ \033[1;31mfailed\033[m ]"
}
echo_skipped() {
echo -e "\033[80G[ \033[33m\033[1mskipped\033[m ]"
}
echo ""
echononl " Shutting down sympa.."
systemctl stop sympa
if [[ "$?" = "0" ]]; then
echo_ok
else
echo_failed
fi
echo ""
_dir=/data/sympa
for _dir in $_sync_dirs ; do
echononl " Syncing ${_dir}.."
rsync -a -e ssh --delete ${_source_host}:$_dir `dirname ${_dir}` > /dev/null 2>&1
if [[ "$?" = "0" ]]; then
echo_ok
else
echo_failed
fi
done
echo ""
for _db in $_databases ; do
echononl " Update Database $_db from ${_source_host}.."
ssh ${_source_host} "$remote_mysqldump_binary --login-path=local --opt $_db" | mysql --login-path=local $_db
if [[ "$?" = "0" ]]; then
echo_ok
else
echo_failed
fi
done
echo ""
echononl " Syncing /etc/postfix/sympa_aliases"
rsync -a -e ssh ${_source_host}:/etc/postfix/sympa_aliases /etc/postfix/ > /dev/null 2>&1
if [[ "$?" = "0" ]]; then
echo_ok
else
echo_failed
fi
echononl " Changing Ownership (sympa:sympa) of /etc/postfix/sympa_aliases"
chown ${sympa_user}:$sympa_group /etc/postfix/sympa_aliases
if [[ "$?" = "0" ]]; then
echo_ok
else
echo_failed
fi
echononl " Syncing /etc/postfix/sympa_domains"
rsync -a -e ssh ${_source_host}:/etc/postfix/sympa_domains /etc/postfix/ > /dev/null 2>&1
if [[ "$?" = "0" ]]; then
echo_ok
else
echo_failed
fi
echononl " Create Postfix lookup table /etc/postfix/sympa_domains.db"
postmap btree:/etc/postfix/sympa_domains
if [[ "$?" = "0" ]]; then
echo_ok
else
echo_failed
fi
echononl " Syncing /etc/postfix/sympa_transport"
rsync -a -e ssh ${_source_host}:/etc/postfix/sympa_transport /etc/postfix/ > /dev/null 2>&1
if [[ "$?" = "0" ]]; then
echo_ok
else
echo_failed
fi
echononl " Syncing /etc/postfix/sympa_virtual_aliases"
rsync -a -e ssh ${_source_host}:/etc/postfix/sympa_virtual_aliases /etc/postfix/ > /dev/null 2>&1
if [[ "$?" = "0" ]]; then
echo_ok
else
echo_failed
fi
echo ""
echononl " Reloading Postfix Service.."
systemctl reload postfix
if [[ "$?" = "0" ]]; then
echo_ok
else
echo_failed
fi
echononl " Starting sympa listmanager.."
systemctl start sympa
if [[ "$?" = "0" ]]; then
echo_ok
else
echo_failed
fi
echo ""
exit 0

536
OLD/sync_from_old_server.sh.00 Executable file
View File

@ -0,0 +1,536 @@
#!/usr/bin/env bash
ipv4_old="83.223.85.101"
ipv4_new="83.223.86.116"
ipv6_old="2a01:30:1fff:3::101"
ipv6_new="2a01:30:0:13:2c5:48ff:feee:f21c"
old_server_ip=$ipv4_old
sync_home_dirs=""
tmp_err_msg=$(mktemp)
log_dir=/root/sync_from_old_server_logs
sync_web_dirs="
/var/www/lists.aktionsbuendnis-brandenburg.de
/var/www/lists.initiativenserver.de
"
sync_web_dirs=""
sync_vhost_dirs="
/usr/local/apache2/conf/vhosts
"
sync_vhost_dirs=""
sync_dehydrated_dirs="
/var/lib/dehydrated/certs
"
sync_other_dirs="
/etc/opendkim
/usr/local/mailman/lists
/usr/local/mailman/data
/usr/local/mailman/archives
"
sync_files=""
# - Set to "ALL" if all databases from old server should be synced
# - Leave empty if no satabases should be synced
# -
sync_pgsql_databases=""
# - Set to "ALL" if all databases from old server should be synced
# - Leave empty if no satabases should be synced
# -
sync_mysql_databases="
postfix
roundcubemail
"
sync_mysql_databases=""
# -------------
# --- Some functions
# -------------
echononl(){
echo X\\c > /tmp/shprompt$$
if [ `wc -c /tmp/shprompt$$ | awk '{print $1}'` -eq 1 ]; then
echo -e -n "$*\\c" 1>&2
else
echo -e -n "$*" 1>&2
fi
rm /tmp/shprompt$$
}
fatal(){
echo ""
echo -e "fatal error: $*"
echo ""
echo -e "\t\033[31m\033[1mInstalllation will be interrupted\033[m\033[m"
echo ""
exit 1
}
error(){
echo ""
echo -e "\t[ \033[31m\033[1mFehler\033[m ]: $*"
echo ""
}
warn (){
echo ""
echo -e "\t[ \033[33m\033[1mWarning\033[m ]: $*"
echo ""
}
info (){
echo ""
echo -e "\t[ \033[32m\033[1mInfo\033[m ]: $*"
echo ""
}
echo_done() {
echo -e "\033[80G[ \033[32mdone\033[m ]"
}
echo_ok() {
echo -e "\033[80G[ \033[32mok\033[m ]"
}
echo_warning() {
echo -e "\033[80G[ \033[33m\033[1mwarn\033[m ]"
}
echo_failed(){
echo -e "\033[80G[ \033[1;31mfailed\033[m ]"
}
echo_skipped() {
echo -e "\033[80G[ \033[33m\033[1mskipped\033[m ]"
}
# ----------
# - Is this a systemd system?
# -
if [[ "X$(which systemd)" = "X" ]]; then
systemd_exists=false
else
systemd_exists=true
fi
echo ""
echononl " Test ssh connection to $old_server_ip"
ssh $old_server_ip "ls" > /dev/null 2> $tmp_err_msg
if [[ $? -eq 0 ]];then
echo_ok
else
echo_failed
fatal "$(cat $tmp_err_msg)"
fi
# - Create log directory
# -
echononl " Create log directory \"${log_dir}\""
if [[ -d "$log_dir" ]] ; then
echo_skipped
else
mkdir ${log_dir} > /dev/null 2> $tmp_err_msg
if [[ $? -eq 0 ]];then
echo_ok
else
echo_failed
error "$(cat $tmp_err_msg)"
fi
fi
# - Syncing PostgreSQL Databases.."
# -
echo -e "\n\n \033[37m\033[1mSyncing PostgreSQL Databases..\033[m\n"
_got_pgsql_databases=false
sync_pgsql_databases="${sync_pgsql_databases##*( )}"
sync_pgsql_databases="${sync_pgsql_databases%%*( )}"
if [[ -z "$sync_pgsql_databases" ]]; then
warn "No PostgreSQL databases for syncing configured."
elif [[ "$sync_pgsql_databases" = "ALL" ]]; then
echononl " Get (all) PostgreSQL databases from $old_server_ip.."
_psql_databases=$(ssh $old_server_ip "cd /tmp ; $sudo -u postgres $psql -lt" | grep -v -e"^$" | awk '{print$1}')
if [[ $? -eq 0 ]];then
echo_ok
_got_pgsql_databases=true
else
echo_failed
fi
else
_pgsql_databases_remote="$sync_pgsql_databases"
_got_pgsql_databases=true
fi
if $_got_pgsql_databases ; then
log_file=${log_dir}/sync_pgsql.log
> $log_file
psql=$(ssh $old_server_ip which psql)
pg_dump=$(ssh $old_server_ip which pg_dump)
pg_dumpall=$(ssh $old_server_ip which pg_dumpall)
sudo=$(ssh $old_server_ip which sudo)
su=$(ssh $old_server_ip which su)
for _db in $_pgsql_databases_remote ; do
echononl " Sync Database $_db"
if [[ "${_db}" = "template0" ]] || [[ "${_db}" = "template1" ]] ; then
echo_skipped
continue
fi
if [[ "${_db}" = ":" ]] || [[ "${_db}" = "|" ]] ; then
echo_skipped
continue
fi
ssh $old_server_ip "cd /tmp ; $sudo -u postgres $pg_dump -c $_db" \
| su postgres -lc "psql $_db" >> $log_file 2> $tmp_err_msg
if [[ $? -eq 0 ]];then
echo_ok
else
echo_failed
error "$(cat $tmp_err_msg)"
fi
done
fi
# - Syncing MySQL Databases.."
# -
echo -e "\n\n \033[37m\033[1mSyncing MySQL Databases..\033[m\n"
_got_mysql_databases=false
sync_mysql_databases="${sync_mysql_databases##*( )}"
sync_mysql_databases="${sync_mysql_databases%%*( )}"
if [[ -z "$sync_mysql_databases" ]]; then
warn "No MySQL databases for syncing configured."
elif [[ "$sync_mysql_databases" = "ALL" ]]; then
echononl " Get MySQL databases from $old_server_ip.."
_mysql_databases_remote=$(ssh $old_server_ip "/usr/local/mysql/bin/mysql --login-path=local -N -s -e \"show databases\"")
if [[ $? -eq 0 ]];then
echo_ok
_got_mysql_databases=true
else
echo_failed
fi
else
_mysql_databases_remote="$sync_mysql_databases"
_got_mysql_databases=true
fi
if $_got_mysql_databases ; then
log_file=${log_dir}/sync_mysql.log
> $log_file
mysqldump_flags="--protocol=SOCKET --max_allowed_packet=128M --skip-opt --add-drop-table --add-locks --create-options --quick --set-charset --disable-keys --lock-tables --routines"
for _db in $_mysql_databases_remote ; do
echononl " Sync Database $_db"
if [[ "${_db}" = "information_schema" ]]; then
echo_skipped
continue
fi
if [[ "${_db}" = "performance_schema" ]]; then
echo_skipped
continue
fi
if [[ "${_db}" = "mysql" ]]; then
echo_skipped
continue
fi
if [[ "${_db}" = "test" ]] || [[ "${_db}" = "mysqltest" ]] ; then
echo_skipped
continue
fi
ssh $old_server_ip "/usr/local/mysql/bin/mysqldump --login-path=local $_db" | mysql --login-path=local $_db >> $log_file 2> $tmp_err_msg
if [[ $? -eq 0 ]];then
echo_ok
else
echo_failed
error "$(cat $tmp_err_msg)"
fi
done
fi
echo -e "\n\n \033[37m\033[1mSyncing Home Directories..\033[m\n"
_got_home_dirs=false
sync_home_dirs="${sync_home_dirs##*( )}"
sync_home_dirs="${sync_home_dirs%%*( )}"
if [[ -z "${sync_home_dirs,,}" ]] ; then
warn "No Home Directories for syncing configured"
else
_got_home_dirs=true
fi
if $_got_home_dirs ; then
log_file=${log_dir}/sync_home_dirs.log
> $log_file
for sync_dir in $sync_home_dirs ; do
echononl " Syncinc directory \"${sync_dir}\".."
rsync -av -e ssh --delete $old_server_ip:$sync_dir $(dirname $sync_dir)/ >> $log_file 2> $tmp_err_msg
if [[ $? -eq 0 ]];then
echo_ok
else
echo_failed
error "$(cat $tmp_err_msg)"
fi
done
fi
echo -e "\n\n \033[37m\033[1mSyncing Files..\033[m\n"
_got_files=false
sync_files="${sync_files##*( )}"
sync_files="${sync_files%%*( )}"
if [[ -z "${sync_files,,}" ]] ; then
warn "No Files for syncing configured"
else
_got_files=true
fi
if $_got_files ; then
log_file=${log_dir}/sync_files.log
> $log_file
for sync_file in $sync_files ; do
echononl " Syncinc file \"${sync_file}\".."
rsync -av -e ssh --delete $old_server_ip:$sync_file $(dirname $sync_file)/ >> $log_file 2> $tmp_err_msg
if [[ $? -eq 0 ]];then
echo_ok
else
echo_failed
error "$(cat $tmp_err_msg)"
fi
done
fi
echo -e "\n\n \033[37m\033[1mSyncing Web Directories..\033[m\n"
_got_web_dirs=false
sync_web_dirs="${sync_web_dirs##*( )}"
sync_web_dirs="${sync_web_dirs%%*( )}"
if [[ -z "${sync_web_dirs,,}" ]] ; then
warn "No Web Directories for syncing configured"
else
_got_web_dirs=true
fi
if $_got_web_dirs ; then
log_file=${log_dir}/sync_web_dirs.log
> $log_file
for sync_dir in $sync_web_dirs ; do
echononl " Syncinc directory \"${sync_dir}\".."
rsync -av -e ssh --delete $old_server_ip:$sync_dir $(dirname $sync_dir)/ >> $log_file 2> $tmp_err_msg
if [[ $? -eq 0 ]];then
echo_ok
else
echo_failed
error "$(cat $tmp_err_msg)"
fi
done
fi
echo -e "\n\n \033[37m\033[1mSyncing Vhost Configurations..\033[m\n"
_got_vhost_dirs=false
sync_vhost_dirs="${sync_vhost_dirs##*( )}"
sync_vhost_dirs="${sync_vhost_dirs%%*( )}"
if [[ -z "${sync_vhost_dirs,,}" ]] ; then
warn "No Vhost Configurations for syncing configured"
else
_got_vhost_dirs=true
fi
if $_got_vhost_dirs ; then
log_file=${log_dir}/sync_vhost_configs.log
> $log_file
for sync_dir in $sync_vhost_dirs ; do
echononl " Syncinc directory \"${sync_dir}\".."
rsync -av -e ssh --delete \
--exclude 000-default.conf \
--exclude 000-dehydrated.conf \
--exclude 000-logformat.conf \
$old_server_ip:$sync_dir $(dirname $sync_dir)/ >> $log_file 2> $tmp_err_msg
if [[ $? -eq 0 ]];then
echo_ok
else
echo_failed
error "$(cat $tmp_err_msg)"
fi
done
fi
echo -e "\n\n \033[37m\033[1mConvert IP's in Vhost Configurations..\033[m\n"
if $_got_vhost_dirs ; then
log_file=${log_dir}/convert_vhost_configs.log
> $log_file
for sync_dir in $sync_vhost_dirs ; do
installation_failed=false
echononl " Convert Vhost Configuration in \"$sync_dir\""
while IFS='' read -r -d '' _file ; do
[[ -d "$_file" ]] && continue
[[ -h "$_file" ]] && continue
[[ "$(basename $_file)" = "000-default.conf" ]] && continue
[[ "$(basename $_file)" = "000-dehydrated.conf" ]] && continue
[[ "$(basename $_file)" = "000-logformat.conf" ]] && continue
if [[ -f "$_file" ]]; then
perl -i -n -p -e s"#$ipv4_old#$ipv4_new#g" \
$_file >> $log_file 2> $tmp_err_msg
if [[ $? -ne 0 ]]; then
installation_failed=true
error "$(cat $tmp_err_msg)"
fi
perl -i -n -p -e s"#$ipv6_old#$ipv6_new#g" \
$_file >> $log_file 2> $tmp_err_msg
if [[ $? -ne 0 ]]; then
installation_failed=true
error "$(cat $tmp_err_msg)"
fi
fi
done < <(find $sync_dir -mindepth 1 -maxdepth 1 -type f -print0)
if ! $installation_failed ; then
echo_ok
fi
done
else
echo_skipped
fi
echo -e "\n\n \033[37m\033[1mSyncing Dehydrated Certs Dirs ..\033[m\n"
_got_dehydrated_dirs_dirs=false
sync_dehydrated_dirs="${sync_dehydrated_dirs##*( )}"
sync_dehydrated_dirs="${sync_dehydrated_dirs%%*( )}"
if [[ -z "${sync_dehydrated_dirs,,}" ]] ; then
warn "No other Directories for syncing configured"
else
_got_dehydrated_dirs_dirs=true
fi
if $_got_dehydrated_dirs_dirs ; then
log_file=${log_dir}/sync_dehydrated_dirs.log
> $log_file
for sync_dir in $sync_dehydrated_dirs ; do
echononl " Syncinc directory \"${sync_dir}\".."
rsync -av -e ssh $old_server_ip:$sync_dir $(dirname $sync_dir)/ >> $log_file 2> $tmp_err_msg
if [[ $? -eq 0 ]];then
echo_ok
else
echo_failed
error "$(cat $tmp_err_msg)"
fi
done
fi
echo -e "\n\n \033[37m\033[1mSyncing other Directories..\033[m\n"
_got_other_dirs=false
sync_other_dirs="${sync_other_dirs##*( )}"
sync_other_dirs="${sync_other_dirs%%*( )}"
if [[ -z "${sync_other_dirs,,}" ]] ; then
warn "No other Directories for syncing configured"
else
_got_other_dirs=true
fi
if $_got_other_dirs ; then
log_file=${log_dir}/sync_other_dirs.log
> $log_file
for sync_dir in $sync_other_dirs ; do
echononl " Syncinc directory \"${sync_dir}\".."
rsync -av -e ssh --delete $old_server_ip:$sync_dir $(dirname $sync_dir)/ >> $log_file 2> $tmp_err_msg
if [[ $? -eq 0 ]];then
echo_ok
else
echo_failed
error "$(cat $tmp_err_msg)"
fi
done
fi
echo -e "\n\n \033[37m\033[1mRestart Services..\033[m\n"
echononl " (Re)start PureFTPd daemon (pure-ftpd)"
if $systemd_exists ; then
if [[ ! -f /etc/init.d/pure-ftpd ]] && [[ ! -f /etc/systemd/system/pure-ftpd.service ]] ; then
echo_skipped
else
systemctl restart pure-ftpd > /dev/null 2> $tmp_err_msg
if [[ $? -eq 0 ]] ; then
echo_ok
else
echo_failed
error "$(cat $tmp_err_msg)"
fi
fi
else
if [[ ! -f /etc/init.d/pure-ftpd ]]; then
echo_skipped
else
/etc/init.d/pure-ftpd restart > /dev/null 2> $tmp_err_msg
if [[ $? -eq 0 ]] ; then
echo_ok
else
echo_failed
error "$(cat $tmp_err_msg)"
fi
fi
fi
echononl " (Re)start Apache webservice"
if $systemd_exists ; then
systemctl restart apache2 > /dev/null 2> $tmp_err_msg
if [[ $? -eq 0 ]] ; then
echo_ok
else
echo_failed
error "$(cat $tmp_err_msg)"
fi
else
/etc/init.d/apache2 restart > /dev/null 2> $tmp_err_msg
if [[ $? -eq 0 ]] ; then
echo_ok
else
echo_failed
error "$(cat $tmp_err_msg)"
fi
fi
rm $tmp_err_msg
echo ""
exit 0
if [[ "$?" -ne 0 ]] ; then
installation_failed=true
error "$(cat $tmp_err_msg)"
fi
if ! $installation_failed ; then
echo_ok
fi

656
OLD/sync_from_old_server.sh.01 Executable file
View File

@ -0,0 +1,656 @@
#!/usr/bin/env bash
ipv4_old=""
old_server_ip=$ipv4_old
tmp_err_msg=$(mktemp)
log_dir=/root/sync_from_old_server_logs
# - Sync Home Directoties
# -
sync_home_dirs=""
# - Sync web directories (included document root directory
# -
sync_web_dirs=""
# - Sync Apache virtual host configurations
# -
# - Note:
# -
sync_vhost_dirs=""
# - Only needed to replace ip-addresse in virtual host configuration files
# -
ipv4_new=""
ipv6_old=""
ipv6_new=""
# - Sync dehydrated cert directory
# -
sync_dehydrated_dirs=""
#sync_dehydrated_dirs="
# /var/lib/dehydrated/certs
#"
# - Sync other directories
# -
sync_other_dirs=""
# - Sync files
# -
sync_files=""
# - Sync Postgres databases
# -
# - Note:
# - Set to "ALL" if all databases from old server should be synced
# - Leave empty if no databases should be synced.
# -
sync_pgsql_databases=""
# - Sync MySQL databases
# -
# - Note:
# - Set to "ALL" if all databases from old server should be synced
# - Leave empty if no databases should be synced.
# -
# - Example:
# - To sync database 'shop19' aot source host to database 'shop19_dev'
# - at destination host:
# - sync_mysql_databases="
# - ...
# - shop19:shop19_dev
# - ...
# - "
# -
sync_mysql_databases=""
# - mysql_credential_args
# -
# - Example
# - mysql_credential_args="--defaults-file=/usr/local/mysql/sys-maint.cnf"
# - mysql_credential_args="--login-path=local"
# - mysql_credential_args="-u <db_name> -p'<db_passwd>'"
# -
mysql_credential_args="--login-path=local"
# -------------
# --- Some functions
# -------------
echononl(){
echo X\\c > /tmp/shprompt$$
if [ `wc -c /tmp/shprompt$$ | awk '{print $1}'` -eq 1 ]; then
echo -e -n "$*\\c" 1>&2
else
echo -e -n "$*" 1>&2
fi
rm /tmp/shprompt$$
}
fatal(){
echo ""
echo -e "fatal error: $*"
echo ""
echo -e "\t\033[31m\033[1mInstalllation will be interrupted\033[m\033[m"
echo ""
exit 1
}
error(){
echo ""
echo -e "\t[ \033[31m\033[1mFehler\033[m ]: $*"
echo ""
}
warn (){
echo ""
echo -e "\t[ \033[33m\033[1mWarning\033[m ]: $*"
echo ""
}
info (){
echo ""
echo -e "\t[ \033[32m\033[1mInfo\033[m ]: $*"
echo ""
}
echo_done() {
echo -e "\033[80G[ \033[32mdone\033[m ]"
}
echo_ok() {
echo -e "\033[80G[ \033[32mok\033[m ]"
}
echo_warning() {
echo -e "\033[80G[ \033[33m\033[1mwarn\033[m ]"
}
echo_failed(){
echo -e "\033[80G[ \033[1;31mfailed\033[m ]"
}
echo_skipped() {
echo -e "\033[80G[ \033[33m\033[1mskipped\033[m ]"
}
# ----------
# - At least ip address from old server must be present
# -
if [[ -z "$old_server_ip" ]]; then
fatal "No remote server ip (variable old_server_ip) is given."
fi
# - Is this a systemd system?
# -
if [[ "X$(which systemd)" = "X" ]]; then
systemd_exists=false
else
systemd_exists=true
fi
echo ""
echononl " Test ssh connection to $old_server_ip"
ssh $old_server_ip "ls" > /dev/null 2> $tmp_err_msg
if [[ $? -eq 0 ]];then
echo_ok
else
echo_failed
fatal "$(cat $tmp_err_msg)"
fi
# - Create log directory
# -
echononl " Create log directory \"${log_dir}\""
if [[ -d "$log_dir" ]] ; then
echo_skipped
else
mkdir ${log_dir} > /dev/null 2> $tmp_err_msg
if [[ $? -eq 0 ]];then
echo_ok
else
echo_failed
error "$(cat $tmp_err_msg)"
fi
fi
# - Syncing PostgreSQL Databases.."
# -
echo -e "\n\n \033[37m\033[1mSyncing PostgreSQL Databases..\033[m\n"
_got_pgsql_databases=false
sync_pgsql_databases="${sync_pgsql_databases##*( )}"
sync_pgsql_databases="${sync_pgsql_databases%%*( )}"
if [[ -z "$sync_pgsql_databases" ]]; then
warn "No PostgreSQL databases for syncing configured."
elif [[ "$sync_pgsql_databases" = "ALL" ]]; then
echononl " Get (all) PostgreSQL databases from $old_server_ip.."
_psql_databases=$(ssh $old_server_ip "cd /tmp ; $sudo -u postgres $psql -lt" | grep -v -e"^$" | awk '{print$1}')
if [[ $? -eq 0 ]];then
echo_ok
_got_pgsql_databases=true
else
echo_failed
fi
else
_pgsql_databases_remote="$sync_pgsql_databases"
_got_pgsql_databases=true
fi
if $_got_pgsql_databases ; then
log_file=${log_dir}/sync_pgsql.log
> $log_file
psql=$(ssh $old_server_ip which psql)
pg_dump=$(ssh $old_server_ip which pg_dump)
pg_dumpall=$(ssh $old_server_ip which pg_dumpall)
sudo=$(ssh $old_server_ip which sudo)
su=$(ssh $old_server_ip which su)
for _db in $_pgsql_databases_remote ; do
echononl " Sync Database '$_db'.."
if [[ "${_db}" = "template0" ]] || [[ "${_db}" = "template1" ]] ; then
echo_skipped
continue
fi
if [[ "${_db}" = ":" ]] || [[ "${_db}" = "|" ]] ; then
echo_skipped
continue
fi
ssh $old_server_ip "cd /tmp ; $sudo -u postgres $pg_dump -c $_db" \
| su postgres -lc "psql $_db" >> $log_file 2> $tmp_err_msg
if [[ $? -eq 0 ]];then
echo_ok
else
echo_failed
error "$(cat $tmp_err_msg)"
fi
done
fi
# - Syncing MySQL Databases.."
# -
echo -e "\n\n \033[37m\033[1mSyncing MySQL Databases..\033[m\n"
_got_mysql_databases=false
sync_mysql_databases="${sync_mysql_databases##*( )}"
sync_mysql_databases="${sync_mysql_databases%%*( )}"
if [[ -z "$sync_mysql_databases" ]]; then
warn "No MySQL databases for syncing configured."
elif [[ "$sync_mysql_databases" = "ALL" ]]; then
echononl " Get MySQL databases from $old_server_ip.."
_mysql_databases_remote=$(ssh $old_server_ip "/usr/local/mysql/bin/mysql $mysql_credential_args -N -s -e \"show databases\"")
if [[ $? -eq 0 ]];then
echo_ok
_got_mysql_databases=true
else
echo_failed
fi
else
_mysql_databases_remote="$sync_mysql_databases"
_got_mysql_databases=true
fi
if $_got_mysql_databases ; then
log_file=${log_dir}/sync_mysql.log
> $log_file
mysqldump_flags="--protocol=SOCKET --max_allowed_packet=128M --skip-opt --add-drop-table --add-locks --create-options --quick --set-charset --disable-keys --lock-tables --routines"
# - GET current (global) Autocommit value
# -
echononl " GET current (global) Autocommit value"
CUR_AUTOCOMMIT="$(/usr/local/mysql/bin/mysql $mysql_credential_args -N -s -e "SHOW GLOBAL VARIABLES LIKE 'autocommit'" | awk '{print$2}')" >> $log_file 2> $tmp_err_msg
if [[ $? -eq 0 ]];then
echo_ok
else
echo_failed
error "$(cat $tmp_err_msg)"
fi
# - Set Autocommit OFF
# -
echononl " Set Autocommit to OFF"
mysql $mysql_credential_args -N -s -e "SET GLOBAL AUTOCOMMIT='OFF'" >> $log_file 2> $tmp_err_msg
if [[ $? -eq 0 ]];then
echo_ok
else
echo_failed
error "$(cat $tmp_err_msg)"
fi
echo ""
for _val in $_mysql_databases_remote ; do
IFS=':' read -a _val_arr <<< "${_val}"
_src_db="${_val_arr[0]}"
if [[ -n "${_val_arr[1]}" ]] ; then
_dst_db="${_val_arr[1]}"
echononl " Sync Database '$_src_db' --> '$_dst_db'.. "
else
echononl " Sync Database '$_src_db'.. "
_dst_db="$_src_db"
fi
if [[ "${_src_db}" = "information_schema" ]]; then
echo_skipped
continue
fi
if [[ "${_src_db}" = "performance_schema" ]]; then
echo_skipped
continue
fi
if [[ "${_src_db}" = "mysql" ]]; then
echo_skipped
continue
fi
if [[ "${_src_db}" = "test" ]] || [[ "${_src_db}" = "mysqltest" ]] ; then
echo_skipped
continue
fi
ssh $old_server_ip "/usr/local/mysql/bin/mysqldump $mysql_credential_args $_src_db" | mysql $mysql_credential_args $_dst_db >> $log_file 2> $tmp_err_msg
if [[ $? -eq 0 ]];then
echo_ok
else
echo_failed
error "$(cat $tmp_err_msg)"
fi
done
# - Reset (global) Autocommit value
# -
echo ""
echononl " Reset (global) Autocommit value to '$CUR_AUTOCOMMIT'"
mysql $mysql_credential_args -N -s -e "SET GLOBAL AUTOCOMMIT='$CUR_AUTOCOMMIT'" >> $log_file 2> $tmp_err_msg
if [[ $? -eq 0 ]];then
echo_ok
else
echo_failed
error "$(cat $tmp_err_msg)"
fi
fi
echo -e "\n\n \033[37m\033[1mSyncing Home Directories..\033[m\n"
_got_home_dirs=false
sync_home_dirs="${sync_home_dirs##*( )}"
sync_home_dirs="${sync_home_dirs%%*( )}"
if [[ -z "${sync_home_dirs,,}" ]] ; then
warn "No Home Directories for syncing configured"
else
_got_home_dirs=true
fi
if $_got_home_dirs ; then
log_file=${log_dir}/sync_home_dirs.log
> $log_file
for sync_dir in $sync_home_dirs ; do
echononl " Syncinc directory \"${sync_dir}\".."
rsync -av -e ssh --delete $old_server_ip:$sync_dir $(dirname $sync_dir)/ >> $log_file 2> $tmp_err_msg
if [[ $? -eq 0 ]];then
echo_ok
else
echo_failed
error "$(cat $tmp_err_msg)"
fi
done
fi
echo -e "\n\n \033[37m\033[1mSyncing Files..\033[m\n"
_got_files=false
sync_files="${sync_files##*( )}"
sync_files="${sync_files%%*( )}"
if [[ -z "${sync_files,,}" ]] ; then
warn "No Files for syncing configured"
else
_got_files=true
fi
if $_got_files ; then
log_file=${log_dir}/sync_files.log
> $log_file
for sync_file in $sync_files ; do
echononl " Syncinc file \"${sync_file}\".."
rsync -av -e ssh --delete $old_server_ip:$sync_file $(dirname $sync_file)/ >> $log_file 2> $tmp_err_msg
if [[ $? -eq 0 ]];then
echo_ok
else
echo_failed
error "$(cat $tmp_err_msg)"
fi
done
fi
echo -e "\n\n \033[37m\033[1mSyncing Web Directories..\033[m\n"
_got_web_dirs=false
sync_web_dirs="${sync_web_dirs##*( )}"
sync_web_dirs="${sync_web_dirs%%*( )}"
if [[ -z "${sync_web_dirs,,}" ]] ; then
warn "No Web Directories for syncing configured"
else
_got_web_dirs=true
fi
if $_got_web_dirs ; then
log_file=${log_dir}/sync_web_dirs.log
> $log_file
for sync_dir in $sync_web_dirs ; do
echononl " Syncinc directory \"${sync_dir}\".."
rsync -av -e ssh --delete $old_server_ip:$sync_dir $(dirname $sync_dir)/ >> $log_file 2> $tmp_err_msg
if [[ $? -eq 0 ]];then
echo_ok
else
echo_failed
error "$(cat $tmp_err_msg)"
fi
done
fi
echo -e "\n\n \033[37m\033[1mSyncing Vhost Configurations..\033[m\n"
_got_vhost_dirs=false
sync_vhost_dirs="${sync_vhost_dirs##*( )}"
sync_vhost_dirs="${sync_vhost_dirs%%*( )}"
if [[ -z "${sync_vhost_dirs,,}" ]] ; then
warn "No Vhost Configurations for syncing configured"
else
_got_vhost_dirs=true
fi
if $_got_vhost_dirs ; then
log_file=${log_dir}/sync_vhost_configs.log
> $log_file
for sync_dir in $sync_vhost_dirs ; do
echononl " Syncinc directory \"${sync_dir}\".."
rsync -av -e ssh --delete \
--exclude 000-default.conf \
--exclude 000-dehydrated.conf \
--exclude 000-logformat.conf \
$old_server_ip:$sync_dir $(dirname $sync_dir)/ >> $log_file 2> $tmp_err_msg
if [[ $? -eq 0 ]];then
echo_ok
else
echo_failed
error "$(cat $tmp_err_msg)"
fi
done
fi
echo -e "\n\n \033[37m\033[1mConvert IP's in Vhost Configurations..\033[m\n"
if $_got_vhost_dirs ; then
log_file=${log_dir}/convert_vhost_configs.log
> $log_file
for sync_dir in $sync_vhost_dirs ; do
installation_failed=false
vhost_replaced=false
echononl " Convert Vhost Configuration in \"$sync_dir\""
while IFS='' read -r -d '' _file ; do
[[ -d "$_file" ]] && continue
[[ -h "$_file" ]] && continue
[[ "$(basename $_file)" = "000-default.conf" ]] && continue
[[ "$(basename $_file)" = "000-dehydrated.conf" ]] && continue
[[ "$(basename $_file)" = "000-logformat.conf" ]] && continue
if [[ -f "$_file" ]]; then
# - Replace old IPv4 Address with the new one
# -
if [[ -n "$ipv6_old" ]] && [[ -n "$ipv6_new" ]] ; then
perl -i -n -p -e s"#$ipv4_old#$ipv4_new#g" \
$_file >> $log_file 2> $tmp_err_msg
if [[ $? -ne 0 ]]; then
installation_failed=true
error "$(cat $tmp_err_msg)"
else
vhost_replaced=true
fi
fi
# - Replace old IPv6 Address with the new one
# -
if [[ -n "$ipv6_old" ]] && [[ -n "$ipv6_new" ]] ; then
perl -i -n -p -e s"#$ipv6_old#$ipv6_new#g" \
$_file >> $log_file 2> $tmp_err_msg
if [[ $? -ne 0 ]]; then
installation_failed=true
error "$(cat $tmp_err_msg)"
else
vhost_replaced=true
fi
fi
fi
done < <(find $sync_dir -mindepth 1 -maxdepth 1 -type f -print0)
if $installation_failed ; then
echo_failed
elif $vhost_replaced ; then
echo_ok
else
echo_skipped
fi
done
else
echo_skipped
fi
echo -e "\n\n \033[37m\033[1mSyncing Dehydrated Certs Dirs ..\033[m\n"
_got_dehydrated_dirs_dirs=false
sync_dehydrated_dirs="${sync_dehydrated_dirs##*( )}"
sync_dehydrated_dirs="${sync_dehydrated_dirs%%*( )}"
if [[ -z "${sync_dehydrated_dirs,,}" ]] ; then
warn "No other Directories for syncing configured"
else
_got_dehydrated_dirs_dirs=true
fi
if $_got_dehydrated_dirs_dirs ; then
log_file=${log_dir}/sync_dehydrated_dirs.log
> $log_file
for sync_dir in $sync_dehydrated_dirs ; do
echononl " Syncinc directory \"${sync_dir}\".."
rsync -av -e ssh $old_server_ip:$sync_dir $(dirname $sync_dir)/ >> $log_file 2> $tmp_err_msg
if [[ $? -eq 0 ]];then
echo_ok
else
echo_failed
error "$(cat $tmp_err_msg)"
fi
done
fi
echo -e "\n\n \033[37m\033[1mSyncing other Directories..\033[m\n"
_got_other_dirs=false
sync_other_dirs="${sync_other_dirs##*( )}"
sync_other_dirs="${sync_other_dirs%%*( )}"
if [[ -z "${sync_other_dirs,,}" ]] ; then
warn "No other Directories for syncing configured"
else
_got_other_dirs=true
fi
if $_got_other_dirs ; then
log_file=${log_dir}/sync_other_dirs.log
> $log_file
for sync_dir in $sync_other_dirs ; do
IFS=':' read -a _val_arr <<< "${_val}"
_src_dir="${_val_arr[0]}"
if [[ -n "${_val_arr[1]}" ]] ; then
_dst_dir="${_val_arr[1]}"
echononl " Syncinc directory \"${_src_dir}\" --> ${_dst_dir} .."
else
_dst_dir="$_src_dir"
echononl " Syncinc directory \"${sync_dir}\".."
fi
rsync -av -e ssh --delete "$old_server_ip:$src_dir" "${_dst_dir}" >> $log_file 2> $tmp_err_msg
if [[ $? -eq 0 ]];then
echo_ok
else
echo_failed
error "$(cat $tmp_err_msg)"
fi
done
fi
echo -e "\n\n \033[37m\033[1mRestart Services..\033[m\n"
echononl " (Re)start PureFTPd daemon (pure-ftpd)"
if $systemd_exists ; then
if [[ ! -f /etc/init.d/pure-ftpd ]] && [[ ! -f /etc/systemd/system/pure-ftpd.service ]] ; then
echo_skipped
else
systemctl restart pure-ftpd > /dev/null 2> $tmp_err_msg
if [[ $? -eq 0 ]] ; then
echo_ok
else
echo_failed
error "$(cat $tmp_err_msg)"
fi
fi
else
if [[ ! -f /etc/init.d/pure-ftpd ]]; then
echo_skipped
else
/etc/init.d/pure-ftpd restart > /dev/null 2> $tmp_err_msg
if [[ $? -eq 0 ]] ; then
echo_ok
else
echo_failed
error "$(cat $tmp_err_msg)"
fi
fi
fi
echononl " (Re)start Apache webservice"
if $systemd_exists ; then
systemctl restart apache2 > /dev/null 2> $tmp_err_msg
if [[ $? -eq 0 ]] ; then
echo_ok
else
echo_failed
error "$(cat $tmp_err_msg)"
fi
else
/etc/init.d/apache2 restart > /dev/null 2> $tmp_err_msg
if [[ $? -eq 0 ]] ; then
echo_ok
else
echo_failed
error "$(cat $tmp_err_msg)"
fi
fi
rm $tmp_err_msg
echo ""
exit 0
if [[ "$?" -ne 0 ]] ; then
installation_failed=true
error "$(cat $tmp_err_msg)"
fi
if ! $installation_failed ; then
echo_ok
fi

102
OLD/sync_from_old_www2.sh Executable file
View File

@ -0,0 +1,102 @@
#!/usr/bin/env bash
_source_host=d-alt.mx.oopen.de
_databases="phpmyadmin red_stuff_be"
_sync_dirs="/var/www/mysql-www2.oopen.de /var/www/red-stuff-be.oopen.de"
#_sync_dirs="/data/sympa /usr/local/sympa"
remote_mysqldump_binary=/usr/local/mysql/bin/mysqldump
# -------------
# --- Some functions
# -------------
echononl(){
echo X\\c > /tmp/shprompt$$
if [ `wc -c /tmp/shprompt$$ | awk '{print $1}'` -eq 1 ]; then
echo -e -n "$*\\c" 1>&2
else
echo -e -n "$*" 1>&2
fi
rm /tmp/shprompt$$
}
error(){
echo ""
echo -e "\t[ \033[31m\033[1mFehler\033[m ]: $*"
echo ""
}
warn (){
echo ""
echo -e "\t[ \033[33m\033[1mWarning\033[m ]: $*"
echo ""
}
info (){
echo ""
echo -e "\t[ \033[32m\033[1mInfo\033[m ]: $*"
echo ""
}
echo_done() {
echo -e "\033[80G[ \033[32mdone\033[m ]"
}
echo_ok() {
echo -e "\033[80G[ \033[32mok\033[m ]"
}
echo_warning() {
echo -e "\033[80G[ \033[33m\033[1mwarn\033[m ]"
}
echo_failed(){
echo -e "\033[80G[ \033[1;31mfailed\033[m ]"
}
echo_skipped() {
echo -e "\033[80G[ \033[33m\033[1mskipped\033[m ]"
}
echo ""
echononl " Shutting down sympa.."
systemctl stop sympa
if [[ "$?" = "0" ]]; then
echo_ok
else
echo_failed
fi
echo ""
for _dir in $_sync_dirs ; do
echononl " Syncing ${_dir}.."
rsync -a -e ssh --delete ${_source_host}:$_dir `dirname ${_dir}` > /dev/null 2>&1
if [[ "$?" = "0" ]]; then
echo_ok
else
echo_failed
fi
done
echo ""
for _db in $_databases ; do
echononl " Update Database $_db from ${_source_host}.."
ssh ${_source_host} "$remote_mysqldump_binary --login-path=local --opt $_db" | mysql --login-path=local $_db
if [[ "$?" = "0" ]]; then
echo_ok
else
echo_failed
fi
done
echo ""
echo ""
exit 0

147
OLD/sync_to_a-neu.mx.sh Executable file
View File

@ -0,0 +1,147 @@
#!/usr/bin/env bash
_target_hostname=a-neu.mx.oopen.de
_databases="postfix roundcubemail roundcubemail2 roundcubemail-alt"
_web_dirs="adm.oopen.de webmail2.oopen.de webmail-alt.oopen.de webmail.oopen.de webmail-opcache.oopen.de"
# -------------
# --- Some functions
# -------------
echononl(){
echo X\\c > /tmp/shprompt$$
if [ `wc -c /tmp/shprompt$$ | awk '{print $1}'` -eq 1 ]; then
echo -e -n "$*\\c" 1>&2
else
echo -e -n "$*" 1>&2
fi
rm /tmp/shprompt$$
}
error(){
echo ""
echo -e "\t[ \033[31m\033[1mFehler\033[m ]: $*"
echo ""
}
warn (){
echo ""
echo -e "\t[ \033[33m\033[1mWarning\033[m ]: $*"
echo ""
}
info (){
echo ""
echo -e "\t[ \033[32m\033[1mInfo\033[m ]: $*"
echo ""
}
echo_done() {
echo -e "\033[80G[ \033[32mdone\033[m ]"
}
echo_ok() {
echo -e "\033[80G[ \033[32mok\033[m ]"
}
echo_warning() {
echo -e "\033[80G[ \033[33m\033[1mwarn\033[m ]"
}
echo_failed(){
echo -e "\033[80G[ \033[1;31mfailed\033[m ]"
}
echo_skipped() {
echo -e "\033[80G[ \033[33m\033[1mskipped\033[m ]"
}
echo ""
echononl " Syncing /var/QUARANTINE"
rsync -a -e ssh --delete /var/QUARANTINE ${_target_hostname}:/var/ > /dev/null 2>&1
if [[ "$?" = "0" ]]; then
echo_ok
else
echo_failed
fi
echononl " Syncing /var/deleted-maildomains.."
rsync -a -e ssh --delete /var/deleted-maildomains ${_target_hostname}:/var/ > /dev/null 2>&1
if [[ "$?" = "0" ]]; then
echo_ok
else
echo_failed
fi
echononl " Syncing /var/deleted-maildirs.."
rsync -a -e ssh --delete /var/deleted-maildirs ${_target_hostname}:/var/ > /dev/null 2>&1
if [[ "$?" = "0" ]]; then
echo_ok
else
echo_failed
fi
echononl " Syncing /var/vmail.."
rsync -a -e ssh --delete /var/vmail ${_target_hostname}:/var/ > /dev/null 2>&1
if [[ "$?" = "0" ]]; then
echo_ok
else
echo_failed
fi
echo ""
for _db in $_databases ; do
_dump_file="/tmp/$_db-`date +%Y%m%d-%H%M`".sql
echononl " Dumping Database $_db"
su - postgres -c "pg_dump -c -C $_db" > $_dump_file 2> /dev/null
if [[ "$?" = "0" ]]; then
echo_ok
else
echo_failed
fi
echononl " Copy Dumpfile $_db to ${_target_hostname}"
scp $_dump_file ${_target_hostname}:/tmp >/dev/null 2>&1
if [[ "$?" = "0" ]]; then
echo_ok
else
echo_failed
fi
echononl " Delete Dumpfile $_db here.."
rm $_dump_file >/dev/null 2>&1
if [[ "$?" = "0" ]]; then
echo_ok
else
echo_failed
fi
echononl " Restore Database $_db at ${_target_hostname}"
ssh ${_target_hostname} "su - postgres -c \"psql < $_dump_file\" > /dev/null 2>&1"
if [[ "$?" = "0" ]]; then
echo_ok
else
echo_failed
fi
echononl " Delete Dumpfile $_db at ${_target_hostname}.."
ssh ${_target_hostname} "rm $_dump_file" >/dev/null 2>&1
if [[ "$?" = "0" ]]; then
echo_ok
else
echo_failed
fi
echo ""
done
for _dir in $_web_dirs ; do
echononl " Syncing /var/www/${_dir}"
rsync -a -e ssh --delete /var/www/$_dir ${_target_hostname}:/var/www/ > /dev/null 2>&1
if [[ "$?" = "0" ]]; then
echo_ok
else
echo_failed
fi
done
echo ""
exit 0

View File

@ -0,0 +1,147 @@
#!/usr/bin/env bash
_target_hostname=mx-neu.warenform.de
_databases="postfix roundcubemail roundcubemail2 roundcubemail-alt"
_web_dirs="adm.oopen.de webmail2.oopen.de webmail-alt.oopen.de webmail.oopen.de webmail-opcache.oopen.de"
# -------------
# --- Some functions
# -------------
echononl(){
echo X\\c > /tmp/shprompt$$
if [ `wc -c /tmp/shprompt$$ | awk '{print $1}'` -eq 1 ]; then
echo -e -n "$*\\c" 1>&2
else
echo -e -n "$*" 1>&2
fi
rm /tmp/shprompt$$
}
error(){
echo ""
echo -e "\t[ \033[31m\033[1mFehler\033[m ]: $*"
echo ""
}
warn (){
echo ""
echo -e "\t[ \033[33m\033[1mWarning\033[m ]: $*"
echo ""
}
info (){
echo ""
echo -e "\t[ \033[32m\033[1mInfo\033[m ]: $*"
echo ""
}
echo_done() {
echo -e "\033[80G[ \033[32mdone\033[m ]"
}
echo_ok() {
echo -e "\033[80G[ \033[32mok\033[m ]"
}
echo_warning() {
echo -e "\033[80G[ \033[33m\033[1mwarn\033[m ]"
}
echo_failed(){
echo -e "\033[80G[ \033[1;31mfailed\033[m ]"
}
echo_skipped() {
echo -e "\033[80G[ \033[33m\033[1mskipped\033[m ]"
}
echo ""
echononl " Syncing /var/QUARANTINE"
rsync -a -e ssh --delete /var/QUARANTINE ${_target_hostname}:/var/ > /dev/null 2>&1
if [[ "$?" = "0" ]]; then
echo_ok
else
echo_failed
fi
echononl " Syncing /var/deleted-maildomains.."
rsync -a -e ssh --delete /var/deleted-maildomains ${_target_hostname}:/var/ > /dev/null 2>&1
if [[ "$?" = "0" ]]; then
echo_ok
else
echo_failed
fi
echononl " Syncing /var/deleted-maildirs.."
rsync -a -e ssh --delete /var/deleted-maildirs ${_target_hostname}:/var/ > /dev/null 2>&1
if [[ "$?" = "0" ]]; then
echo_ok
else
echo_failed
fi
echononl " Syncing /var/vmail.."
rsync -a -e ssh --delete /var/vmail ${_target_hostname}:/var/ > /dev/null 2>&1
if [[ "$?" = "0" ]]; then
echo_ok
else
echo_failed
fi
echo ""
for _db in $_databases ; do
_dump_file="/tmp/$_db-`date +%Y%m%d-%H%M`".sql
echononl " Dumping Database $_db"
su - postgres -c "pg_dump -c -C $_db" > $_dump_file 2> /dev/null
if [[ "$?" = "0" ]]; then
echo_ok
else
echo_failed
fi
echononl " Copy Dumpfile $_db to ${_target_hostname}"
scp $_dump_file ${_target_hostname}:/tmp >/dev/null 2>&1
if [[ "$?" = "0" ]]; then
echo_ok
else
echo_failed
fi
echononl " Delete Dumpfile $_db here.."
rm $_dump_file >/dev/null 2>&1
if [[ "$?" = "0" ]]; then
echo_ok
else
echo_failed
fi
echononl " Restore Database $_db at ${_target_hostname}"
ssh ${_target_hostname} "su - postgres -c \"psql < $_dump_file\" > /dev/null 2>&1"
if [[ "$?" = "0" ]]; then
echo_ok
else
echo_failed
fi
echononl " Delete Dumpfile $_db at ${_target_hostname}.."
ssh ${_target_hostname} "rm $_dump_file" >/dev/null 2>&1
if [[ "$?" = "0" ]]; then
echo_ok
else
echo_failed
fi
echo ""
done
for _dir in $_web_dirs ; do
echononl " Syncing /var/www/${_dir}"
rsync -a -e ssh --delete /var/www/$_dir ${_target_hostname}:/var/www/ > /dev/null 2>&1
if [[ "$?" = "0" ]]; then
echo_ok
else
echo_failed
fi
done
echo ""
exit 0

744
sync_from_old_server.sh Executable file
View File

@ -0,0 +1,744 @@
#!/usr/bin/env bash
ipv4_old="192.168.82.10"
old_server_ip=$ipv4_old
tmp_err_msg=$(mktemp)
log_dir=/root/sync_from_old_server_logs
# - Sync Home Directoties
# -
sync_home_dirs="
/data/home/anna
/data/home/back
/data/home/birgit
/data/home/buero
/data/home/chris
/data/home/frauke
/data/home/fsj
/data/home/jibran
/data/home/jonas
/data/home/julia
/data/home/kamera
/data/home/localadmin
/data/home/maica
/data/home/praktikum
/data/home/sysadm
/data/home/thomas
"
# - Sync web directories (included document root directory
# -
# - If the Path to the sncing directory differs at destination,
# - you cat use the following syntax:
# -
# - sync_other_dirs="
# - ...
# - <path-to-sync-dir-at-src>/<sync-dir>:<path-to-sync-dir-at-dst><sync-dir>
# - ...
# - "
# -
# -
# - Note:
# - the basename of the destination directory must be the same as
# - the basename of the source directory.
# -
sync_web_dirs=""
# - Sync Apache virtual host configurations
# -
# - Note:
# -
sync_vhost_dirs=""
# - Only needed to replace ip-addresse in virtual host configuration files
# -
ipv4_new=""
ipv6_old=""
ipv6_new=""
# - Sync dehydrated cert directory
# -
sync_dehydrated_dirs=""
#sync_dehydrated_dirs="
# /var/lib/dehydrated/certs
#"
# - Sync other directories
# -
# - If the Path to the sncing directory differs at destination,
# - you cat use the following syntax:
# -
# - sync_other_dirs="
# - ...
# - <path-to-sync-dir-at-src>/<sync-dir>:<path-to-sync-dir-at-dst><sync-dir>
# - ...
# - "
# -
# -
# - Note:
# - the basename of the destination directory must be the same as
# - the basename of the source directory.
# -
sync_other_dirs="
/data/samba
"
# - Sync files
# -
sync_files=""
# - Sync Postgres databases
# -
# - Note:
# - Set to "ALL" if all databases from old server should be synced
# - Leave empty if no databases should be synced.
# -
sync_pgsql_databases=""
# - Sync MySQL databases
# -
# - Note:
# - Set to "ALL" if all databases from old server should be synced
# - Leave empty if no databases should be synced.
# -
# - Example:
# - To sync database 'shop19' aot source host to database 'shop19_dev'
# - at destination host:
# - sync_mysql_databases="
# - ...
# - shop19:shop19_dev
# - ...
# - "
# -
sync_mysql_databases=""
# - mysql_credential_args
# -
# - Example
# - mysql_credential_args="--defaults-file=/usr/local/mysql/sys-maint.cnf"
# - mysql_credential_args="--login-path=local"
# - mysql_credential_args="-u <db_name> -p'<db_passwd>'"
# -
mysql_credential_args="--login-path=local"
# -------------
# --- Some functions
# -------------
echononl(){
echo X\\c > /tmp/shprompt$$
if [ `wc -c /tmp/shprompt$$ | awk '{print $1}'` -eq 1 ]; then
echo -e -n "$*\\c" 1>&2
else
echo -e -n "$*" 1>&2
fi
rm /tmp/shprompt$$
}
fatal(){
echo ""
echo -e "fatal error: $*"
echo ""
echo -e "\t\033[31m\033[1mInstalllation will be interrupted\033[m\033[m"
echo ""
exit 1
}
error(){
echo ""
echo -e "\t[ \033[31m\033[1mFehler\033[m ]: $*"
echo ""
}
warn (){
echo ""
echo -e "\t[ \033[33m\033[1mWarning\033[m ]: $*"
echo ""
}
info (){
echo ""
echo -e "\t[ \033[32m\033[1mInfo\033[m ]: $*"
echo ""
}
echo_done() {
echo -e "\033[80G[ \033[32mdone\033[m ]"
}
echo_ok() {
echo -e "\033[80G[ \033[32mok\033[m ]"
}
echo_warning() {
echo -e "\033[80G[ \033[33m\033[1mwarn\033[m ]"
}
echo_failed(){
echo -e "\033[80G[ \033[1;31mfailed\033[m ]"
}
echo_skipped() {
echo -e "\033[80G[ \033[33m\033[1mskipped\033[m ]"
}
service_exists() {
local n=$1
if [[ $(systemctl list-units --all -t service --full --no-legend "$n.service" | cut -f1 -d' ') == $n.service ]]; then
return 0
else
return 1
fi
}
# ----------
# - At least ip address from old server must be present
# -
if [[ -z "$old_server_ip" ]]; then
fatal "No remote server ip (variable old_server_ip) is given."
fi
# - Is this a systemd system?
# -
if [[ "X$(which systemd)" = "X" ]]; then
systemd_exists=false
else
systemd_exists=true
fi
echo ""
echononl " Test ssh connection to $old_server_ip"
ssh $old_server_ip "ls" > /dev/null 2> $tmp_err_msg
if [[ $? -eq 0 ]];then
echo_ok
else
echo_failed
fatal "$(cat $tmp_err_msg)"
fi
# - Create log directory
# -
echononl " Create log directory \"${log_dir}\""
if [[ -d "$log_dir" ]] ; then
echo_skipped
else
mkdir ${log_dir} > /dev/null 2> $tmp_err_msg
if [[ $? -eq 0 ]];then
echo_ok
else
echo_failed
error "$(cat $tmp_err_msg)"
fi
fi
# - Syncing PostgreSQL Databases.."
# -
echo -e "\n\n \033[37m\033[1mSyncing PostgreSQL Databases..\033[m\n"
_got_pgsql_databases=false
sync_pgsql_databases="${sync_pgsql_databases##*( )}"
sync_pgsql_databases="${sync_pgsql_databases%%*( )}"
if [[ -z "$sync_pgsql_databases" ]]; then
warn "No PostgreSQL databases for syncing configured."
elif [[ "$sync_pgsql_databases" = "ALL" ]]; then
echononl " Get (all) PostgreSQL databases from $old_server_ip.."
_psql_databases=$(ssh $old_server_ip "cd /tmp ; $sudo -u postgres $psql -lt" | grep -v -e"^$" | awk '{print$1}')
if [[ $? -eq 0 ]];then
echo_ok
_got_pgsql_databases=true
else
echo_failed
fi
else
_pgsql_databases_remote="$sync_pgsql_databases"
_got_pgsql_databases=true
fi
if $_got_pgsql_databases ; then
log_file=${log_dir}/sync_pgsql.log
> $log_file
psql=$(ssh $old_server_ip which psql)
pg_dump=$(ssh $old_server_ip which pg_dump)
pg_dumpall=$(ssh $old_server_ip which pg_dumpall)
sudo=$(ssh $old_server_ip which sudo)
su=$(ssh $old_server_ip which su)
for _db in $_pgsql_databases_remote ; do
echononl " Sync Database '$_db'.."
if [[ "${_db}" = "template0" ]] || [[ "${_db}" = "template1" ]] ; then
echo_skipped
continue
fi
if [[ "${_db}" = ":" ]] || [[ "${_db}" = "|" ]] ; then
echo_skipped
continue
fi
ssh $old_server_ip "cd /tmp ; $sudo -u postgres $pg_dump -c $_db" \
| su postgres -lc "psql $_db" >> $log_file 2> $tmp_err_msg
if [[ $? -eq 0 ]];then
echo_ok
else
echo_failed
error "$(cat $tmp_err_msg)"
fi
done
fi
# - Syncing MySQL Databases.."
# -
echo -e "\n\n \033[37m\033[1mSyncing MySQL Databases..\033[m\n"
_got_mysql_databases=false
sync_mysql_databases="${sync_mysql_databases##*( )}"
sync_mysql_databases="${sync_mysql_databases%%*( )}"
if [[ -z "$sync_mysql_databases" ]]; then
warn "No MySQL databases for syncing configured."
elif [[ "$sync_mysql_databases" = "ALL" ]]; then
echononl " Get MySQL databases from $old_server_ip.."
_mysql_databases_remote=$(ssh $old_server_ip "/usr/local/mysql/bin/mysql $mysql_credential_args -N -s -e \"show databases\"")
if [[ $? -eq 0 ]];then
echo_ok
_got_mysql_databases=true
else
echo_failed
fi
else
_mysql_databases_remote="$sync_mysql_databases"
_got_mysql_databases=true
fi
if $_got_mysql_databases ; then
log_file=${log_dir}/sync_mysql.log
> $log_file
mysqldump_flags="--protocol=SOCKET --max_allowed_packet=128M --skip-opt --add-drop-table --add-locks --create-options --quick --set-charset --disable-keys --lock-tables --routines"
# - GET current (global) Autocommit value
# -
echononl " GET current (global) Autocommit value"
CUR_AUTOCOMMIT="$(/usr/local/mysql/bin/mysql $mysql_credential_args -N -s -e "SHOW GLOBAL VARIABLES LIKE 'autocommit'" | awk '{print$2}')" >> $log_file 2> $tmp_err_msg
if [[ $? -eq 0 ]];then
echo_ok
else
echo_failed
error "$(cat $tmp_err_msg)"
fi
# - Set Autocommit OFF
# -
echononl " Set Autocommit to OFF"
mysql $mysql_credential_args -N -s -e "SET GLOBAL AUTOCOMMIT='OFF'" >> $log_file 2> $tmp_err_msg
if [[ $? -eq 0 ]];then
echo_ok
else
echo_failed
error "$(cat $tmp_err_msg)"
fi
echo ""
for _val in $_mysql_databases_remote ; do
IFS=':' read -a _val_arr <<< "${_val}"
_src_db="${_val_arr[0]}"
if [[ -n "${_val_arr[1]}" ]] ; then
_dst_db="${_val_arr[1]}"
echononl " Sync Database '$_src_db' --> '$_dst_db'.. "
else
echononl " Sync Database '$_src_db'.. "
_dst_db="$_src_db"
fi
if [[ "${_src_db}" = "information_schema" ]]; then
echo_skipped
continue
fi
if [[ "${_src_db}" = "performance_schema" ]]; then
echo_skipped
continue
fi
if [[ "${_src_db}" = "mysql" ]]; then
echo_skipped
continue
fi
if [[ "${_src_db}" = "test" ]] || [[ "${_src_db}" = "mysqltest" ]] ; then
echo_skipped
continue
fi
ssh $old_server_ip "/usr/local/mysql/bin/mysqldump $mysql_credential_args $_src_db" | mysql $mysql_credential_args $_dst_db >> $log_file 2> $tmp_err_msg
if [[ $? -eq 0 ]];then
echo_ok
else
echo_failed
error "$(cat $tmp_err_msg)"
fi
done
# - Reset (global) Autocommit value
# -
echo ""
echononl " Reset (global) Autocommit value to '$CUR_AUTOCOMMIT'"
mysql $mysql_credential_args -N -s -e "SET GLOBAL AUTOCOMMIT='$CUR_AUTOCOMMIT'" >> $log_file 2> $tmp_err_msg
if [[ $? -eq 0 ]];then
echo_ok
else
echo_failed
error "$(cat $tmp_err_msg)"
fi
fi
echo -e "\n\n \033[37m\033[1mSyncing Home Directories..\033[m\n"
_got_home_dirs=false
sync_home_dirs="${sync_home_dirs##*( )}"
sync_home_dirs="${sync_home_dirs%%*( )}"
if [[ -z "${sync_home_dirs,,}" ]] ; then
warn "No Home Directories for syncing configured"
else
_got_home_dirs=true
fi
if $_got_home_dirs ; then
log_file=${log_dir}/sync_home_dirs.log
> $log_file
for sync_dir in $sync_home_dirs ; do
echononl " Syncinc directory \"${sync_dir}\".."
rsync -av -e ssh --delete $old_server_ip:$sync_dir $(dirname $sync_dir)/ >> $log_file 2> $tmp_err_msg
if [[ $? -eq 0 ]];then
echo_ok
else
echo_failed
error "$(cat $tmp_err_msg)"
fi
done
fi
echo -e "\n\n \033[37m\033[1mSyncing Files..\033[m\n"
_got_files=false
sync_files="${sync_files##*( )}"
sync_files="${sync_files%%*( )}"
if [[ -z "${sync_files,,}" ]] ; then
warn "No Files for syncing configured"
else
_got_files=true
fi
if $_got_files ; then
log_file=${log_dir}/sync_files.log
> $log_file
for sync_file in $sync_files ; do
echononl " Syncinc file \"${sync_file}\".."
rsync -av -e ssh --delete $old_server_ip:$sync_file $(dirname $sync_file)/ >> $log_file 2> $tmp_err_msg
if [[ $? -eq 0 ]];then
echo_ok
else
echo_failed
error "$(cat $tmp_err_msg)"
fi
done
fi
echo -e "\n\n \033[37m\033[1mSyncing Web Directories..\033[m\n"
_got_web_dirs=false
sync_web_dirs="${sync_web_dirs##*( )}"
sync_web_dirs="${sync_web_dirs%%*( )}"
if [[ -z "${sync_web_dirs,,}" ]] ; then
warn "No Web Directories for syncing configured"
else
_got_web_dirs=true
fi
if $_got_web_dirs ; then
log_file=${log_dir}/sync_web_dirs.log
> $log_file
for _val in $sync_web_dirs ; do
IFS=':' read -a _val_arr <<< "${_val}"
_src_dir="${_val_arr[0]}"
if [[ -n "${_val_arr[1]}" ]] ; then
_dst_dir="${_val_arr[1]}"
echo -e " Syncinc directory \"${_src_dir}\""
echononl " --> ${_dst_dir} .."
else
_dst_dir="$_src_dir"
echononl " Syncinc directory \"${_src_dir}\".."
fi
if [[ "$(basename ${_dst_dir})" != "$(basename ${_src_dir})" ]] ; then
echo_failed
error "The basename of source directory and destination directory must not be deffer!"
continue
fi
rsync -av -e ssh --delete "$old_server_ip:$_src_dir" "$(dirname "${_dst_dir}")" >> $log_file 2> $tmp_err_msg
if [[ $? -eq 0 ]];then
echo_ok
else
echo_failed
error "$(cat $tmp_err_msg)"
fi
done
fi
echo -e "\n\n \033[37m\033[1mSyncing Vhost Configurations..\033[m\n"
_got_vhost_dirs=false
sync_vhost_dirs="${sync_vhost_dirs##*( )}"
sync_vhost_dirs="${sync_vhost_dirs%%*( )}"
if [[ -z "${sync_vhost_dirs,,}" ]] ; then
warn "No Vhost Configurations for syncing configured"
else
_got_vhost_dirs=true
fi
if $_got_vhost_dirs ; then
log_file=${log_dir}/sync_vhost_configs.log
> $log_file
for sync_dir in $sync_vhost_dirs ; do
echononl " Syncinc directory \"${sync_dir}\".."
rsync -av -e ssh --delete \
--exclude 000-default.conf \
--exclude 000-dehydrated.conf \
--exclude 000-logformat.conf \
$old_server_ip:$sync_dir $(dirname $sync_dir)/ >> $log_file 2> $tmp_err_msg
if [[ $? -eq 0 ]];then
echo_ok
else
echo_failed
error "$(cat $tmp_err_msg)"
fi
done
fi
echo -e "\n\n \033[37m\033[1mConvert IP's in Vhost Configurations..\033[m\n"
if $_got_vhost_dirs ; then
log_file=${log_dir}/convert_vhost_configs.log
> $log_file
for sync_dir in $sync_vhost_dirs ; do
installation_failed=false
vhost_replaced=false
echononl " Convert Vhost Configuration in \"$sync_dir\""
while IFS='' read -r -d '' _file ; do
[[ -d "$_file" ]] && continue
[[ -h "$_file" ]] && continue
[[ "$(basename $_file)" = "000-default.conf" ]] && continue
[[ "$(basename $_file)" = "000-dehydrated.conf" ]] && continue
[[ "$(basename $_file)" = "000-logformat.conf" ]] && continue
if [[ -f "$_file" ]]; then
# - Replace old IPv4 Address with the new one
# -
if [[ -n "$ipv6_old" ]] && [[ -n "$ipv6_new" ]] ; then
perl -i -n -p -e s"#$ipv4_old#$ipv4_new#g" \
$_file >> $log_file 2> $tmp_err_msg
if [[ $? -ne 0 ]]; then
installation_failed=true
error "$(cat $tmp_err_msg)"
else
vhost_replaced=true
fi
fi
# - Replace old IPv6 Address with the new one
# -
if [[ -n "$ipv6_old" ]] && [[ -n "$ipv6_new" ]] ; then
perl -i -n -p -e s"#$ipv6_old#$ipv6_new#g" \
$_file >> $log_file 2> $tmp_err_msg
if [[ $? -ne 0 ]]; then
installation_failed=true
error "$(cat $tmp_err_msg)"
else
vhost_replaced=true
fi
fi
fi
done < <(find $sync_dir -mindepth 1 -maxdepth 1 -type f -print0)
if $installation_failed ; then
echo_failed
elif $vhost_replaced ; then
echo_ok
else
echo_skipped
fi
done
else
warn "No Vhost Configurations for syncing configured, so no IP conversion done.."
fi
echo -e "\n\n \033[37m\033[1mSyncing Dehydrated Certs Dirs ..\033[m\n"
_got_dehydrated_dirs_dirs=false
sync_dehydrated_dirs="${sync_dehydrated_dirs##*( )}"
sync_dehydrated_dirs="${sync_dehydrated_dirs%%*( )}"
if [[ -z "${sync_dehydrated_dirs,,}" ]] ; then
warn "No other Directories for syncing configured"
else
_got_dehydrated_dirs_dirs=true
fi
if $_got_dehydrated_dirs_dirs ; then
log_file=${log_dir}/sync_dehydrated_dirs.log
> $log_file
for sync_dir in $sync_dehydrated_dirs ; do
echononl " Syncinc directory \"${sync_dir}\".."
rsync -av -e ssh $old_server_ip:$sync_dir $(dirname $sync_dir)/ >> $log_file 2> $tmp_err_msg
if [[ $? -eq 0 ]];then
echo_ok
else
echo_failed
error "$(cat $tmp_err_msg)"
fi
done
fi
echo -e "\n\n \033[37m\033[1mSyncing other Directories..\033[m\n"
_got_other_dirs=false
sync_other_dirs="${sync_other_dirs##*( )}"
sync_other_dirs="${sync_other_dirs%%*( )}"
if [[ -z "${sync_other_dirs,,}" ]] ; then
warn "No other Directories for syncing configured"
else
_got_other_dirs=true
fi
if $_got_other_dirs ; then
log_file=${log_dir}/sync_other_dirs.log
> $log_file
for _val in $sync_other_dirs ; do
IFS=':' read -a _val_arr <<< "${_val}"
_src_dir="${_val_arr[0]}"
if [[ -n "${_val_arr[1]}" ]] ; then
_dst_dir="${_val_arr[1]}"
echo -e " Syncinc directory \"${_src_dir}\""
echononl " --> ${_dst_dir} .."
else
_dst_dir="$_src_dir"
echononl " Syncinc directory \"${_src_dir}\".."
fi
if [[ "$(basename ${_dst_dir})" != "$(basename ${_src_dir})" ]] ; then
echo_failed
error "The basename of source directory and destination directory must not be deffer!"
continue
fi
rsync -av -e ssh --delete "$old_server_ip:$_src_dir" "$(dirname "${_dst_dir}")" >> $log_file 2> $tmp_err_msg
if [[ $? -eq 0 ]];then
echo_ok
else
echo_failed
error "$(cat $tmp_err_msg)"
fi
done
fi
echo -e "\n\n \033[37m\033[1mRestart Services..\033[m\n"
echononl " (Re)start PureFTPd daemon (pure-ftpd)"
if $systemd_exists ; then
if [[ ! -f /etc/init.d/pure-ftpd ]] && [[ ! -f /etc/systemd/system/pure-ftpd.service ]] ; then
echo_skipped
else
systemctl restart pure-ftpd > /dev/null 2> $tmp_err_msg
if [[ $? -eq 0 ]] ; then
echo_ok
else
echo_failed
error "$(cat $tmp_err_msg)"
fi
fi
else
if [[ ! -f /etc/init.d/pure-ftpd ]]; then
echo_skipped
else
/etc/init.d/pure-ftpd restart > /dev/null 2> $tmp_err_msg
if [[ $? -eq 0 ]] ; then
echo_ok
else
echo_failed
error "$(cat $tmp_err_msg)"
fi
fi
fi
echononl " (Re)start Apache webservice"
if $systemd_exists ; then
if service_exists apache2 ; then
systemctl restart apache2 > /dev/null 2> $tmp_err_msg
if [[ $? -eq 0 ]] ; then
echo_ok
else
echo_failed
error "$(cat $tmp_err_msg)"
fi
else
echo_skipped
fi
elif [[ -f "/etc/init.d/apache2" ]]; then
/etc/init.d/apache2 restart > /dev/null 2> $tmp_err_msg
if [[ $? -eq 0 ]] ; then
echo_ok
else
echo_failed
error "$(cat $tmp_err_msg)"
fi
else
echo_skipped
fi
rm $tmp_err_msg
echo ""
exit 0
if [[ "$?" -ne 0 ]] ; then
installation_failed=true
error "$(cat $tmp_err_msg)"
fi
if ! $installation_failed ; then
echo_ok
fi