Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Backup cron
0 * * * * /usr/bin/env bash /root/backup/db-backup.sh &>> /root/backup/backup.log
0 0 * * * /usr/bin/env bash /root/backup/uploads-backup.sh &>> /root/backup/backup.log
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 -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
#!/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