Last active
September 25, 2017 04:41
-
-
Save luisehk/4d77d757329dc3608b06bbc97cf33835 to your computer and use it in GitHub Desktop.
Daily, weekly and monthly backups of host-mounted docker volumes, with slack notifications.
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
#!/bin/bash | |
# make sure we're running as root. need it to backup docker volumes and control services | |
if [ "$EUID" -ne 0 ]; then | |
echo "Please run as root" | |
exit | |
fi | |
# function to send slack notifications | |
SCRIPT_PATH=$( cd "$(dirname "${BASH_SOURCE[0]}")" ; pwd -P ) | |
function slack { | |
sh "${SCRIPT_PATH}/notify-slack.sh" "$@" | |
} | |
# send slack notifications so people prepare if for some reason they're working during the night | |
slack "Gonna backup in 30 minutes. Docker services will be shut down while the backup is made." | |
sleep 20m | |
slack "10 minutes before starting to backup" | |
sleep 5m | |
slack "5 minutes before starting to backup" | |
sleep 5m | |
# start the backup process. stop docker daemon and backup locally first | |
slack "Backup process has started. Docker services are being shut down" | |
mkdir -p /home/<SOME_USER>/.server-backups/daily | |
service docker stop | |
rsync -av --delete /home/<SOME_USER>/<YOUR_VOLUMES_FOLDER>/ /home/<SOME_USER>/.server-backups/daily | |
# start docker daemon again and send the backup to the server | |
service docker start | |
slack "Docker services have been restarted. The backup is being sent to another server." | |
chown -R <SOME_USER>:<SOME_USER> /home/<SOME_USER>/.server-backups/daily | |
sudo -u <SOME_USER> rsync -av --delete /home/<SOME_USER>/.server-backups/daily/ remoteuser@remoteserver:/<REMOTE_FOLDER>/server-backups/daily | |
slack "Daily backup was done succesfully" | |
# detect if weekly and monthly backups apply and make the server do the job | |
DOW=$(date +%u) | |
DOM=$(date +%d) | |
if [ $DOW == 1 ]; then | |
slack "Creating weekly snapshot" | |
sudo -u <SOME_USER> ssh -t remoteuser@remoteserver "rsync -av /<REMOTE_FOLDER>/server-backups/daily/ /<REMOTE_FOLDER>/server-backups/weekly" | |
slack "Weekly snapshot ready" | |
fi | |
if [ $DOM == 01 ]; then | |
slack "Creating monthly snapshot" | |
sudo -u <SOME_USER> ssh -t remoteuser@remoteserver "rsync -av /<REMOTE_FOLDER>/server-backups/daily/ /<REMOTE_FOLDER>/server-backups/monthly" | |
slack "Monthly snapshot ready" | |
fi | |
# cleanup | |
rm -rf /home/<SOME_USER>/.server-backups | |
slack "Everything is backed up. Goodnight." |
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
[Unit] | |
Description=Run daily backup | |
[Service] | |
User=root | |
Type=oneshot | |
ExecStart=/home/<SOME_USER>/server/scripts/backup.sh |
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
[Unit] | |
Description=Run daily backup | |
[Timer] | |
OnCalendar=daily | |
Persistent=true | |
[Install] | |
WantedBy=timers.target |
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
#!/bin/bash | |
MESSAGE=$@ | |
curl -X POST --data-urlencode "payload={\"channel\": \"#server\", \"username\": \"server\", \"text\": \"${MESSAGE}\", \"icon_emoji\": \":shipit:\"}" https://hooks.slack.com/services/<REST_OF_HOOK_URL> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I made this script to make daily, weekly and monthly snapshots of a folder where I have all of my docker volumes, docker-compose files, configuration files, etc.
Didn't make it accept any parameters, I just removed some hard-coded values and replaced them with obvious hints. But it is pretty straightforward so just read it and modify it to fit your needs.
It also send notifications to Slack to keep record of events.
For this to work, you should:
It also can be improved by adding some error handling, and additionally creating timestamped backups.