Last active
November 17, 2024 04:34
-
-
Save itzikbenh/2c73c687858989a95c052550e0480fde to your computer and use it in GitHub Desktop.
Backup cron
This file contains hidden or 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
0 * * * * /usr/bin/env bash /root/backup/db-backup.sh &>> /root/backup/db-backup.log | |
0 0 * * * /usr/bin/env bash /root/backup/uploads-backup.sh &>> /root/backup/uploads-backup.log |
This file contains hidden or 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
#!/usr/bin/env bash | |
set -e | |
FILE_NAME="db-`date +"%Y-%m-%d-%I:%M:%S%p"`.sql.gz" | |
SAVE_DIR="/root/backup" | |
S3BUCKET="bucket" | |
MYSQL_USER="root" | |
MYSQL_PASSWORD="root" | |
YEAR_DIR=$(date +%Y) | |
MONTH_DIR=$(date +%b) | |
DAY_DIR=$(date +%d) | |
mysqldump --single-transaction=TRUE -u ${MYSQL_USER} -p${MYSQL_PASSWORD} db_name | gzip > ${SAVE_DIR}/${FILE_NAME} | |
if [ -e ${SAVE_DIR}/${FILE_NAME} ]; then | |
# Upload to AWS | |
/usr/local/bin/aws s3 cp ${SAVE_DIR}/${FILE_NAME} s3://${S3BUCKET}/'database-backups'/${YEAR_DIR}/${MONTH_DIR}/${DAY_DIR}/${FILE_NAME} | |
# Test result of last command run | |
if [ "$?" -ne "0" ]; then | |
echo "$(tput setaf 1)Upload to AWS failed$(tput sgr0)" | |
exit 1 | |
fi | |
# If success, remove backup file | |
rm ${SAVE_DIR}/${FILE_NAME} | |
# Exit with no error | |
echo "$(tput setaf 2)Upload to AWS succeeded$(tput sgr0)" | |
exit 0 | |
fi | |
# Exit with error if we reach this point | |
echo "$(tput setaf 1)Backup file wasn't created$(tput sgr0)" | |
exit 1 |
This file contains hidden or 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
#!/usr/bin/env bash | |
UPLOADS_DIR="/srv/users/serverpilot/apps/sitename/public/wp-content/uploads" | |
S3BUCKET="bucket" | |
if [ -e ${UPLOADS_DIR} ]; then | |
# Upload to AWS | |
/usr/local/bin/aws s3 sync ${UPLOADS_DIR} s3://${S3BUCKET}/'uploads' | |
# Test result of last command run | |
if [ "$?" -ne "0" ]; then | |
echo "$(tput setaf 1)Upload to AWS failed$(tput sgr0)" | |
exit 1 | |
fi | |
# Exit with no error | |
echo "$(tput setaf 2)Upload to AWS succeeded$(tput sgr0)" | |
exit 0 | |
fi | |
# Exit with error if we reach this point | |
echo "$(tput setaf 1)Backup file wasn't created$(tput sgr0)" | |
exit 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment