/
/
/
Ansible role that provisions my storage server.
1#!/bin/bash
2# Restic Client Setup Example
3# Generated by Ansible - DO NOT EDIT MANUALLY
4
5# This script shows how to configure a restic client to use the backup server
6# Copy and customize this script for your backup clients
7
8set -e
9
10# Configuration
11RESTIC_SERVER_URL="http://{{ ansible_default_ipv4.address }}:{{ restic_backup_host_port }}"
12{% if restic_backup_username is defined and restic_backup_username != "" %}
13RESTIC_USERNAME="{{ restic_backup_username }}"
14RESTIC_PASSWORD="{{ restic_backup_password }}"
15{% endif %}
16RESTIC_REPOSITORY="${RESTIC_SERVER_URL}/backup-$(hostname)"
17
18# Export environment variables
19export RESTIC_REPOSITORY
20{% if restic_backup_username is defined and restic_backup_username != "" %}
21export RESTIC_PASSWORD_COMMAND="echo '${RESTIC_PASSWORD}'"
22export RESTIC_REST_USERNAME="${RESTIC_USERNAME}"
23export RESTIC_REST_PASSWORD="${RESTIC_PASSWORD}"
24{% endif %}
25
26# Colors for output
27GREEN='\033[0;32m'
28YELLOW='\033[1;33m'
29RED='\033[0;31m'
30NC='\033[0m'
31
32echo -e "${GREEN}Restic Client Setup for {{ ansible_hostname }}${NC}"
33echo "=============================================="
34echo "Server URL: ${RESTIC_SERVER_URL}"
35echo "Repository: ${RESTIC_REPOSITORY}"
36echo ""
37
38# Function to check if restic is installed
39check_restic() {
40 if ! command -v restic &> /dev/null; then
41 echo -e "${RED}Error: restic is not installed${NC}"
42 echo "Install with:"
43 echo " Ubuntu/Debian: sudo apt install restic"
44 echo " CentOS/RHEL: sudo dnf install restic"
45 echo " macOS: brew install restic"
46 exit 1
47 fi
48 echo -e "${GREEN}â Restic is installed: $(restic version)${NC}"
49}
50
51# Function to test server connection
52test_connection() {
53 echo -n "Testing server connection... "
54 if curl -s --max-time 10 "${RESTIC_SERVER_URL}/" > /dev/null; then
55 echo -e "${GREEN}â Connected${NC}"
56 else
57 echo -e "${RED}â Failed${NC}"
58 echo "Check if the server is running and accessible"
59 exit 1
60 fi
61}
62
63# Function to initialize repository
64init_repo() {
65 echo -n "Checking if repository exists... "
66 if restic snapshots > /dev/null 2>&1; then
67 echo -e "${YELLOW}Repository already exists${NC}"
68 else
69 echo -e "${GREEN}Initializing new repository${NC}"
70 echo "You will be prompted to create a repository password."
71 echo "Keep this password safe - you'll need it to restore backups!"
72 restic init
73 fi
74}
75
76# Function to show example backup commands
77show_examples() {
78 cat << EOF
79
80${GREEN}Example Commands:${NC}
81==================
82
83# Create a backup snapshot
84restic backup /home/user/documents /etc
85
86# List snapshots
87restic snapshots
88
89# Restore a snapshot
90restic restore latest --target /tmp/restore
91
92# Mount repository for browsing
93restic mount /mnt/restic
94
95# Check repository integrity
96restic check
97
98# Cleanup old snapshots (keep last 30 days, 12 months, 10 years)
99restic forget --keep-daily 30 --keep-monthly 12 --keep-yearly 10 --prune
100
101${YELLOW}Automated Backup Script:${NC}
102=========================
103Create a script like this for regular backups:
104
105#!/bin/bash
106export RESTIC_REPOSITORY="${RESTIC_REPOSITORY}"
107{% if restic_backup_username is defined and restic_backup_username != "" %}
108export RESTIC_REST_USERNAME="${RESTIC_USERNAME}"
109export RESTIC_REST_PASSWORD="${RESTIC_PASSWORD}"
110{% endif %}
111
112# Backup important directories
113restic backup \\
114 /home \\
115 /etc \\
116 /var/log \\
117 --exclude=/home/*/.cache \\
118 --exclude=/home/*/Downloads
119
120# Cleanup old snapshots
121restic forget --keep-daily 7 --keep-weekly 4 --keep-monthly 6 --prune
122
123echo "Backup completed: \$(date)"
124
125${YELLOW}Systemd Timer (Optional):${NC}
126==========================
127Create /etc/systemd/system/backup.service:
128
129[Unit]
130Description=Restic backup
131
132[Service]
133Type=oneshot
134ExecStart=/path/to/your/backup-script.sh
135
136Create /etc/systemd/system/backup.timer:
137
138[Unit]
139Description=Run backup daily
140
141[Timer]
142OnCalendar=daily
143Persistent=true
144
145[Install]
146WantedBy=timers.target
147
148Enable with:
149sudo systemctl enable backup.timer
150sudo systemctl start backup.timer
151
152EOF
153}
154
155# Main execution
156main() {
157 check_restic
158 test_connection
159
160 if [ "$1" = "--init" ]; then
161 init_repo
162 fi
163
164 show_examples
165
166 echo ""
167 echo -e "${GREEN}Setup complete!${NC}"
168 echo "Run this script with --init to initialize the repository"
169}
170
171# Check command line arguments
172if [ "$1" = "--help" ] || [ "$1" = "-h" ]; then
173 echo "Usage: $0 [--init] [--help]"
174 echo " --init Initialize the restic repository"
175 echo " --help Show this help message"
176 exit 0
177fi
178
179main "$@"