Skip to content

Instantly share code, notes, and snippets.

@Dilden
Last active March 3, 2023 15:51
Show Gist options
  • Save Dilden/70645ecd7feffdf914dbd0b92c3d6778 to your computer and use it in GitHub Desktop.
Save Dilden/70645ecd7feffdf914dbd0b92c3d6778 to your computer and use it in GitHub Desktop.
WordPress Backup script
#!/bin/sh
THESITENAME=""
THEDB=""
THEDBUSER=""
THEDBPW=""
THEDATE=`date +%d%m%y%H%M`
# There is a space between the first `/` and the rest of the path in this variable
# It needs to be there for the TAR command to work for some reason
SITE_PATH="/ var/www/"
LOCAL_BACKUP_PATH=""
# Backup MySQL DB
if mysqldump -u $THEDBUSER -p${THEDBPW} $THEDB | gzip > ${LOCAL_BACKUP_PATH}/db_${THESITENAME}_${THEDATE}.bak.gz
then
echo "db backup successful \n";
else
echo "db backup failed \n";
fi
# Backup $SITE_PATH directory
if tar czf ${LOCAL_BACKUP_PATH}/wp_${THESITENAME}_${THEDATE}.tar.gz -C ${SITE_PATH}
then
echo "WP site backup successful \n";
else
echo "WP site backup failed \n";
fi
# Remove files older than 2 days
find ${LOCAL_BACKUP_PATH}/wp_* -mtime +2 -exec rm {} \;
find ${LOCAL_BACKUP_PATH}/db_* -mtime +2 -exec rm {} \;
echo "backup complete $THEDATE \n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment