forked from opennet/FSBackup
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_backup.sh
More file actions
executable file
·145 lines (121 loc) · 4.1 KB
/
create_backup.sh
File metadata and controls
executable file
·145 lines (121 loc) · 4.1 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
#!/usr/bin/env bash
#
# Backup planner running from crontab.
#
# Example line for crontab:
#
# 18 4 * * * /usr/local/fsbackup/create_backup.sh | mail -aFrom:"FROM NAME<from_example@example.com>" -s "Backup Report: $(hostname), $(hostname -I | awk '{print $1}')" to_example@example.com
#
set -e -o pipefail # Recommended for safety
#--------------------------------------
# Path where fsbackup installed.
#--------------------------------------
backup_path="/usr/local/fsbackup"
#--------------------------------------
# List of fsbackup configuration files, as a Bash array for safety.
# This correctly handles filenames with spaces.
#--------------------------------------
config_files=("cfg_example" "cfg_example_users" "cfg_example_sql")
#--------------------------------------
# MySQL table backup flag, 1 - run, 0 - not run.
#--------------------------------------
backup_mysql=0
#--------------------------------------
# MariaDB table backup flag, 1 - run, 0 - not run.
#--------------------------------------
backup_mariadb=0
#--------------------------------------
# PostgreSQL table backup flag, 1 - run, 0 - not run.
#--------------------------------------
backup_pgsql=0
#--------------------------------------
# SQLite table backup flag, 1 - run, 0 - not run.
#--------------------------------------
backup_sqlite=0
#--------------------------------------
# System parameters backup flag, 1 - run, 0 - not run.
#--------------------------------------
backup_sys=0
#--------------------------------------
# Mount Windows share flag, 1 - run, 0 - not run.
#--------------------------------------
mount_winshare=0
#--------------------------------------
# Mount FTPS share flag, 1 - run, 0 - not run.
#--------------------------------------
mount_ftpsshare=0
#############################################################################
# --- Reliable Lock File Implementation ---
# Creates a lock directory. If it already exists, the script exits.
# `trap` ensures the lock is removed when the script finishes or is interrupted.
LOCK_DIR="/tmp/fsbackup.lock"
if ! mkdir "${LOCK_DIR}" 2>/dev/null; then
echo "!!!!!!!!!!!!!!! $(date) - Backup script is already running. Exiting."
exit 1
fi
trap 'rmdir "${LOCK_DIR}"' EXIT
# --- Main Script Logic ---
# Safely change to the backup directory
if ! cd "${backup_path}"; then
echo "Error: Could not change to backup directory ${backup_path}"
exit 1
fi
# Saving MySQL databases
if (( backup_mysql == 1 )); then
echo "--- Running MySQL backup ---"
./scripts/mysql_backup.sh
fi
# Saving MariaDB databases
if (( backup_mariadb == 1 )); then
echo "--- Running MariaDB backup ---"
./scripts/mariadb_backup.sh
fi
# Saving PostgreSQL databases
if (( backup_pgsql == 1 )); then
echo "--- Running PostgreSQL backup ---"
./scripts/pgsql_backup.sh
fi
# Saving SQLite databases
if (( backup_sqlite == 1 )); then
echo "--- Running SQLite backup ---"
./scripts/sqlite_backup.sh
fi
# Saving system parameters
if (( backup_sys == 1 )); then
echo "--- Running System backup ---"
./scripts/sysbackup.sh
fi
# Mount Windows share
if (( mount_winshare == 1 )); then
echo "--- Mounting Windows share ---"
./scripts/mount-windows-share.sh || exit 1
fi
# Mount FTPS share
if (( mount_ftpsshare == 1 )); then
echo "--- Mounting FTPS share ---"
./scripts/mount-ftps-share.sh || exit 1
fi
# Main backup loop.
echo "--- Starting main backup tasks ---"
num_configs=${#config_files[@]}
for i in "${!config_files[@]}"; do
cur_conf="${config_files[$i]}"
echo "Running fsbackup.pl with config: ${cur_conf}"
./fsbackup.pl "./${cur_conf}"
# Sleep if it's not the last config file in the list
if (( i < num_configs - 1 )); then
echo "Sleeping for 60 seconds before next task..."
sleep 60
fi
done
# Unmounting the Windows share.
if (( mount_winshare == 1 )); then
echo "--- Unmounting Windows share ---"
./scripts/mount-windows-share.sh umount
fi
# Unmounting the FTPS share.
if (( mount_ftpsshare == 1 )); then
echo "--- Unmounting FTPS share ---"
./scripts/mount-ftps-share.sh umount
fi
echo "--- All tasks completed successfully ---"