Last active
December 8, 2023 13:31
-
-
Save mutationevent/af46476c378f18c7d87e5f97b082abbf to your computer and use it in GitHub Desktop.
A shell script periodically backup a MySQL database
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 0 * * * /path/to/backup/script.sh |
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
#!/bin/bash | |
# Set the database credentials | |
USERNAME=root | |
PASSWORD=password | |
DATABASE=mydatabase | |
# Set the backup directory and filename | |
BACKUP_DIR="/path/to/backup/directory" | |
BACKUP_FILE="${BACKUP_DIR}/${DATABASE}_$(date +%Y%m%d_%H%M%S).sql" | |
# Perform the backup | |
mysqldump -u ${USERNAME} -p${PASSWORD} ${DATABASE} > ${BACKUP_FILE} | |
# Compress the backup | |
gzip ${BACKUP_FILE} | |
# Set the Dropbox API access token | |
ACCESS_TOKEN=your_access_token | |
# Set the Dropbox directory and filename | |
DROPBOX_DIR="/backups" | |
DROPBOX_FILE="${DATABASE}_$(date +%Y%m%d_%H%M%S).sql.gz" | |
# Upload the file to Dropbox | |
curl -X POST https://content.dropboxapi.com/2/files/upload \ | |
--header "Authorization: Bearer ${ACCESS_TOKEN}" \ | |
--header "Dropbox-API-Arg: {\"path\": \"${DROPBOX_DIR}/${DROPBOX_FILE}\",\"mode\": \"overwrite\"}" \ | |
--header "Content-Type: application/octet-stream" \ | |
--data-binary @${BACKUP_FILE}.gz |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment