2020-07-17 05:02:25 +00:00
|
|
|
#!/bin/bash
|
|
|
|
|
2021-12-20 04:27:12 +00:00
|
|
|
SCRIPT_VERSION="17"
|
2022-03-18 18:09:58 +00:00
|
|
|
SCRIPT_URL='https://raw.githubusercontent.com/amidaware/tacticalrmm/master/backup.sh'
|
2020-10-18 23:45:21 +00:00
|
|
|
|
|
|
|
GREEN='\033[0;32m'
|
|
|
|
YELLOW='\033[1;33m'
|
|
|
|
BLUE='\033[0;34m'
|
|
|
|
RED='\033[0;31m'
|
|
|
|
NC='\033[0m'
|
2021-03-27 20:00:47 +00:00
|
|
|
THIS_SCRIPT=$(readlink -f "$0")
|
2020-10-18 23:45:21 +00:00
|
|
|
|
|
|
|
TMP_FILE=$(mktemp -p "" "rmmbackup_XXXXXXXXXX")
|
|
|
|
curl -s -L "${SCRIPT_URL}" > ${TMP_FILE}
|
|
|
|
NEW_VER=$(grep "^SCRIPT_VERSION" "$TMP_FILE" | awk -F'[="]' '{print $3}')
|
|
|
|
|
|
|
|
if [ "${SCRIPT_VERSION}" -ne "${NEW_VER}" ]; then
|
2021-03-27 20:00:47 +00:00
|
|
|
printf >&2 "${YELLOW}Old backup script detected, downloading and replacing with the latest version...${NC}\n"
|
|
|
|
wget -q "${SCRIPT_URL}" -O backup.sh
|
|
|
|
exec ${THIS_SCRIPT}
|
2020-10-18 23:45:21 +00:00
|
|
|
fi
|
2021-03-27 20:00:47 +00:00
|
|
|
|
|
|
|
rm -f $TMP_FILE
|
|
|
|
|
2020-10-17 04:41:47 +00:00
|
|
|
if [ $EUID -eq 0 ]; then
|
|
|
|
echo -ne "\033[0;31mDo NOT run this script as root. Exiting.\e[0m\n"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2021-02-24 19:23:47 +00:00
|
|
|
POSTGRES_USER=$(grep -w USER /rmm/api/tacticalrmm/tacticalrmm/local_settings.py | sed 's/^.*: //' | sed 's/.//' | sed -r 's/.{2}$//')
|
|
|
|
POSTGRES_PW=$(grep -w PASSWORD /rmm/api/tacticalrmm/tacticalrmm/local_settings.py | sed 's/^.*: //' | sed 's/.//' | sed -r 's/.{2}$//')
|
|
|
|
|
2020-11-26 03:42:27 +00:00
|
|
|
|
2020-07-17 05:02:25 +00:00
|
|
|
if [ ! -d /rmmbackups ]; then
|
|
|
|
sudo mkdir /rmmbackups
|
|
|
|
sudo chown ${USER}:${USER} /rmmbackups
|
|
|
|
fi
|
|
|
|
|
2020-11-26 03:42:27 +00:00
|
|
|
if [ -d /meshcentral/meshcentral-backup ]; then
|
2021-02-06 00:38:29 +00:00
|
|
|
rm -rf /meshcentral/meshcentral-backup/*
|
2020-11-26 03:42:27 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -d /meshcentral/meshcentral-coredumps ]; then
|
|
|
|
rm -f /meshcentral/meshcentral-coredumps/*
|
|
|
|
fi
|
|
|
|
|
2020-12-10 00:00:02 +00:00
|
|
|
printf >&2 "${GREEN}Running postgres vacuum${NC}\n"
|
|
|
|
sudo -u postgres psql -d tacticalrmm -c "vacuum full logs_auditlog"
|
|
|
|
sudo -u postgres psql -d tacticalrmm -c "vacuum full logs_pendingaction"
|
|
|
|
|
2020-07-17 05:02:25 +00:00
|
|
|
dt_now=$(date '+%Y_%m_%d__%H_%M_%S')
|
|
|
|
tmp_dir=$(mktemp -d -t tacticalrmm-XXXXXXXXXXXXXXXXXXXXX)
|
|
|
|
sysd="/etc/systemd/system"
|
|
|
|
|
|
|
|
mkdir -p ${tmp_dir}/meshcentral/mongo
|
|
|
|
mkdir ${tmp_dir}/postgres
|
|
|
|
mkdir ${tmp_dir}/certs
|
|
|
|
mkdir ${tmp_dir}/nginx
|
|
|
|
mkdir ${tmp_dir}/systemd
|
|
|
|
mkdir ${tmp_dir}/rmm
|
2020-09-10 17:09:26 +00:00
|
|
|
mkdir ${tmp_dir}/confd
|
2021-06-07 02:54:52 +00:00
|
|
|
mkdir ${tmp_dir}/redis
|
2020-07-17 05:02:25 +00:00
|
|
|
|
|
|
|
|
2020-10-30 03:55:50 +00:00
|
|
|
pg_dump --dbname=postgresql://"${POSTGRES_USER}":"${POSTGRES_PW}"@127.0.0.1:5432/tacticalrmm | gzip -9 > ${tmp_dir}/postgres/db-${dt_now}.psql.gz
|
2020-07-17 05:02:25 +00:00
|
|
|
|
|
|
|
tar -czvf ${tmp_dir}/meshcentral/mesh.tar.gz --exclude=/meshcentral/node_modules /meshcentral
|
|
|
|
mongodump --gzip --out=${tmp_dir}/meshcentral/mongo
|
|
|
|
|
2020-11-28 06:05:47 +00:00
|
|
|
sudo tar -czvf ${tmp_dir}/certs/etc-letsencrypt.tar.gz -C /etc/letsencrypt .
|
2020-07-17 05:02:25 +00:00
|
|
|
|
|
|
|
sudo tar -czvf ${tmp_dir}/nginx/etc-nginx.tar.gz -C /etc/nginx .
|
|
|
|
|
2020-09-10 17:09:26 +00:00
|
|
|
sudo tar -czvf ${tmp_dir}/confd/etc-confd.tar.gz -C /etc/conf.d .
|
|
|
|
|
2021-06-13 00:10:58 +00:00
|
|
|
sudo gzip -9 -c /var/lib/redis/appendonly.aof > ${tmp_dir}/redis/appendonly.aof.gz
|
2021-06-07 02:54:52 +00:00
|
|
|
|
2021-11-13 21:30:01 +00:00
|
|
|
sudo cp ${sysd}/rmm.service ${sysd}/celery.service ${sysd}/celerybeat.service ${sysd}/meshcentral.service ${sysd}/nats.service ${sysd}/daphne.service ${tmp_dir}/systemd/
|
|
|
|
if [ -f "${sysd}/nats-api.service" ]; then
|
|
|
|
sudo cp ${sysd}/nats-api.service ${tmp_dir}/systemd/
|
2021-04-04 01:13:27 +00:00
|
|
|
fi
|
2020-07-17 05:02:25 +00:00
|
|
|
|
2021-08-30 03:45:35 +00:00
|
|
|
cat /rmm/api/tacticalrmm/tacticalrmm/private/log/django_debug.log | gzip -9 > ${tmp_dir}/rmm/debug.log.gz
|
2020-07-17 05:02:25 +00:00
|
|
|
cp /rmm/api/tacticalrmm/tacticalrmm/local_settings.py /rmm/api/tacticalrmm/app.ini ${tmp_dir}/rmm/
|
|
|
|
cp /rmm/web/.env ${tmp_dir}/rmm/env
|
|
|
|
|
|
|
|
tar -cf /rmmbackups/rmm-backup-${dt_now}.tar -C ${tmp_dir} .
|
|
|
|
|
2020-10-17 04:41:47 +00:00
|
|
|
rm -rf ${tmp_dir}
|
|
|
|
|
2021-12-16 19:22:43 +00:00
|
|
|
echo -ne "${GREEN}Backup saved to /rmmbackups/rmm-backup-${dt_now}.tar${NC}\n"
|