Files
scripts/backup/restic_backup.sh
2025-08-13 22:51:13 -03:00

77 lines
2.3 KiB
Bash

#!/bin/bash
user=$1
# Set the source and destination paths
include_file=/home/$user/scripts/backup/include_file.txt
exclude_file=/home/$user/scripts/backup/exclude_file.txt
respository=/mnt/restic/
timestamp=$(date "+%Y-%m-%d_%H:%M:%S")
log_file=/home/$user/scripts/backup/restic_log.log
restic_repo="/mnt/restic"
echo "${timestamp} Starting backup" >> $log_file
echo "User: ${user}" >> $log_file
export RESTIC_PASSWORD=asdf1234
# Stop containers
echo "${timestamp} Stopping containers" >> $log_file
#docker stop $(docker ps -q) >> $log_file
for dir in /home/$user/*; do
# Remove trailing slash
dir=${dir%/}
# Check if it is a directory and does NOT end with "DISABLED"
if [[ -d "$dir" && ! "$dir" =~ DISABLED$ ]]; then
if [ -f "$dir/docker-compose.yml" ]; then
echo "Bringing down Docker Compose in $dir" >> $log_file
(cd "$dir" && docker compose down)
else
echo "No docker-compose.yml found in $dir, skipping." >> $log_file
fi
else
echo "Project is disabled. Skipping $dir" >> $log_file
fi
done
restic_command="restic -r ${restic_repo} backup --files-from ${include_file} --exclude-file=${exclude_file}"
echo "${timestamp} Starting restic backup. Command: ${restic_command}" >> $log_file
# Run restic backup
$restic_command >> $log_file
# Restart containers
echo "Restarting containers" >> $log_file
#docker start $(docker ps -a -q) >> $log_file
for dir in /home/$user/*; do
# Remove trailing slash
dir=${dir%/}
# Check if it is a directory and does NOT end with "DISABLED"
if [[ -d "$dir" && ! "$dir" =~ DISABLED$ ]]; then
if [ -f "$dir/docker-compose.yml" ]; then
echo "Bringing up Docker Compose in $dir" >> $log_file
(cd "$dir" && docker compose up -d)
else
echo "No docker-compose.yml found in $dir, skipping." >> $log_file
fi
else
echo "Project is disabled. Skipping $dir" >> $log_file
fi
done
echo "${timestamp} Backup complete." >> $log_file
# Remove old backups with policy
forget_command="restic -r ${restic_repo} forget --keep-daily 2 --keep-weekly 1 --keep-monthly 1 --keep-yearly 1 --prune"
echo "${timestamp} Removing old backups. Command: ${forget_command}" >> $log_file
$forget_command >> $log_file
echo "${timestamp} Backup script done" >> $log_file