Skip to content

Commit

Permalink
Fix a major bug while taking backup of files
Browse files Browse the repository at this point in the history
  • Loading branch information
pothi committed Feb 21, 2023
1 parent da77be6 commit 7b18904
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 12 deletions.
8 changes: 8 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
version: 6.1.1
- date: 2023-02-21
- fix a major bug while taking files backup.
- introduce debug variable to print important values.
- suppress warnings in the output of wp transient delete.
- remove sourcing ~/.env
- improve docs

version: 6.0.3
- date: 2023-02-20
- improve docs
Expand Down
15 changes: 12 additions & 3 deletions db-backup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# requirements
# ~/log, ~/backups, ~/path/to/example.com/public

version=6.0.3
version=6.1.1

### Variables - Please do not add trailing slash in the PATHs

Expand All @@ -22,8 +22,13 @@ PASSPHRASE=
# if you have a different pattern, such as ~/app/example.com, please change the following to fit the server environment!
SITES_PATH=${HOME}/sites

# To debug, use any value for "debug", otherwise please leave it empty
debug=

#-------- Do NOT Edit Below This Line --------#

[ "$debug" ] && set -x

log_file=${HOME}/log/backups.log
exec > >(tee -a "${log_file}")
exec 2> >(tee -a "${log_file}" >&2)
Expand All @@ -39,8 +44,11 @@ DOMAIN=
PUBLIC_DIR=public

# get environment variables, if exists
# .envrc is in the following format
# export VARIABLE=value
[ -f "$HOME/.envrc" ] && source ~/.envrc
[ -f "$HOME/.env" ] && source ~/.env
# uncomment the following, if you use .env with the format "VARIABLE=value" (without export)
# if [ -f "$HOME/.env" ]; then; set -a; source ~/.env; set +a; fi

