Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 51 additions & 16 deletions WordPressBackup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -62,32 +62,67 @@ for backupprofile in $profile ; do
db_name=$(grep DB_NAME "${wp_config}" | cut -f4 -d"'")
db_user=$(grep DB_USER "${wp_config}" | cut -f4 -d"'")
db_pass=$(grep DB_PASSWORD "${wp_config}" | cut -f4 -d"'")
db_host=$(grep DB_HOST "${wp_config}" | cut -f4 -d"'")
table_prefix=$(grep table_prefix "${wp_config}" | cut -f2 -d"'")

# Which files & sub-directories to backup of the root directory
if [ "${file_list}" = "" ]; then
file_list_bckp=${wp_root}
else
files=(${file_list})
file_list_absolute=""
for i in "${!files[@]}"
do
file_list_absolute="${file_list_absolute} ${wp_root}/${files[i]}";
done
file_list_bckp=${file_list_absolute}
fi

# Creates a Backup Directory if one does not exist.
mkdir -p ${backup_location}/${user}/${wp_domain}/

# Make Backup location the current directory
cd ${backup_location}/${user}/${wp_domain}

# MySQL Takes a Dump and compress the Home Directory
mysqldump -u ${db_user} -p${db_pass} ${db_name} | gzip > ./${backupname}-DB.sql.gz &&
tar zcPf ./${backupname}-FILES.tar.gz ${wp_root}

# Compresses the MySQL Dump and the Home Directory
tar zcPf ./${wp_domain}-${backupname}.tar.gz ./${backupname}-FILES.tar.gz ./${backupname}-DB.sql.gz
chmod 600 ./${wp_domain}-${backupname}.tar.gz

# Generates the Backup Size
FILENAME=${backup_location}/${user}/${wp_domain}/${wp_domain}-${backupname}.tar.gz
FILESIZE=$(du -h "$FILENAME")
if [ "${quiet}" = "0" ]; then
echo "$FILESIZE"
fi

#Removes the SQL dump and Home DIR to conserve space
rm -rf ./${backupname}-FILES.tar.gz ./${backupname}-DB.sql.gz
if [ "${compressed_tar_file}" != false ]; then
mysqldump -u ${db_user} --host ${db_host} -p${db_pass} ${db_name} | gzip > ./${backupname}-DB.sql.gz &&
tar zcPf ./${backupname}-FILES.tar.gz ${file_list_bckp}

# Compresses the MySQL Dump and the Home Directory
tar zcPf ./${wp_domain}-${backupname}.tar.gz ./${backupname}-FILES.tar.gz ./${backupname}-DB.sql.gz
chmod 600 ./${wp_domain}-${backupname}.tar.gz

# Generates the Backup Size
#FILENAME=${backup_location}/${user}/${wp_domain}/${wp_domain}-${backupname}.tar.gz
FILENAME=${wp_domain}-${backupname}.tar.gz
FILESIZE=$(du -h "$FILENAME")
if [ "${quiet}" = "0" ]; then
echo "$FILESIZE"
fi

#Removes the SQL dump and Home DIR to conserve space
rm -rf ./${backupname}-FILES.tar.gz ./${backupname}-DB.sql.gz

else
mysqldump -u ${db_user} --host ${db_host} -p${db_pass} ${db_name} | gzip > ./${backupname}-DB.sql.gz &&
tar -cPf ./${backupname}-FILES.tar ${file_list_bckp}

# Generates the Backup files Size
if [ "${quiet}" = "0" ]; then
#FILENAME=${backup_location}/${user}/${wp_domain}/${backupname}-DB.sql.gz
FILENAME=${backupname}-DB.sql.gz
FILESIZE=$(du -h "$FILENAME")
echo "$FILESIZE"

#FILENAME=${backup_location}/${user}/${wp_domain}/${backupname}-FILES.tar
FILENAME=${backupname}-FILES.tar
FILESIZE=$(du -h "$FILENAME")
echo "$FILESIZE"
fi

fi

#Deletes any Backup older than X days
find ${backup_location}/${user}/${wp_domain}/ -type f -mtime +${keepdays} -exec rm {} \;
fi
Expand Down
9 changes: 9 additions & 0 deletions backup.profile.example
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ wp_domain=example.com
# Directory that WordPress is installed at
wp_root=/home/admin/example.com/public_html

# Optional: backup only these files & directories to be included
# Example (all WP): wp-* favicon.ico index*
# Example (only user content): wp-content wp-config*
file_list="wp-* favicon.ico index*"

# Optional: if your files & directories to be backed up are too bigger in size, you can set this to false
# and then that backup will not be compressed and the the daily backup were composed by 2 files (SQL.GZ for DB and TAR for files&dirs)
compressed_tar_file=false

# Backup location
backup_location=/backups

Expand Down