Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Eboubaker/1452e1413b4fc0e1e8793516b229c7e5 to your computer and use it in GitHub Desktop.
Save Eboubaker/1452e1413b4fc0e1e8793516b229c7e5 to your computer and use it in GitHub Desktop.
follow https://rclone.org/drive/
sudo mkdir /backup
sudo chown -R eboubaker:sysadmin /backup
sudo chmod g+rws /backup
# save as /opt/db-backup.sh
#!/usr/bin/bash
set -e
# change these to your parameters
backup_path=/backup
days_to_expire=365 # remove local files older than `days_to_expire` days, does not affect cloud backup
dbcontainer=example-container-db
database=example-db
password=example-password
gdrivepath=rclone-root-name:/the-backup # setup rclone and change `rclone-root-name` to the name of your remote. change `the-backup` to the new name of the cloud version of `backup_path`
current_date=$(date +%Y-%m-%d)
# create folder if it does not exist
if [ ! -d "$backup_path" ]; then
mkdir "$backup_path"
fi
if [ ! -d "$backup_path/$current_date" ]; then
mkdir -p "$backup_path/$current_date"
fi
if [ ! -f $backup_path/$current_date/db-$(date +%H%M).sql ]; then
docker exec $dbcontainer mysqldump -p$password $database | zip -q $backup_path/$current_date/db-$(date +%H%M).sql.zip -
printf "@ -\n@=db-$(date +%H%M).sql\n" | zipnote -w $backup_path/$current_date/db-$(date +%H%M).sql.zip
fi
# copy to gdrive
rclone copy --ignore-existing $backup_path $gdrivepath
# delete backup older than x day
find $backup_path -type d -mtime +$days_to_expire | xargs rm -Rf

in terminal:

add this line

@daily /usr/bin/bash /opt/db-backup.sh

to

crontab -e

enter input mode with "i" save with ESC:x

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment