tacticalrmm/backup.sh

162 lines
5.1 KiB
Bash
Raw Permalink Normal View History

2022-06-15 20:04:58 +00:00
#!/usr/bin/env bash
2020-07-17 05:02:25 +00:00
2024-04-02 15:59:48 +00:00
SCRIPT_VERSION="32"
2020-10-18 23:45:21 +00:00
2024-04-25 22:03:22 +00:00
export DEBIAN_FRONTEND=noninteractive
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
2023-05-11 18:45:41 +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
2022-12-31 23:25:46 +00:00
if [[ $* == *--schedule* ]]; then
2024-04-02 15:59:48 +00:00
if ! sudo -n true 2>/dev/null; then
echo -ne "${RED}Error: Passwordless sudo is required for scheduling.${NC}\n"
exit 1
fi
2023-03-21 05:57:24 +00:00
(
crontab -l 2>/dev/null
2023-08-11 23:05:14 +00:00
echo "0 0 * * * /rmm/backup.sh --auto > /dev/null 2>&1"
2023-03-21 05:57:24 +00:00
) | crontab -
if [ ! -d /rmmbackups ]; then
2023-05-11 18:58:29 +00:00
sudo mkdir /rmmbackups
fi
2023-05-11 18:58:29 +00:00
if [ ! -d /rmmbackups/daily ]; then
2023-05-11 18:54:07 +00:00
sudo mkdir /rmmbackups/daily
fi
if [ ! -d /rmmbackups/weekly ]; then
sudo mkdir /rmmbackups/weekly
fi
if [ ! -d /rmmbackups/monthly ]; then
sudo mkdir /rmmbackups/monthly
fi
2023-05-11 19:00:49 +00:00
sudo chown ${USER}:${USER} -R /rmmbackups
2023-03-21 05:57:24 +00:00
printf >&2 "${GREEN}Backups setup to run at midnight and rotate.${NC}\n"
exit 0
2022-12-31 23:25:46 +00:00
fi
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
2023-07-07 03:59:48 +00:00
if [ -d /meshcentral/meshcentral-backups ]; then
rm -rf /meshcentral/meshcentral-backups/*
fi
2020-11-26 03:42:27 +00:00
if [ -d /meshcentral/meshcentral-coredumps ]; then
rm -f /meshcentral/meshcentral-coredumps/*
fi
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
mkdir ${tmp_dir}/confd
2023-10-20 20:25:43 +00:00
mkdir ${tmp_dir}/opt
2020-07-17 05:02:25 +00:00
POSTGRES_USER=$(/rmm/api/env/bin/python /rmm/api/tacticalrmm/manage.py get_config dbuser)
POSTGRES_PW=$(/rmm/api/env/bin/python /rmm/api/tacticalrmm/manage.py get_config dbpw)
2020-07-17 05:02:25 +00:00
2024-04-19 20:36:11 +00:00
pg_dump --no-privileges --no-owner --dbname=postgresql://"${POSTGRES_USER}":"${POSTGRES_PW}"@localhost:5432/tacticalrmm | gzip -9 >${tmp_dir}/postgres/db-${dt_now}.psql.gz
2020-07-17 05:02:25 +00:00
node /meshcentral/node_modules/meshcentral --dbexport # for import to postgres
2023-01-01 01:06:41 +00:00
if grep -q postgres "/meshcentral/meshcentral-data/config.json"; then
2023-03-21 05:57:24 +00:00
if ! which jq >/dev/null; then
2023-08-11 23:05:14 +00:00
sudo apt-get install -y jq >/dev/null
2023-03-21 05:57:24 +00:00
fi
MESH_POSTGRES_USER=$(jq '.settings.postgres.user' /meshcentral/meshcentral-data/config.json -r)
MESH_POSTGRES_PW=$(jq '.settings.postgres.password' /meshcentral/meshcentral-data/config.json -r)
2024-04-19 20:36:11 +00:00
pg_dump --no-privileges --no-owner --dbname=postgresql://"${MESH_POSTGRES_USER}":"${MESH_POSTGRES_PW}"@localhost:5432/meshcentral | gzip -9 >${tmp_dir}/postgres/mesh-db-${dt_now}.psql.gz
2023-01-01 01:06:41 +00:00
else
2023-03-21 05:57:24 +00:00
mongodump --gzip --out=${tmp_dir}/meshcentral/mongo
2023-01-01 01:06:41 +00:00
fi
2020-07-17 05:02:25 +00:00
tar -czvf ${tmp_dir}/meshcentral/mesh.tar.gz --exclude=/meshcentral/node_modules /meshcentral
2023-07-30 07:14:44 +00:00
if [ -d /etc/letsencrypt ]; then
sudo tar -czvf ${tmp_dir}/certs/etc-letsencrypt.tar.gz -C /etc/letsencrypt .
fi
2023-10-20 20:25:43 +00:00
if [ -d /opt/tactical ]; then
sudo tar -czvf ${tmp_dir}/opt/opt-tactical.tar.gz -C /opt/tactical .
fi
2023-07-30 07:14:44 +00:00
local_settings='/rmm/api/tacticalrmm/tacticalrmm/local_settings.py'
if grep -q CERT_FILE "$local_settings"; then
mkdir -p ${tmp_dir}/certs/custom
CERT_FILE=$(grep "^CERT_FILE" "$local_settings" | awk -F'[= "]' '{print $5}')
KEY_FILE=$(grep "^KEY_FILE" "$local_settings" | awk -F'[= "]' '{print $5}')
cp -p $CERT_FILE ${tmp_dir}/certs/custom/cert
cp -p $KEY_FILE ${tmp_dir}/certs/custom/key
2023-08-29 23:53:19 +00:00
elif grep -q TRMM_INSECURE "$local_settings"; then
mkdir -p ${tmp_dir}/certs/selfsigned
certdir='/etc/ssl/tactical'
cp -p ${certdir}/key.pem ${tmp_dir}/certs/selfsigned/
cp -p ${certdir}/cert.pem ${tmp_dir}/certs/selfsigned/
2023-07-30 07:14:44 +00:00
fi
2020-07-17 05:02:25 +00:00
for i in rmm frontend meshcentral; do
sudo cp /etc/nginx/sites-available/${i}.conf ${tmp_dir}/nginx/
done
2020-07-17 05:02:25 +00:00
sudo tar -czvf ${tmp_dir}/confd/etc-confd.tar.gz -C /etc/conf.d .
sudo cp ${sysd}/rmm.service ${sysd}/celery.service ${sysd}/celerybeat.service ${sysd}/meshcentral.service ${sysd}/nats.service ${sysd}/daphne.service ${sysd}/nats-api.service ${tmp_dir}/systemd/
2020-07-17 05:02:25 +00:00
2023-07-30 07:14:44 +00:00
cp $local_settings ${tmp_dir}/rmm/
2020-07-17 05:02:25 +00:00
2022-12-31 23:41:32 +00:00
if [[ $* == *--auto* ]]; then
2023-03-21 05:57:24 +00:00
month_day=$(date +"%d")
week_day=$(date +"%u")
if [ "$month_day" -eq 10 ]; then
tar -cf /rmmbackups/monthly/rmm-backup-${dt_now}.tar -C ${tmp_dir} .
else
if [ "$week_day" -eq 5 ]; then
tar -cf /rmmbackups/weekly/rmm-backup-${dt_now}.tar -C ${tmp_dir} .
else
tar -cf /rmmbackups/daily/rmm-backup-${dt_now}.tar -C ${tmp_dir} .
fi
fi
2020-07-17 05:02:25 +00:00
2023-03-21 05:57:24 +00:00
rm -rf ${tmp_dir}
2023-07-05 21:38:30 +00:00
find /rmmbackups/daily/ -type f -mtime +14 -name '*.tar' -execdir rm -- '{}' \;
find /rmmbackups/weekly/ -type f -mtime +60 -name '*.tar' -execdir rm -- '{}' \;
find /rmmbackups/monthly/ -type f -mtime +380 -name '*.tar' -execdir rm -- '{}' \;
2023-03-21 05:57:24 +00:00
echo -ne "${GREEN}Backup Completed${NC}\n"
exit
2022-12-31 23:41:32 +00:00
else
tar -cf /rmmbackups/rmm-backup-${dt_now}.tar -C ${tmp_dir} .
2023-09-13 20:29:33 +00:00
rm -rf ${tmp_dir}
2022-12-31 23:25:46 +00:00
2023-03-21 05:57:24 +00:00
echo -ne "${GREEN}Backup saved to /rmmbackups/rmm-backup-${dt_now}.tar${NC}\n"
2022-12-31 23:41:32 +00:00
fi