-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun-backup.sh
More file actions
executable file
·91 lines (76 loc) · 2.85 KB
/
run-backup.sh
File metadata and controls
executable file
·91 lines (76 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/bin/bash
# Wrapper script to run the containerized backup
# This script should be run from the borgbackup directory
set -e
# Cleanup on interruption
cleanup() {
echo -e "\n${YELLOW}Interrupted, stopping container...${NC}"
sudo docker compose down --remove-orphans 2>/dev/null
exit 130
}
trap cleanup INT TERM
# Colors for output (respect NO_COLOR standard: https://no-color.org/)
if [ -n "${NO_COLOR}" ]; then
RED='' GREEN='' YELLOW='' NC=''
else
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'
fi
echo -e "${GREEN}================================================${NC}"
echo -e "${GREEN}Starting Dockerized BorgBackup${NC}"
echo -e "${GREEN}================================================${NC}"
# Check if we're in the right directory
if [ ! -f "docker-compose.yml" ]; then
echo -e "${RED}Error: docker-compose.yml not found!${NC}"
echo "Please run this script from the borgbackup directory"
exit 1
fi
# Check if .env file exists
if [ ! -f ".env" ]; then
echo -e "${RED}Error: .env file not found!${NC}"
echo "Please copy .env.example to .env and configure it first"
exit 1
fi
# Prevent concurrent execution (protects borg repo from corruption)
LOCK_FILE="/tmp/borgbackup.lock"
exec 200>"$LOCK_FILE"
if ! flock -n 200; then
echo -e "${RED}Error: Another backup is already running${NC}"
echo "Lock file: $LOCK_FILE"
exit 1
fi
# Verify Docker daemon is running
if ! docker info >/dev/null 2>&1; then
echo -e "${RED}Error: Docker daemon is not running${NC}"
echo "Start Docker with: sudo systemctl start docker"
exit 1
fi
# Ensure writable directories exist on host (mounted into container for logs/dumps)
mkdir -p /backup/log /backup/dump 2>/dev/null || sudo mkdir -p /backup/log /backup/dump
# Rebuild the image if needed (in case backup.sh was updated)
echo -e "${YELLOW}Building/updating Docker image...${NC}"
if ! docker compose build --quiet; then
echo -e "${RED}Error: Docker image build failed${NC}"
exit 1
fi
# Run the backup
echo -e "${GREEN}Running backup script...${NC}"
sudo docker compose run --rm borgbackup /usr/sbin/backup.sh
# Get exit code
EXIT_CODE=$?
if [ $EXIT_CODE -eq 0 ]; then
echo -e "${GREEN}================================================${NC}"
echo -e "${GREEN}Backup completed successfully!${NC}"
echo -e "${GREEN}================================================${NC}"
elif [ $EXIT_CODE -eq 1 ]; then
echo -e "${YELLOW}================================================${NC}"
echo -e "${YELLOW}Backup completed with warnings${NC}"
echo -e "${YELLOW}================================================${NC}"
else
echo -e "${RED}================================================${NC}"
echo -e "${RED}Backup failed with exit code: $EXIT_CODE${NC}"
echo -e "${RED}================================================${NC}"
fi
exit $EXIT_CODE