-
Notifications
You must be signed in to change notification settings - Fork 1
/
backup.sh
120 lines (92 loc) · 3.16 KB
/
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
#!/bin/bash
cat <<EOF
----------- Important -----------
Backup Database with RCLONE
Source: https://github.com/JuniYadi/backup-db-rclone
---------------------------------
By: Juni Yadi
Update: 2020-08-26 15:18
License: MIT
---------------------------------
EOF
# Default Config Path
backup_config="$HOME/.backup-rclone"
# Custom Config Path
if [ $1 ]; then
backup_config=$1
fi
# Exit If Config Not Found
if [ ! -f $backup_config ]; then
echo "Config File Path $backup_config Not Found.!"
echo "Please Check Config at https://github.com/JuniYadi/backup-db-rclone"
exit
fi
# Load all variable from config path
source $backup_config
# Create Folder Backup
if [ ! -d $HOME/backup ]; then
echo "Create Folder Backup: $HOME/backup"
mkdir $HOME/backup
fi
# Move to Folder Backup
echo "Move to Folder Backup: $HOME/backup"
cd $HOME/backup
# Generate File Backup Name With Date
datenow=$(date '+%Y%m%d_%H%M%S')
filename="$db_name-$datenow"
filename_sql="$filename.sql"
filename_tar="$filename.tar.gz"
# Dump Database
echo "------------------------------"
echo "Backup Database: $filename_sql"
echo "Backup Process Start at $(date '+%Y/%m/%d %H:%M:%S')"
# MySQL Dump With Secure Password Inline
mysqldump --defaults-extra-file=<( echo $'[client]\npassword='"$db_pass" ) $db_opts --host=$db_host --port=$db_port --user=$db_user $db_name > $filename_sql
echo "Backup Process Complete at $(date '+%Y/%m/%d %H:%M:%S')"
# Compress File
echo "------------------------------"
echo "Compress File SQL: $filename_sql to $filename_tar"
echo "Compress File Process Start at $(date '+%Y/%m/%d %H:%M:%S')"
tar cfz $filename_tar $filename_sql
echo "Compress File Complete at $(date '+%Y/%m/%d %H:%M:%S')"
# ----------- Rclone Copy -----------
# Start Backup File with Rclone Scripts
# -----------------------------------
for x in $(echo $rclone_account | sed "s/,/ /g")
do
# Set Name of Account
account_name=$(echo $x | tr a-z A-Z)
echo "------------------------------"
echo "Copy File $filename_tar to $account_name"
echo "PATH: $account_name/$domain/db/$filename_tar"
echo "------------------------------"
rclone -v copy $filename_tar $x:$domain/db
done
# ----------- Rclone Delete -----------
# Start Delete Old Files X Days Scripts
# -------------------------------------
# Only Running if Variable Available
if [ $rclone_auto_delete_account ]; then
for i in $(echo $rclone_auto_delete_account | sed "s/,/ /g")
do
# Set Name of Account
account_name=$(echo $i | tr a-z A-Z)
echo "------------------------------"
echo "Delete Old Files in account: $account_name"
rclone -v delete $i:$domain/db --min-age $rclone_auto_delete_time --drive-use-trash=false
done
fi
# ----------- Delete Files -----------
# Start Delete Local Files
# ------------------------------------
if [ $delete_local_file_sql == "yes" ]; then
echo "------------------------------"
echo "Delete SQL File: $filename_sql"
rm -rf $filename_sql
fi
if [ $delete_local_file_tar == "yes" ]; then
echo "------------------------------"
echo "Delete TAR File: $filename_tar"
rm -rf $filename_tar
fi
echo "------------------------------"