-
Notifications
You must be signed in to change notification settings - Fork 0
/
4soulsband_wordpress_backup.sh
executable file
·123 lines (102 loc) · 4.26 KB
/
4soulsband_wordpress_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
#!/usr/bin/env bash
task_name=$1
days_to_keep=$2
mysql_db_name=$3
mysql_user=$4
mysql_password=$5
debug=$6
db_docker_name=4soulsband_mysql_db
wp_source_dir=4soulsband_wordpress
config_source_dir=deploy_configs
backup_dir_name=./4soulsband_backup
combine_file_name=4soulsband
gdrive_folder_id=1gRJ_jMUr_zBazgREn0_TwkcuKVL7vP2W
keep_log_lines=300
if [[ -z $task_name || -z $days_to_keep || -z $mysql_db_name || -z $mysql_user || -z $mysql_password || ! "$task_name" =~ ^(daily|monthly|yearly)$ ]]; then
echo "Need args: task_name (daily, monthly, yearly), days_to_keep (7, 186, 1825), mysql_db_name, mysql_user, mysql_password, [debug (true)]"
echo "In cron: daily 7 copies, monthly 6 copies, yearly 5 copies"
exit 1
fi
timestamp=`date +%Y-%m-%d"T"%H%M%S`
logfile_name="$task_name"_backup.log
backup_task_path=$backup_dir_name/$task_name
logfile_path="$backup_task_path/$logfile_name"
backup_path_timestamp=$backup_dir_name/$task_name/$timestamp
if [[ -n $debug ]]; then
echo "===DEBUG==="
echo '$task_name: '$task_name
echo '$days_to_keep: '$days_to_keep
echo '$logfile_name: '$logfile_name
echo '$logfile_path: '$logfile_path
echo '$backup_dir_name: '$backup_dir_name
echo '$backup_task_path: '$backup_task_path
echo '$backup_path_timestamp: '$backup_path_timestamp
echo "===DEBUG==="
echo
fi
mkdir --parents $backup_path_timestamp
echo "$task_name backup has started: $timestamp" |& tee -a $logfile_path
# Wordpress DB backup
echo "MySQL..." |& tee -a $logfile_path
docker exec $db_docker_name mysqldump --no-tablespaces -u $mysql_user --password=$mysql_password $mysql_db_name \
| gzip -9 > "$backup_path_timestamp/mysql_$timestamp.sql.gz" \
|& tee -a $logfile_path
# Wordpress files backup
echo "Wordpress..." |& tee -a $logfile_path
tar -czf "$backup_path_timestamp/wordpress_$timestamp.tar.gz" $wp_source_dir \
|& tee -a "$logfile_path"
# Env and settings files backup
echo "Settings..." |& tee -a $logfile_path
tar -czf "$backup_path_timestamp/config_$timestamp.tar.gz" $config_source_dir .env README_secret.md\
|& tee -a "$logfile_path"
# Combine files
echo "Combine it into one file..." |& tee -a $logfile_path
tar -cf "$backup_task_path"/"$combine_file_name"_"$timestamp.tar" \
"$backup_path_timestamp/mysql_$timestamp.sql.gz" \
"$backup_path_timestamp/wordpress_$timestamp.tar.gz" \
"$backup_path_timestamp/config_$timestamp.tar.gz"
rm -rf $backup_path_timestamp
# Backup to Google Drive
if [[ "$task_name" =~ ^(monthly|yearly)$ ]]; then
echo "Uploading to google drive..." |& tee -a $logfile_path
echo gdrive_folder_id: $gdrive_folder_id "$backup_task_path"/"$combine_file_name"_"$timestamp.tar" |& tee -a $logfile_path
gdrive upload --parent $gdrive_folder_id "$backup_task_path"/"$combine_file_name"_"$timestamp.tar"
fi
# Backup to Yandex Disk
if [[ "$task_name" =~ ^(monthly|yearly)$ ]]; then
echo "Uploading to yandex disk..." |& tee -a $logfile_path
yandex_token=$(grep YANDEX_TOKEN .env | cut -f2 -d=)
filepath="$backup_task_path"/"$combine_file_name"_"$timestamp.tar"
filename="$combine_file_name"_"$timestamp.tar"
ya_disk_path="Backups/sites_backup/$filename"
echo ya_disk_path: "$ya_disk_path" |& tee -a $logfile_path
curl -v -H "Authorization: OAuth $yandex_token" \
-H "Etag: $(md5sum $filepath | cut -f1 -d' ')" \
-H "Sha256: $(sha256sum $filepath | cut -f1 -d' ')" \
-H "Content-Type: application/binary" \
-X PUT https://webdav.yandex.ru/"$ya_disk_path" \
--data-binary "@$filepath" \
|& tee -a /dev/null
fi
# Log rotate
tail -n $keep_log_lines $logfile_path > /tmp/"$logfile_name".tmp
mv -f /tmp/"$logfile_name".tmp $logfile_path
# Rotate backups
echo "Delete files older than $days_to_keep (Day/Days)" |& tee -a $logfile_path
find $backup_task_path/* ! -path $logfile_path -mtime +$days_to_keep |& tee -a $logfile_path
find $backup_task_path/* ! -path $logfile_path -mtime +$days_to_keep -delete |& tee -a $logfile_path
echo "Deletion completed" |& tee -a $logfile_path
timestamp=`date +%Y-%m-%d"T"%H%M%S`
echo "Backup was ended at: "$timestamp |& tee -a $logfile_path
echo |& tee -a $logfile_path # A newline at the end of the file.
if [[ -n $debug ]]; then
echo "===DEBUG==="
echo "===CAT==="
cat $logfile_path
echo
echo "===TREE==="
tree ./4soulsband_backup -pugsh
echo "===DEBUG==="
echo
fi
exit