2018-02-18 14:28:52 +00:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
source $(dirname `realpath $0`)/environment.sh;
|
|
|
|
|
|
|
|
#############################################
|
|
|
|
## ##
|
|
|
|
## Executed after launching containers ##
|
|
|
|
## ##
|
|
|
|
#############################################
|
|
|
|
|
|
|
|
|
2018-02-18 18:07:44 +00:00
|
|
|
|
2018-02-18 18:17:18 +00:00
|
|
|
## [1] Wait for database to be READY
|
|
|
|
|
|
|
|
# Restore mariadb database
|
|
|
|
echo -ne " * mariadb socket..";
|
|
|
|
docker exec $MARIADB_SERVICE mysql -uroot -p$MARIADB_ROOT_PASSWORD 2>/dev/null;
|
|
|
|
DB_RESTORED="$?";
|
|
|
|
|
2018-04-04 07:37:35 +00:00
|
|
|
sleep 5s;
|
|
|
|
|
2018-02-18 18:17:18 +00:00
|
|
|
# Try until connection OK
|
2018-03-15 12:36:24 +00:00
|
|
|
while [ "$DB_RESTORED" == "1" ]; do
|
2018-02-18 18:17:18 +00:00
|
|
|
sleep .5;
|
|
|
|
echo -ne ".";
|
2018-03-21 00:18:44 +00:00
|
|
|
timeout 1 docker exec $MARIADB_SERVICE mysql -uroot -p$MARIADB_ROOT_PASSWORD 2>/dev/null;
|
2018-02-18 18:17:18 +00:00
|
|
|
DB_RESTORED="$?";
|
|
|
|
done;
|
|
|
|
|
|
|
|
echo "ready";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-02-19 21:02:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
## [2] Restore database
|
2018-02-18 18:07:44 +00:00
|
|
|
|
|
|
|
# 1. Check if dump file exists
|
|
|
|
echo -ne " * Check dump file...........";
|
2018-02-18 18:17:18 +00:00
|
|
|
test -f $MARIADB_DUMP;
|
2018-02-18 18:07:44 +00:00
|
|
|
DUMP_FILE_EXISTS="$?";
|
|
|
|
|
|
|
|
# 2. Restore database
|
|
|
|
if [ "$DUMP_FILE_EXISTS" = "0" ]; then
|
|
|
|
|
|
|
|
echo "found";
|
|
|
|
|
|
|
|
# Restore mariadb database
|
2018-02-18 18:17:18 +00:00
|
|
|
echo -ne " * Restore database..........";
|
2018-03-20 23:11:11 +00:00
|
|
|
cat $MARIADB_DUMP | docker exec -i $MARIADB_SERVICE mysql -uroot -p$MARIADB_ROOT_PASSWORD 2>/dev/null && echo "restored" || echo "failed";
|
2018-02-18 18:07:44 +00:00
|
|
|
|
|
|
|
else
|
|
|
|
|
2018-03-14 21:12:02 +00:00
|
|
|
echo "missing";
|
|
|
|
|
|
|
|
echo -ne " * Create trigger............";
|
|
|
|
test ! -d ./virtual/ci && mkdir ./virtual/ci;
|
2018-02-18 18:07:44 +00:00
|
|
|
|
|
|
|
# Create 'persistent' directory if missing
|
2018-03-14 21:12:02 +00:00
|
|
|
test ! -d $ROOT/persistent && mkdir -p $ROOT/persistent && echo "created" || ( echo "error"; exit 1 );
|
2018-02-18 18:07:44 +00:00
|
|
|
|
|
|
|
echo " * Create dump file..........created";
|
2018-02-18 18:17:18 +00:00
|
|
|
touch $MARIADB_DUMP;
|
2018-02-18 18:07:44 +00:00
|
|
|
|
|
|
|
|
2018-02-19 21:02:07 +00:00
|
|
|
fi;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## [3] Grant www-data for CI triggers
|
|
|
|
echo -n " * Grant php on triggers.....";
|
2018-02-20 13:14:23 +00:00
|
|
|
docker exec php-fpm chown www-data -R /ci && echo "granted" || (echo "failed"; exit 1);
|
|
|
|
|
|
|
|
|
|
|
|
## [4] Update vhost libs/builds
|
|
|
|
|
|
|
|
# 1. Update composer
|
|
|
|
echo -n " * Build composer............";
|
|
|
|
docker exec php-fpm bash -c "cd /vhost; composer install" >/dev/null 2>&1 && echo "built" || (echo "failed"; exit 1);
|
|
|
|
|
|
|
|
# 2. Re-build js/scss
|
|
|
|
echo -n " * Webpack build.............";
|
2018-06-09 10:25:25 +00:00
|
|
|
docker exec php-fpm bash -c "cd /vhost; npm install; npm update; npm run build" >/dev/null 2>&1 && echo "built" || (echo "failed"; exit 1);
|
2018-05-13 23:47:20 +00:00
|
|
|
|
|
|
|
# 3. touch the file the backup loop will use to run
|
2018-05-14 10:11:49 +00:00
|
|
|
echo -n " * Mariadb backup loop.......";
|
2018-05-13 23:47:20 +00:00
|
|
|
|
|
|
|
backup(){
|
|
|
|
if [ ! -d $ROOT/backups ]; then
|
|
|
|
mkdir $ROOT/backups;
|
|
|
|
fi
|
|
|
|
|
|
|
|
touch $ROOT/backuploop
|
|
|
|
while [ -f $ROOT/backuploop ]; do
|
|
|
|
# sleep 24 hours before next backup kick in
|
|
|
|
sleep 86400;
|
|
|
|
docker exec $MARIADB_SERVICE mysqldump --all-databases --flush-privileges -uroot -p$MARIADB_ROOT_PASSWORD | gzip > $ROOT/backups/$(date -d "today" +"%Y%m%d%H%M").sql.gz;
|
|
|
|
done;
|
|
|
|
}
|
|
|
|
|
2018-05-14 10:11:49 +00:00
|
|
|
backup &
|
2018-05-13 23:47:20 +00:00
|
|
|
echo "done"
|
2018-06-09 10:25:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## [5] Setup/Fix permissions (for API download)
|
|
|
|
|
|
|
|
# 1. Create download directories
|
|
|
|
echo -n " * Create download folders...";
|
|
|
|
docker exec php-fpm bash -c "mkdir -p /vhost/tmp";
|
|
|
|
docker exec php-fpm bash -c "mkdir -p /vhost/public_html/tmp";
|
|
|
|
echo "created"
|
|
|
|
|
|
|
|
# 2. Fix permissions
|
|
|
|
echo -n " * Setup permissions.........";
|
|
|
|
docker exec php-fpm bash -c "chown -R www-data:www-data /vhost";
|
|
|
|
docker exec php-fpm bash -c "chmod -R 775 /vhost";
|
|
|
|
echo "set"
|