Skip to content

Instantly share code, notes, and snippets.

@mutationevent
Last active December 8, 2023 13:31
Show Gist options
  • Save mutationevent/af46476c378f18c7d87e5f97b082abbf to your computer and use it in GitHub Desktop.
Save mutationevent/af46476c378f18c7d87e5f97b082abbf to your computer and use it in GitHub Desktop.
A shell script periodically backup a MySQL database
0 0 * * * /path/to/backup/script.sh
#!/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