set -e
W=/var/www/glarkru/data/www/glark.ru
D=/tmp/gdump
rm -rf $D; mkdir -p $D
echo '== SQL dump =='
mysqldump -uglarkru -p'7Q4j4Y4qQ6c4B4h8P1n3N9p8' --routines --triggers --events --databases glarkru 2>/dev/null | gzip > $D/glarkru_full.sql.gz
ls -la $D/glarkru_full.sql.gz
mysql -uglarkru -p'7Q4j4Y4qQ6c4B4h8P1n3N9p8' glarkru -e "SHOW TABLES;" 2>/dev/null > $D/tables_list.txt
echo '== per-table data dumps =='
for t in $(mysql -uglarkru -p'7Q4j4Y4qQ6c4B4h8P1n3N9p8' glarkru -N -e "SHOW TABLES;" 2>/dev/null); do
  mysqldump -uglarkru -p'7Q4j4Y4qQ6c4B4h8P1n3N9p8' glarkru "$t" 2>/dev/null > $D/table_$t.sql 2>/dev/null || true
done
ls $D | head -80
echo '== src: panel + server docroots =='
cp -r $W/apps $D/src_apps 2>/dev/null || true
cp -r $W/core $D/src_core 2>/dev/null || true
cp -r /var/www/glarkru/data/www/server.glark.ru $D/server.glark.ru 2>/dev/null || true
cp /srv/node_worker.php $D/ 2>/dev/null || true
echo '== conf files =='
mkdir -p $D/conf
for f in $W/.htaccess /var/www/glarkru/data/www/server.glark.ru/.htaccess $W/core/database.php /var/www/glarkru/data/www/server.glark.ru/core/database.php /var/www/glarkru/data/www/_dbase6407d687c9463a9c67a69ac9ef11a7f9.ini /var/www/glarkru/data/www/_dbase6407d687c9463a9c67a69ac9ef11a7f9.dat /var/www/glarkru/data/www/glark.ru/core/INIStorage.php /etc/nginx/vhosts/glarkru/glark.ru.conf /etc/nginx/vhosts/glarkru/server.glark.ru.conf /etc/httpd/conf/vhosts/glarkru/glark.ru /etc/httpd/conf/vhosts/glarkru/server.glark.ru /etc/httpd/conf.d/fcgid.conf /etc/httpd/conf.d/php.conf; do
  [ -r "$f" ] && { mkdir -p "$D/conf/$(dirname $f | sed 's#/##; s#/#_#g')"; cp "$f" "$D/conf/$(dirname $f | sed 's#/##; s#/#_#g')/" 2>/dev/null; }
done
echo '== final ls =='
du -sh $D; ls -la $D | head -40
