salt-states/restic/files/backup.sh.jinja

110 lines
4.1 KiB
Plaintext
Raw Normal View History

2023-06-27 07:16:37 +00:00
#!/bin/bash
{%- if pillar['pods']['mariadb'] is defined %}
umask 0077
2023-08-22 08:57:55 +00:00
GOTIFY_URL="{{ pillar.restic.gotify.url }}"
GOTIFY_TITLE="$(hostname)"
GOTIFY_TOKEN="{{ pillar.restic.gotify.token }}"
GOTIFY_PRIO="5"
2023-06-27 07:16:37 +00:00
BACKUP_DIR={{ pillar.pods.mariadb.containers.main.backup_dir }}
2023-08-22 07:15:31 +00:00
databases=$(podman exec -it mariadb-main mariadb -B -u root -p{{ pillar.pods.mariadb.containers.main.env.MYSQL_ROOT_PASSWORD }} -e "SHOW DATABASES;" | tr -d "| " | grep -v Database)
2023-06-27 07:16:37 +00:00
for db in ${databases[@]}; do
db=${db::-1}
if [[ "$db" != "information_schema" ]] && [[ "$db" != "performance_schema" ]] && [[ "$db" != "mysql" ]] && [[ "$db" != _* ]] && [[ "$db" != "sys" ]]; then
echo "Dumping database: $db"
2023-08-22 07:15:31 +00:00
podman exec -it mariadb-main mariadb-dump -u root -p{{ pillar.pods.mariadb.containers.main.env.MYSQL_ROOT_PASSWORD }} --databases $db | gzip > ${BACKUP_DIR}/$(date +"%Y-%m-%d_%H-%M-%S")_$db-sql.gz
2023-06-27 07:16:37 +00:00
fi
done
# Delete the files older than 3 days
echo "removing old mysql dumps"
find $BACKUP_DIR/* -type f -name *-sql.gz -mtime +3 -exec rm {} \;
umask 0022
{%- endif %}
{%- if pillar['containers']['freeipa'] is defined %}
echo "Stopping FreeIPA"
systemctl stop freeipa.service
{%- endif %}
podman run --rm \
--name=restic \
--hostname="$HOSTNAME" \
-v /root/.restic.password:/restic-password:ro \
-v /root:/root \
{%- if pillar.restic.repository is defined %}
-v {{ pillar.restic.mount }}{{ pillar.restic.suffix }}:/repo \
{%- endif %}
{%- for target in pillar.restic.targets %}
-v {{ target }}:{{ target }} \
{%- endfor %}
-v /root/.restic.password:/root/.restic.password \
{{ pillar.restic.image.url }}:{{ pillar.restic.image.tag }} \
restic \
backup \
--password-file=/restic-password \
{%- if pillar.restic.repository is defined %}
--repo=/repo \
{%- else %}
-r sftp:{{ pillar.restic.user }}@{{ pillar.restic.host }}:{{ pillar.restic.mount }}{{ pillar.restic.suffix }} \
{%- endif %}
--exclude="*.tmp" \
--exclude="lost+found" \
--exclude="Cache" \
--exclude="cache" \
--exclude=".cache" \
--exclude="tmp" \
--exclude="temp" \
--exclude="Temp" \
--exclude="/home/*/go" \
--exclude="/home/*/.local/share/virtualenv" \
--exclude="/home/*/.local/share/virtualenvs" \
--exclude="/home/*/VirtualBox VMs" \
--exclude="/home/*/.mozillla/firefox/*/minidumps" \
--exclude="/home/*/.mozillla/firefox/*/storage" \
--exclude="/home/*/.mozillla/firefox/*/extensions.sqlite" \
--exclude="/home/*/.mozillla/firefox/*/urlclassifier3.sqlite" \
--exclude="/home/*/.config/google-chrome/*/Local Storage" \
--exclude="/home/*/.config/google-chrome/*/Session Storage" \
--exclude="/home/*/.config/google-chrome/*/Application Cache" \
--exclude="/home/*/.config/google-chrome/*/History" \
--exclude="/home/*/.config/google-chrome/*/History-journal" \
--exclude="/home/*/.config/google-chrome/*/History Provider Cache" \
--exclude="/home/*/.local/share/flatpak" \
--exclude="/home/*/.var/app/com.slack.Slack" \
--exclude="/home/*/.local/share/Trash" \
--exclude="/home/*/.config/Microsoft/Microsoft Teams" \
--exclude="/home/*/.wine" \
--exclude="/home/*/.vim/bundle" \
--exclude="/home/*/snap" \
--exclude="/home/*/Downloads" \
--exclude="/home/*/Nextcloud" \
--exclude="/home/*/git" \
--exclude="/srv/backup" \
--verbose \
{%- for target in pillar.restic.targets %}
{{ target }} \
{%- endfor %}
return_code=$?
2023-08-22 08:57:55 +00:00
2023-06-27 07:16:37 +00:00
{%- if pillar['containers']['freeipa'] is defined %}
echo "Starting FreeIPA"
systemctl start freeipa.service
{%- endif %}
2023-08-22 08:57:55 +00:00
MESSAGE="$(journalctl -u restic-backup.service --since today | grep Error)"
if [[ "$MESSAGE" != "-- No entries --" ]]; then
curl "$GOTIFY_URL/message?token=$GOTIFY_TOKEN" -F "title=$GOTIFY_TITLE" -F "priority=$GOTIFY_PRIO" -F "message=$MESSAGE"
fi
2023-06-27 07:16:37 +00:00
if [[ $return_code -eq 0 ]]; then
exit 0
fi
MESSAGE="$(journalctl -u restic-backup.service -p 5 --since today)"
curl "$GOTIFY_URL/message?token=$GOTIFY_TOKEN" -F "title=$GOTIFY_TITLE" -F "priority=$GOTIFY_PRIO" -F "message=$MESSAGE"