This repository was archived by the owner on Oct 21, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathbackup.sh
More file actions
112 lines (87 loc) · 3.43 KB
/
Copy pathbackup.sh
File metadata and controls
112 lines (87 loc) · 3.43 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
#!/bin/bash
set -e # Exit script on error
##
# Styled echo
# @param $1 text
echo_styled()
{
Yellow='\x1B[;33m'
ResetColor='\x1B[0m'
echo "${Yellow}${1}${ResetColor}"
}
# Output program information
version="2.0.0"
author_1="Daniel Ruf (https://daniel-ruf.de)"
author_2="Matthias Schobner (https://schobner.rocks)"
echo_styled "Website backup ${version} by ${author_1} & ${author_2}"
# Pass config file to this script. Example: sh backup.sh my_website.config
CONFIG_FILE=$( dirname "${BASH_SOURCE[0]}" )/$1
echo_styled "Read config file: ${CONFIG_FILE}"
# shellcheck disable=SC1090
source "${CONFIG_FILE}"
# Config
tar_filename="$(date +"%Y_%m_%d_%H_%M_%S")_files.tar.gz"
sql_filename="$(date +"%Y_%m_%d_%H_%M_%S")_database.sql"
local_backups_path="./backups/${1%.config}"
# Set ssh connection details
# shellcheck disable=SC2154
if [ -n "${ssh_config}" ]; then
ssh_connection=$ssh_config
else
ssh_connection=$ssh_user@$ssh_host
fi
echo_styled 'SSH connection configured.'
# Set mysql connection details
echo_styled 'Validate if MySQL configuration is set.'
if [ -n "${db_host}" ] && [ -n "${db_user}" ] && [ -n "${db_password}" ] && [ -n "${db_name}" ]; then
mysql_connection="-h ${db_host} -u ${db_user} -p${db_password} ${db_name}"
echo_styled 'MySQL connection configured.'
fi
# Run backup
if [ -z "${server_website_path}" ]; then
echo 'Config parameter server_website_path is missing'
exit
fi
if [ -z "${server_backups_path}" ]; then
echo 'Config parameter server_backups_path is missing'
exit
fi
echo_styled 'Connect to server for run backup.'
# shellcheck disable=SC2087
# shellcheck disable=SC2154
ssh -T "${ssh_connection}" << EOF
mkdir -p ${server_backups_path}
echo 'Remote backup directory created.'
rm -rfv ${server_backups_path}/*
echo 'Remote backup directory cleaned up.'
tar cfpz ${server_backups_path}/${tar_filename} ${server_website_path} ${server_website_path_exclude} --checkpoint=1000
echo 'Remote files backup completed.'
if [ -n "${mysql_connection}" ]; then mysqldump ${mysql_connection} --result-file=${server_backups_path}/${sql_filename}; fi
if [ -n "${mysql_connection}" ]; then echo 'Remote database backup completed.'; fi
EOF
echo_styled 'Remote backup is ready.'
# Create local backup directory
echo_styled "Create local backup directory: '${local_backups_path}'"
mkdir -p "${local_backups_path}"
echo_styled "Local backup directory created."
# Download remote files
echo_styled 'Connect to server to download the files backup.'
scp "${ssh_connection}:${server_backups_path}/${tar_filename}" "${local_backups_path}/${tar_filename}"
echo_styled 'Downloaded remote files backup.'
if [ -n "${mysql_connection}" ]; then
echo_styled 'Connect to server to download the database backup.'
scp "${ssh_connection}:${server_backups_path}/${sql_filename}" "${local_backups_path}/${sql_filename}"
echo_styled 'Downloaded remote database backup.'
fi
# Remove old files
echo_styled 'Connect to server to cleanup backup folder.'
# shellcheck disable=SC2087
ssh "${ssh_connection}" << EOF
rm ${server_backups_path}/${tar_filename}
echo 'Remote files backup deleted.'
if [ -n "${mysql_connection}" ]; then rm ${server_backups_path}/${sql_filename}; fi
if [ -n "${mysql_connection}" ]; then echo 'Remote database backup deleted.'; fi
rm -rfv ${server_backups_path}/*
echo 'Remote backup directory cleaned up.'
EOF
echo_styled 'Remote backup directory cleaned up.'