Skip to content

Instantly share code, notes, and snippets.

@everdark
Last active December 3, 2015 15:20
Show Gist options
  • Save everdark/be73b060a40d9da86a1c to your computer and use it in GitHub Desktop.
Save everdark/be73b060a40d9da86a1c to your computer and use it in GitHub Desktop.
A helper script for backup operation on wordpress in docker
#!/usr/bin/env bash
usage() {
echo ""
echo "Usage: $0 command"
echo ""
echo "commands:"
echo " -b, --backup : backup the current data volume of MySQL container"
echo " -r, --restore : restore the data volume of MySQL container from a backup"
echo " -s, --sync : sync every local and remote file for whichever is newer"
echo " --push : push local directory to remote directory"
echo " --pull : pull remote directory to local directory"
echo ""
exit
}
[[ $# -eq 0 ]] && usage
cmd="$1"
container_mount_dir=/var/lib/mysql
backup_filename=mysqlbackup.tar.gz
mysql_container_name=wordpress_mysql
remote_dir=/home/kylechung/
case $cmd in
-b|--backup)
docker-compose stop
docker run --rm \
--volumes-from $mysql_container_name \
-v $(pwd):/backup \
ubuntu tar zcvf /backup/$backup_filename $container_mount_dir
docker-compose up -d
;;
-r|--restore)
docker-compose stop
docker run --rm \
--volumes-from $mysql_container_name \
-v $(pwd):/backup \
ubuntu bash -c "tar zxvf /backup/$backup_filename"
docker-compose up -d
;;
-s|--sync)
rsync -u -a $(pwd) ocean:$remote_dir
rsync -u -a ocean:$remote_dir $(pwd)
;;
--push)
rsync -a $(pwd) ocean:$remote_dir
;;
--pull)
rsync -a ocean:$remote_dir $(pwd)
;;
*)
echo "Unknown command."
usage
exit 1
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment