-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbackup_script.sh
More file actions
198 lines (170 loc) · 5.38 KB
/
Copy pathbackup_script.sh
File metadata and controls
198 lines (170 loc) · 5.38 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#!/bin/bash
#
# Debian System Backup and Restore Script v3.0
# Enhanced with comprehensive error handling and validation
#
# Author: Volkan Kücükbudak
# Repository: https://github.com/VolkanSah/Debian-System-Backup-and-Restore-Script
# License: GPL v.3
#
# Features:
# - Package list backup/restore
# - Configuration files backup (/etc)
# - Optional full system backup
# - Detailed logging with error checking
# - Permission validation
#
# Usage:
# ./backup_script.sh backup [full]
# ./backup_script.sh restore /path/to/backup [full]
#
#!/bin/bash
# Configuration
BACKUP_DIR="/backup/$(date +%Y%m%d_%H%M%S)"
LOG_FILE="$BACKUP_DIR/backup.log"
EXCLUDE_FILE="/tmp/backup_exclude.txt"
# Function for logging
log() {
echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" | tee -a "$LOG_FILE"
}
# Function to check if command succeeded
check_command() {
if [ $? -eq 0 ]; then
log "SUCCESS: $1"
else
log "ERROR: $1 failed with exit code $?"
exit 1
fi
}
# Backup function
backup() {
# Check if backup directory exists and is writable
if [ ! -d "/backup" ]; then
log "ERROR: /backup directory does not exist"
exit 1
fi
if [ ! -w "/backup" ]; then
log "ERROR: No write permission for /backup directory"
exit 1
fi
mkdir -p "$BACKUP_DIR"
check_command "Creating backup directory $BACKUP_DIR"
# Initialize log file
touch "$LOG_FILE"
check_command "Creating log file"
log "Backup started in $BACKUP_DIR"
# Save list of installed packages
log "Saving package list..."
dpkg --get-selections > "$BACKUP_DIR/installed_packages.list"
check_command "Saving package list"
# Check if package list was actually created and has content
if [ ! -s "$BACKUP_DIR/installed_packages.list" ]; then
log "WARNING: Package list is empty or not created"
else
log "Package list saved with $(wc -l < "$BACKUP_DIR/installed_packages.list") packages"
fi
# Backup configuration files
log "Backing up /etc directory..."
tar czf "$BACKUP_DIR/etc_backup.tar.gz" /etc 2>>"$LOG_FILE"
check_command "Backing up configuration files"
# Check if tar file was created and has reasonable size
if [ -f "$BACKUP_DIR/etc_backup.tar.gz" ]; then
size=$(du -h "$BACKUP_DIR/etc_backup.tar.gz" | cut -f1)
log "Configuration backup created: $size"
else
log "ERROR: Configuration backup file not created"
exit 1
fi
# Create exclusion list
log "Creating exclusion list..."
cat << EOF > "$EXCLUDE_FILE"
/proc/*
/sys/*
/dev/*
/tmp/*
/run/*
/mnt/*
/media/*
/lost+found
/backup/*
EOF
check_command "Creating exclusion list"
# Back up entire file system (optional)
if [ "$FULL_BACKUP" = true ]; then
log "Starting full backup..."
tar czf "$BACKUP_DIR/full_backup.tar.gz" --exclude-from="$EXCLUDE_FILE" / 2>>"$LOG_FILE"
check_command "Full backup"
if [ -f "$BACKUP_DIR/full_backup.tar.gz" ]; then
size=$(du -h "$BACKUP_DIR/full_backup.tar.gz" | cut -f1)
log "Full backup created: $size"
else
log "ERROR: Full backup file not created"
exit 1
fi
fi
# Show final backup summary
log "Backup completed. Files created:"
ls -lh "$BACKUP_DIR"/ >> "$LOG_FILE"
log "Backup completed successfully."
}
# Restore function
restore() {
local restore_dir="$1"
if [ -z "$restore_dir" ]; then
log "Please specify the backup directory."
exit 1
fi
if [ ! -d "$restore_dir" ]; then
log "ERROR: Backup directory $restore_dir does not exist"
exit 1
fi
log "Starting restore from $restore_dir"
# Restore package list
if [ -f "$restore_dir/installed_packages.list" ]; then
log "Restoring package list..."
sudo dpkg --set-selections < "$restore_dir/installed_packages.list"
check_command "Setting package selections"
sudo apt-get -y dselect-upgrade
check_command "Installing packages"
log "Package list restored."
else
log "Warning: Package list not found."
fi
# Restore configuration files
if [ -f "$restore_dir/etc_backup.tar.gz" ]; then
log "Restoring configuration files..."
sudo tar xzf "$restore_dir/etc_backup.tar.gz" -C /
check_command "Restoring configuration files"
log "Configuration files restored."
else
log "Warning: Configuration files backup not found."
fi
# Restore entire file system (optional)
if [ "$FULL_RESTORE" = true ] && [ -f "$restore_dir/full_backup.tar.gz" ]; then
log "Starting full restore..."
sudo tar xzf "$restore_dir/full_backup.tar.gz" -C /
check_command "Full restore"
log "Full restore completed."
fi
log "Restore completed."
}
# Main program
case "$1" in
backup)
FULL_BACKUP=false
[ "$2" = "full" ] && FULL_BACKUP=true
backup
;;
restore)
FULL_RESTORE=false
[ "$3" = "full" ] && FULL_RESTORE=true
restore "$2"
;;
*)
echo "Usage: $0 {backup|restore} [options]"
echo " backup [full] - Performs a backup (optional: full backup)"
echo " restore DIR [full] - Restores from the specified directory"
exit 1
;;
esac
exit 0