print_help() {
printf '%s\n\n' "Take a database backup"
Expand Down Expand Up @@ -189,7 +197,8 @@ DB_OUTPUT_FILE_NAME=${BACKUP_PATH}/${DOMAIN_FULL_PATH}-${timestamp}.sql.gz
DB_LATEST_FILE_NAME=${BACKUP_PATH}/${DOMAIN_FULL_PATH}-latest.sql.gz

# take actual DB backup
wp --path="${WP_PATH}" transient delete --all
# 2>/dev/null to suppress any warnings / errors
wp --path="${WP_PATH}" transient delete --all 2>/dev/null
if [ -n "$PASSPHRASE" ] ; then
DB_OUTPUT_FILE_NAME="${DB_OUTPUT_FILE_NAME}".gpg
wp --path="${WP_PATH}" db export --no-tablespaces=true --add-drop-table - | gzip | gpg --symmetric --passphrase "$PASSPHRASE" --batch -o "$DB_OUTPUT_FILE_NAME"
Expand Down
26 changes: 21 additions & 5 deletions files-backup-without-uploads.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# no DB backup
# Excludes contain uploads folder.

version=6.0.3
version=6.1.1

### Variables ###

Expand All @@ -23,8 +23,13 @@ PASSPHRASE=
# if you have a different pattern, such as ~/app/example.com, please change the following to fit the server environment!
SITES_PATH=${HOME}/sites

# To debug, use any value for "debug", otherwise please leave it empty
debug=

#-------- Do NOT Edit Below This Line --------#

[ "$debug" ] && set -x

log_file=${HOME}/log/backups.log
exec > >(tee -a "${log_file}")
exec 2> >(tee -a "${log_file}" >&2)
Expand All @@ -40,8 +45,11 @@ DOMAIN=
PUBLIC_DIR=public

# get environment variables, if exists
# .envrc is in the following format
# export VARIABLE=value
[ -f "$HOME/.envrc" ] && source ~/.envrc
[ -f "$HOME/.env" ] && source ~/.env
# uncomment the following, if you use .env with the format "VARIABLE=value" (without export)
# if [ -f "$HOME/.env" ]; then; set -a; source ~/.env; set +a; fi

# printf 'Usage: %s [-b|--bucket <name>] [-k|--keepfor <days>] [-e|--email <email-address>] [-p|--path <WP path>] [-v|--version] [-h|--help] example.com\n' "$0"
print_help() {
Expand Down Expand Up @@ -192,7 +200,7 @@ EXC_PATH[0]='*.log'
EXC_PATH[1]=${exclude_base_path}/.git
EXC_PATH[2]=${exclude_base_path}/wp-content/cache
# need more? - just use the above format
# EXC_PATH[3]=${exclude_base_path}/wp-content/uploads
EXC_PATH[3]=${exclude_base_path}/wp-content/uploads

EXCLUDES=''
for i in "${!EXC_PATH[@]}" ; do
Expand All @@ -201,6 +209,14 @@ for i in "${!EXC_PATH[@]}" ; do
# remember the trailing space; we'll use it later
done

if [ "$debug" ]; then
echo "exclude_base_path: $exclude_base_path"
printf "EXC_PATH: %s\n" "${EXC_PATH[@]}"
echo "EXCLUDES: $EXCLUDES"

# exit
fi

#------------- from db-script.sh --------------#
#------------- end of snippet from db-script.sh --------------#

Expand All @@ -213,11 +229,11 @@ if [ "$PASSPHRASE" ]; then
# using symmetric encryption
# option --batch to avoid passphrase prompt
# encrypting database dump
tar hcz -C "${SITES_PATH}" "${EXCLUDES}" "${dir_to_backup}" | gpg --symmetric --passphrase "$PASSPHRASE" --batch -o "$FULL_BACKUP_FILE_NAME"
tar hcz -C "${SITES_PATH}" ${EXCLUDES} "${dir_to_backup}" | gpg --symmetric --passphrase "$PASSPHRASE" --batch -o "$FULL_BACKUP_FILE_NAME"
else
echo "[Warn] No passphrase provided for encryption!"
echo "[Warn] If you are from Europe, please check GDPR compliance."
tar hczf "${FULL_BACKUP_FILE_NAME}" "${EXCLUDES}" -C "${SITES_PATH}" "${dir_to_backup}" > /dev/null
tar hczf "${FULL_BACKUP_FILE_NAME}" ${EXCLUDES} -C "${SITES_PATH}" "${dir_to_backup}" > /dev/null
fi
if [ "$?" != "0" ]; then
msg="$script_name - [Warn] Something went wrong while taking a local backup."
Expand Down
25 changes: 21 additions & 4 deletions full-backup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# requirements
# ~/log, ~/backups, ~/path/to/example.com/public

version=6.0.3
version=6.1.1

# this script is basically
# files-backup-without-uploads.sh script + part of db-backup.sh script
Expand All @@ -26,8 +26,13 @@ PASSPHRASE=
# if you have a different pattern, such as ~/app/example.com, please change the following to fit the server environment!
SITES_PATH=${HOME}/sites

# To debug, use any value for "debug", otherwise please leave it empty
debug=

#-------- Do NOT Edit Below This Line --------#

[ "$debug" ] && set -x

log_file=${HOME}/log/backups.log
exec > >(tee -a "${log_file}")
exec 2> >(tee -a "${log_file}" >&2)
Expand All @@ -43,8 +48,11 @@ DOMAIN=
PUBLIC_DIR=public

# get environment variables, if exists
# .envrc is in the following format
# export VARIABLE=value
[ -f "$HOME/.envrc" ] && source ~/.envrc
[ -f "$HOME/.env" ] && source ~/.env
# uncomment the following, if you use .env with the format "VARIABLE=value" (without export)
# if [ -f "$HOME/.env" ]; then; set -a; source ~/.env; set +a; fi

# printf 'Usage: %s [-b|--bucket <name>] [-k|--keepfor <days>] [-e|--email <email-address>] [-p|--path <WP path>] [-v|--version] [-h|--help] example.com\n' "$0"
print_help() {
Expand Down Expand Up @@ -204,8 +212,17 @@ for i in "${!EXC_PATH[@]}" ; do
# remember the trailing space; we'll use it later
done

if [ "$debug" ]; then
echo "exclude_base_path: $exclude_base_path"
printf "EXC_PATH: %s\n" "${EXC_PATH[@]}"
echo "EXCLUDES: $EXCLUDES"

# exit
fi

#------------- from db-script.sh --------------#
# take actual DB backup
# 2>/dev/null to suppress any warnings / errors
wp --path="${WP_PATH}" transient delete --all
if ! wp --path="${WP_PATH}" db export --no-tablespaces=true --add-drop-table "$db_dump"; then
msg="$script_name - [Error] Something went wrong while taking DB dump!"
Expand All @@ -225,12 +242,12 @@ if [ "$PASSPHRASE" ]; then
LATEST_FULL_BACKUP_FILE_NAME=${LATEST_FULL_BACKUP_FILE_NAME}.gpg
# using symmetric encryption
# option --batch to avoid passphrase prompt
tar hcz "${EXCLUDES}" -C "${SITES_PATH}" "${dir_to_backup}" | gpg --symmetric --passphrase "$PASSPHRASE" --batch -o "$FULL_BACKUP_FILE_NAME"
tar hcz -C "${SITES_PATH}" ${EXCLUDES} "${dir_to_backup}" | gpg --symmetric --passphrase "$PASSPHRASE" --batch -o "$FULL_BACKUP_FILE_NAME"
else
echo "[Warn] No passphrase provided for encryption!"
echo "[Warn] If you are from Europe, please check GDPR compliance."
# tar hczf ${FULL_BACKUP_FILE_NAME} --warning=no-file-changed ${EXCLUDES} -C ${SITES_PATH} ${dir_to_backup} > /dev/null
tar hczf "${FULL_BACKUP_FILE_NAME}" "${EXCLUDES}" -C "${SITES_PATH}" "${dir_to_backup}" > /dev/null
tar hczf "${FULL_BACKUP_FILE_NAME}" ${EXCLUDES} -C "${SITES_PATH}" "${dir_to_backup}" > /dev/null
fi
if [ "$?" = "0" ]; then
printf "\nBackup is successfully taken locally.\n\n"
Expand Down

0 comments on commit 7b18904

Please sign in to comment.