Last active
December 6, 2022 12:03
-
-
Save swissbuechi/c183d44320219b06db734abe4dd1c30f to your computer and use it in GitHub Desktop.
Script to backup easyengine wordpress sites
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# local backup | |
backup_user="<username>" | |
backup_dst_path="/home/$backup_user/wp_backup" | |
db_container="services_global-db_1" | |
sites_path="/opt/easyengine/sites/" | |
# remote backup copy | |
remote_backup_enabled=true | |
scp_server="<server>" | |
scp_user=$backup_user | |
scp_target_path="/home/$backup_user/backup/" | |
if [ ! -d $sites_path ]; then | |
echo "Directory $sites_path does not exist." | |
exit 1 | |
fi | |
if [ ! -d $backup_dst_path ]; then | |
sudo -u $backup_user mkdir $backup_dst_path | |
fi | |
backup_site () { | |
site_name=$1 | |
site_path="$sites_path$site_name/app" | |
site_config="$site_path/wp-config.php" | |
if [ ! -f $site_config ]; then | |
echo "$site_name ist not a WP site, not able to backup" | |
return 0 | |
fi | |
if [ ! -d $backup_dst_path/$site_name ]; then | |
sudo -u $backup_user mkdir $backup_dst_path/$site_name | |
fi | |
db_backup_name="$site_name-wp-db-backup-"`date "+%Y%m%d%H%M%S"`".sql.gz" | |
files_backup_name="$site_name-wp-files-backup-"`date "+%Y%m%d%H%M%S"`".tar.gz" | |
# wp_upload_folder="htdocs/wp-content/themes" | |
# wp_theme_folder="htdocs/wp-content/uploads" | |
# wp_plugin_folder="htdocs/wp-content/plugins" | |
wp_content_folder="htdocs/wp-content" | |
db_name=`grep DB_NAME $site_config | cut -d \' -f 4` | |
db_username=`grep DB_USER $site_config | cut -d \' -f 4` | |
db_password=`grep DB_PASSWORD $site_config | cut -d \' -f 4` | |
# backup DB | |
docker exec $db_container /usr/bin/mysqldump -u$db_username -p$db_password $db_name | gzip > $backup_dst_path/$site_name/$db_backup_name | |
chown $backup_user:$backup_user $backup_dst_path/$site_name/$db_backup_name | |
# backup files | |
sudo -u $backup_user tar -czf $backup_dst_path/$site_name/$files_backup_name -C $site_path $wp_content_folder | |
# clean up old backups if more than 2 available | |
find $backup_dst_path/$site_name -maxdepth 1 -name "*.sql.gz" -type f | xargs -x ls -t | awk 'NR>2' | xargs --no-run-if-empty rm | |
find $backup_dst_path/$site_name -maxdepth 1 -name "*.tar.gz" -type f | xargs -x ls -t | awk 'NR>2' | xargs --no-run-if-empty rm | |
echo "$site_name WP backup finished successfully at "`date "+%Y%m%d%H%M%S"` | |
} | |
remote_backup () { | |
sudo -u $backup_user rsync -r $backup_dst_path $scp_user@$scp_server:$scp_target_path | |
} | |
sites=( $( ls $sites_path ) ) | |
for site in ${sites[@]} | |
do | |
backup_site $site | |
done | |
if [ "$remote_backup_enabled" = true ]; then | |
remote_backup | |
echo "Remote backup copy finished successfully at "`date "+%Y%m%d%H%M%S"` | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment