-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
synology_config_backup.sh
141 lines (106 loc) · 4.11 KB
/
synology_config_backup.sh
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
#!/usr/bin/env bash
#--------------------------------------------------------------------------
# Backup Synology system configuration and copy backup to another NAS
# Must be run as root or scheduled to run as root
#
# Works on DMS 7 and DSM 6
#
# Author: 007revad
# Date/Version: 2024-10-24 v1.1.6
#
# Github: https://github.com/007revad/Synology_Config_Backup
# Script verified at https://www.shellcheck.net/
#--------------------------------------------------------------------------
# Required Setting:
# Set where to save the exported configuration file
Target_DIR=
# Optional Remote Backup Settings:
# Set to yes to enable remote backup. Anything else = no
Remote_Backup=
# Where to copy the exported configuration file to
Remote_Port=22
Remote_IP=
Remote_DIR=
# Local and remote users with SSH key setup
Local_User=
Remote_User=
# Optional 2nd Remote Backup Settings:
# Set to yes to enable remote backup. Anything else = no
Remote2_Backup=
# Where to copy 2nd exported configuration file to
Remote2_Port=22
Remote2_IP=
Remote2_DIR=
# Local and 2nd remote users with SSH key setup
Local2_User=
Remote2_User=
#--------------------------------------------------------------------------
# Nothing below here should need changing
#--------------------------------------------------------------------------
# Set backup filename
# Append date and time to backup file
File_Name="$( hostname )_$( date +%F_%H%M ).dss"
#--------------------------------------------------------------------------
# Check that script is running as root
if [[ $( whoami ) != "root" ]]; then
echo -e "\nERROR: This script must be run as root!\nERROR: $( whoami ) is not root. Aborting.\n"
# Abort script because it isn't being run by root
exit 255
fi
#--------------------------------------------------------------------------
# Export Synology configuration to a Synology directory
echo -e "Starting backup of Synology configuration on $( hostname )\n"
if [[ ! -d "$Target_DIR" ]]; then
echo -e "\nBackup path does not exist:\n${Target_DIR}"
exit 255
fi
cd "${Target_DIR}" || exit 255
if [[ -f "${Target_DIR}/${File_Name}" ]]; then
echo -e "ERROR: Backup file already exists: \n${Target_DIR}/${File_Name}"
exit 255
else
/usr/syno/bin/synoconfbkp export --filepath="${Target_DIR}/${File_Name}" >/dev/null
chown admin:administrators "${Target_DIR}/${File_Name}"
#chmod 770 "${Target_DIR}/${File_Name}"
fi
# Check exported file created
if [[ ! -f "${Target_DIR}/${File_Name}" ]]; then
echo -e "ERROR: Backup file not created: \n${Target_DIR}/${File_Name}"
exit 255
else
#echo "Synology configuration exported to $File_Name on $( hostname )"
#echo "Exported Synology configuration on $( hostname )"
echo "Export successful on $( hostname )"
fi
#--------------------------------------------------------------------------
# Copy backup to remote NAS
# Remote backup
if [[ $Remote_Backup == "yes" ]]; then
# Get remote NAS hostname
Remote_Host=$(nmblookup -A "$Remote_IP" | sed -n 2p | cut -d ' ' -f1)
Remote_Host="${Remote_Host:1}"
if [[ $Remote_Host ]]; then
echo -e "\nCopying backup to ${Remote_Host}"
else
echo -e "\nCopying backup to ${Remote_IP}"
fi
# Push backup to other device (safer for other device to pull backup from read only share)
sudo -u "${Local_User}" scp -P "${Remote_Port}" "${Target_DIR}/${File_Name}" "${Remote_User}@${Remote_IP}:'${Remote_DIR}/'"
fi
# 2nd remote backup
if [[ $Remote2_Backup == "yes" ]]; then
# Get remote NAS hostname
Remote2_Host=$(nmblookup -A "$Remote2_IP" | sed -n 2p | cut -d ' ' -f1)
Remote2_Host="${Remote2_Host:1}"
if [[ $Remote2_Host ]]; then
echo -e "\nCopying backup to ${Remote2_Host}"
else
echo -e "\nCopying backup to ${Remote2_IP}"
fi
# Push backup to other device (safer for other device to pull backup from read only share)
sudo -u "${Local2_User}" scp -P "${Remote2_Port}" "${Target_DIR}/${File_Name}" "${Remote2_User}@${Remote2_IP}:'${Remote2_DIR}/'"
fi
#--------------------------------------------------------------------------
# Finished
echo -e "\nSynology configuration backup complete"
exit