Last active
October 21, 2018 11:39
-
-
Save azyobuzin/9481a9b9096d38454b18c52cefd28d9a to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
# Usage: dockerbackup.sh VolumeName | |
set -e | |
WORKER_USER=backupworker | |
WORKER_HOME="/home/${WORKER_USER}" | |
DEST_DIR_NAME=DockerBackup | |
for volume_name do | |
backup_id="${volume_name}-$(date +%Y%m%d-%H%M%S)" | |
filename="${backup_id}.tar.xz" | |
tmp_filepath="${WORKER_HOME}/${filename}" | |
echo "${filename}" | |
# Get the volume directory | |
volume_dir="$(docker volume inspect -f '{{.Mountpoint}}' "${volume_name}")" | |
# Compress the directory | |
cd "${volume_dir}" | |
tar -caf "${tmp_filepath}" . | |
chown "${WORKER_USER}" "${tmp_filepath}" | |
gdrive_dir="${WORKER_HOME}/gdrive-${backup_id}" | |
dest_dir="${gdrive_dir}/${DEST_DIR_NAME}" | |
worker_script="\ | |
# Mount Google Drive | |
mkdir '${gdrive_dir}' | |
google-drive-ocamlfuse '${gdrive_dir}' | |
# Copy the file | |
mkdir -p '${dest_dir}' | |
cp -n '${tmp_filepath}' '${dest_dir}/${filename}' | |
# Unmount | |
fusermount -u '${gdrive_dir}' | |
rmdir '${gdrive_dir}' | |
" | |
# Upload the file to Google Drive | |
sudo -Hu "${WORKER_USER}" bash -c "${worker_script}" | |
rm "${tmp_filepath}" | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment