-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup
executable file
·73 lines (57 loc) · 2.75 KB
/
backup
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
#!/bin/bash
# Create a directory for backups if it doesn't exist
if ! [ -e "${HOME}/.jb_backup" ]; then
mkdir "${HOME}/.jb_backup"
fi
# Create a directory for the current backup
if ! [ -e "${HOME}/.jb_backup/$(date +%Y%m%d)" ]; then
mkdir "${HOME}/.jb_backup/$(date +%Y%m%d)"
fi
backup_dir_path="${HOME}/.jb_backup/$(date +%Y%m%d)"
# Create a log file
touch "${backup_dir_path}/.backup_log"
backup_log_file="${backup_dir_path}/.backup_log"
# Loop through the .backup_id file, read in backup info
if [ -e "${HOME}/.backup_id" ]; then
while IFS='' read -r line || [[ -n $line ]]; do
# Store each line in an array
id_array+=("${line}")
done < "${HOME}/.backup_id"
echo "Successfully found the backup_id file." | tee -a "${backup_log_file}"
else
# TODO: this should prompt: create a new one?
# TODO: log that a .backup_id file was created
printf "Need a backup_id file.\n\nExiting without backing up\n" | tee -a "${backup_log_file}" && exit
fi
site_name=${id_array[0]}
user_name=${id_array[1]}
backup_filename="$(date +%Y%m%d%H%M%S)_${site_name}.tar.gz"
backup_file_path="${backup_dir_path}/${backup_filename}"
# Log date and time of the backup
echo "Backing up and updating ${site_name}." | tee -a "${backup_log_file}"
echo "Backup date: $(date +%F)" | tee -a "${backup_log_file}"
echo "" | tee -a "${backup_log_file}"
echo "Starting the backup..." | tee -a "${backup_log_file}"
cd "${HOME}/public_html/" || ( printf "Directory not found.\n\nExiting. . ." | tee -a "${backup_log_file}" && exit )
echo "Exporting database..." | tee -a "${backup_log_file}"
db_backup_filename=$( wp db export --porcelain | tee -a "${backup_log_file}" )
echo "Successfully exported database to ${db_backup_filename}"
echo "Compressing..." | tee -a "${backup_log_file}"
tar -cpzf "${backup_dir_path}/${backup_filename}" .
echo "Successfully exported archive to ${backup_file_path}" | tee -a "${backup_log_file}"
echo "Finished creating the backup" | tee -a "${backup_log_file}"
echo "Removing SQL file from the public directory..." | tee -a "${backup_log_file}"
rm "${db_backup_filename}"
echo "The SQL file ${db_backup_filename} successfully deleted" | tee -a "${backup_log_file}"
echo "" | tee -a "${backup_log_file}"
echo "Starting the update process..." | tee -a "${backup_log_file}"
echo "Updating WordPress core..." | tee -a "${backup_log_file}"
wp core update | tee -a "${backup_log_file}"
echo "WordPress core updated" | tee -a "${backup_log_file}"
echo "Updating WordPress plugins..." | tee -a "${backup_log_file}"
wp plugin update --all | tee -a "${backup_log_file}"
echo "WordPress plugins updated" | tee -a "${backup_log_file}"
echo "Done updating" | tee -a "${backup_log_file}"
echo "" | tee -a "${backup_log_file}"
echo "Starting file transfer..." | tee -a "${backup_log_file}"
exit