-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackup-configs
86 lines (68 loc) · 2.53 KB
/
backup-configs
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
#!/usr/bin/env bash
#########################################################################
## Title: backup-configs
## Description: Backup all custom configs to the mounted backup location
## Author: Andrew Lamarra
## Created: 4/27/2017
## Last updated: 5/4/2017
## Dependencies: duplicity
#########################################################################
# Check if running as root
if [[ "$EUID" -ne 0 ]]; then exit 1; fi
# Make sure duplicity is installed
command -v duplicity >/dev/null 2>&1 || exit 1;
# Make sure the backup location is mounted
mountdir=/mnt/backup
mountpoint -q "$mountdir" || exit 1;
# Source the PASSPHRASE variable
. /root/.duplicitykey
export PASSPHRASE
# Use the absolute path for the duplicity command
dup="$(which duplicity)"
# Set the base backup directory
budir="$mountdir/$(hostname)"
# Set the base backup url
buurl="file://$budir"
# Set the backup log file
logdir=/var/log/duplicity
bulog="$logdir/duplicity_$(date +%Y%m%d)_backup.log"
# Make sure the log directory exists
mkdir -p "$logdir"
# Optionally, backup a mysql database
mysqldump --all-databases > "$budir/mysql_database_backup.sql"
# Add a header to the logs
echo -e "$(date)\n" >> "$bulog"
# This function will perform 3 steps:
# 1. Make sure the backup location exists
# 2. Run the backups
# 3. Remove all backups older than the last 3 full backups
# All output is saved to a separate file each day in /var/log/duplicity
function three_step {
# Make sure the directory exists
mkdir -p "$budir/$1"
# Indicate which directory is being backed up in the logs
{
echo "###########################################################";
echo "# BACKING UP /$1";
echo "###########################################################";
} >> "$bulog"
# If a second argument exists, then exclude that from the backups.
if [[ $2 ]]; then
# Do a full backup every month. Otherwise, do an incremental.
$dup --full-if-older-than 1M --exclude "**/$2" "/$1" "$buurl/$1" >> "$bulog" 2>&1
else
$dup --full-if-older-than 1M "/$1" "$buurl/$1" >> "$bulog" 2>&1
fi
# Keep only the last 3 full backups
{
echo "Removing old backups if necessary (keep 3 full backups)...";
$dup remove-all-but-n-full 3 --force "$buurl/$1";
echo -e "\n";
} >> "$bulog" 2>&1
}
# Call the 'three_step' function for each directory you wish to backup
# A second argument can be added if there's a subdirectory you wish to exclude
# Leave off the leading slash "/"
three_step "etc/sampledir" "subdirectory"
# Delete old log files (keep 1 month)
find $logdir -mtime +31 -delete