Last active
June 3, 2023 23:32
-
-
Save Eboubaker/1452e1413b4fc0e1e8793516b229c7e5 to your computer and use it in GitHub Desktop.
This file contains 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
follow https://rclone.org/drive/ |
This file contains 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
sudo mkdir /backup | |
sudo chown -R eboubaker:sysadmin /backup | |
sudo chmod g+rws /backup |
This file contains 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
# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment