diff --git a/.env.sample b/.env.sample new file mode 100644 index 0000000..0d62c72 --- /dev/null +++ b/.env.sample @@ -0,0 +1,8 @@ +# Remote Server Information +REMOTE_USER=your_username +REMOTE_HOST=your_server_address + +# Will be created under the Remote User Home Directory eg: docker-stack-backupper +REMOTE_PATH=folder_path_starting_from_/home/user +# Root folder for your docker-stack volumes eg: /opt/docker-stack/{archive, dashboard, wiki, website} +REMOTE_DIRECTORY_BACKUP=/opt/docker-stack \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..17da2f4 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.env +data +*.tar.gz \ No newline at end of file diff --git a/README.md b/README.md index 0992551..dbea772 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,51 @@ # docker-stack-backupper -Scripts to help backing up Docker Volumes and Images from VPS (usually Cloud) to other systems (usually local) +### Overview + +This toolkit comprises a set of Bash scripts designed to facilitate the backup and restoration of server files and Docker containers. The scripts are built to interact with a remote server, create backups of specified directories and running Docker containers, and retrieve these backups to a local machine. + +### Prerequisites +- Bash +- Docker (for Docker-related operations) +- Access to a remote server with SSH +- Sudo privileges on the machine where the scripts are run + +### Configuration +Copy the ``.env.sample`` into ``.env`` and add your own values + + + +### Scripts Description +1. 01_copy_to_remote.sh + +Copies specified files or directories to a remote server. + +2. 02_tar_folders.sh + +Creates tar.gz backups of each folder in a specified directory and changes the ownership to a user defined in .env. + +3. 03_export_running_docker_images.sh + +Exports all currently running Docker container images as tar.gz files. + +4. 04_copy_backups_local.sh + +Retrieves tar.gz backups from the remote server to the local machine. + +### Usage +Run the scripts in order. + +``01_copy_to_remote.sh`` and ``04_copy_backups_local.sh`` are to be run on your local machine (copy to, copy from) + +``02_tar_folders.sh`` and ``03_export_running_docker_images.sh`` are to be run on the machine that is being backed up (backup data, backup images) + + +### Contribution + +Contributions to this project are welcome. Please follow the standard Git workflow - fork the repository, make your changes, and submit a pull request. + +### License + +This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. + + +Made by me - [CarlosSousa.tech](https://carlossousa.tech) \ No newline at end of file diff --git a/scripts/01_copy_to_remote.sh b/scripts/01_copy_to_remote.sh new file mode 100755 index 0000000..fbfb1c3 --- /dev/null +++ b/scripts/01_copy_to_remote.sh @@ -0,0 +1,9 @@ +#!/bin/bash +source ../.env + +# Ensure the target directory exists on the remote server +ssh ${REMOTE_USER}@${REMOTE_HOST} "mkdir -p /home/${REMOTE_USER}/${REMOTE_PATH}" + +# Copy specified files/folders to the remote server +scp -r ../scripts ${REMOTE_USER}@${REMOTE_HOST}:${REMOTE_PATH} +scp -r ../.env ${REMOTE_USER}@${REMOTE_HOST}:${REMOTE_PATH} diff --git a/scripts/02_tar_folders.sh b/scripts/02_tar_folders.sh new file mode 100755 index 0000000..6c54da8 --- /dev/null +++ b/scripts/02_tar_folders.sh @@ -0,0 +1,12 @@ +#!/bin/bash +source ../.env + +current_date=$(date +%Y-%m-%d) +mkdir -p /home/${REMOTE_USER}/${REMOTE_PATH}/data/${current_date} + +# Tar.gz each folder in the target directory +for dir in ${REMOTE_DIRECTORY_BACKUP}/*/; do + echo "Backing Up: ${dir}" + sudo tar -czf "/home/${REMOTE_USER}/${REMOTE_PATH}/data/${current_date}/$(basename "$dir").tar.gz" -C "$dir" . + sudo chown ${REMOTE_USER}:${REMOTE_USER} "/home/${REMOTE_USER}/${REMOTE_PATH}/data/${current_date}/$(basename "$dir").tar.gz" +done diff --git a/scripts/03_export_running_docker_images.sh b/scripts/03_export_running_docker_images.sh new file mode 100755 index 0000000..14e40b3 --- /dev/null +++ b/scripts/03_export_running_docker_images.sh @@ -0,0 +1,11 @@ +#!/bin/bash +source ../.env + +current_date=$(date +%Y-%m-%d) +mkdir -p /home/${REMOTE_USER}/${REMOTE_PATH}/images/${current_date} + +# List and export all Docker images +docker ps --format "{{.Image}}" | while read image; do + echo "Backing Up: ${image}" + sudo docker save "$image" | gzip > "/home/${REMOTE_USER}/${REMOTE_PATH}/images/${current_date}/${image//\//_}.tar.gz" +done diff --git a/scripts/04_copy_backups_local.sh b/scripts/04_copy_backups_local.sh new file mode 100755 index 0000000..d70687e --- /dev/null +++ b/scripts/04_copy_backups_local.sh @@ -0,0 +1,6 @@ +#!/bin/bash +source ../.env + +# Copy backups from remote server to local machine +scp -r ${REMOTE_USER}@${REMOTE_HOST}:/home/${REMOTE_USER}/${REMOTE_PATH}/data ../ +scp -r ${REMOTE_USER}@${REMOTE_HOST}:/home/${REMOTE_USER}/${REMOTE_PATH}/images ../