diff --git a/.env b/.env index e2e9725..72c8f6d 100644 --- a/.env +++ b/.env @@ -38,6 +38,10 @@ ENV_CRON='0 1 * * *' ### possible values: "postgres", "nextcloud", "none" ENV_PROVISION_MODE="nextcloud" +## storagebox settings +### storagebox settings (optional) +# when not set, defaults to 22 +ENV_TARGET_DOMAIN_PORT="22" ### storagebox settings (needed) ENV_TARGET_DOMAIN=name_of_your_storagebox_domain ENV_TARGET_DOMAIN_USER=login_name_of_your_storagebox diff --git a/.github/workflows/pipeline.yml b/.github/workflows/pipeline.yml index 3cda35b..6cf2a93 100644 --- a/.github/workflows/pipeline.yml +++ b/.github/workflows/pipeline.yml @@ -64,3 +64,69 @@ jobs: password: ${{ secrets.DOCKER_HUB_PASSWORD }} repository: ${{ vars.DOCKER_HUB_USERNAME }}/backup readme-filepath: ./README.md + + + test: + runs-on: ubuntu-latest + + steps: + - name: Checkout repository + uses: actions/checkout@v3 + + - name: Generate SSH Key Pair + run: | + mkdir -p sftp-keys + ssh-keygen -t rsa -b 2048 -f sftp-keys/id_rsa -q -N "" + chmod 600 sftp-keys/id_rsa + + - name: Start SFTP Server with Mounts + run: | + docker run -d --name fake-sftp \ + -p 2222:22 \ + -v $(pwd)/sftp-keys/id_rsa.pub:/home/test/.ssh/keys/id_rsa.pub:ro \ + -v $(pwd)/sftp-data:/home/test/uploads \ + atmoz/sftp test::1001 + + - name: Wait for SFTP server to be ready + run: | + for i in {1..10}; do + if nc -z localhost 2222; then + echo "SFTP server is up!" + exit 0 + fi + echo "Waiting for SFTP server..." + sleep 2 + done + echo "SFTP server did not start in time" && exit 1 + + - name: Create test files + run: | + mkdir -p test-data + echo "File 1 content" > test-data/file1.txt + echo "File 2 content" > test-data/file2.txt + echo "File 3 content" > test-data/file3.txt + + - name: delete before + run: | + docker rmi ${{ vars.DOCKER_HUB_USERNAME }}/backup:test || true + + - uses: docker/setup-buildx-action@v3 + - name: Build for testing + uses: docker/build-push-action@v6 + with: + context: ./docker + push: false + load: true + tags: ${{ vars.DOCKER_HUB_USERNAME }}/backup:test + platforms: linux/amd64 + cache-from: type=gha + cache-to: type=gha,mode=max + + - name: prepare test.env + run: | + echo "ENV_SSH_PRIVATE_KEY_BASE64=$(cat $(pwd)/sftp-keys/id_rsa | base64 -w 0)" >> test.env + cat test.env + + - name: Run Backup Container + run: | + ./run_backup.sh backup /source \ No newline at end of file diff --git a/.gitignore b/.gitignore index 33e236f..e2d2d40 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,5 @@ # Project specific ignores -/test.env +/local.env /prod.env /log/* diff --git a/docker/entry.sh b/docker/entry.sh index 006883f..e3e4e80 100644 --- a/docker/entry.sh +++ b/docker/entry.sh @@ -21,6 +21,14 @@ if [[ "$DEBUG" == "1" ]];then exec bin/bash fi +echo "TODO: REMOVE ME: CI/CD TESTING" +echo "DEBUG: prepare_ssh.sh" +echo "cat known hosts" +cat /etc/ssh/ssh_known_hosts + +echo "cat ssh_config" +cat /etc/ssh/ssh_config + # argument mode if [[ $# -gt 0 ]]; then restic "${@}" diff --git a/docker/manager.sh b/docker/manager.sh index 4a71bda..2b76247 100644 --- a/docker/manager.sh +++ b/docker/manager.sh @@ -33,6 +33,10 @@ fi if [[ $status_backup == 0 ]]; then telegram.sh "Backup: Successful" + # todo: find a better way for assert the backup status in tests + echo "Backup: Successful" 2>&1 | tee ${backupLastLogfile} else telegram.sh "Backup: Failure" + # todo: find a better way for assert the backup status in tests + echo "Backup: Failure" 2>&1 | tee ${backupLastLogfile} fi diff --git a/docker/prepare.sh b/docker/prepare.sh index 2bd9bb6..4716ee6 100644 --- a/docker/prepare.sh +++ b/docker/prepare.sh @@ -56,5 +56,12 @@ mkdir -p /restore ## Backup specific parts not plugins prepare_ssh.sh +echo "DEBUG: prepare_ssh.sh" +echo "cat known hosts" +cat /etc/ssh/ssh_known_hosts + +echo "cat ssh_config" +cat /etc/ssh/ssh_config + ## Feature specific parts # currently nothing here diff --git a/docker/prepare_ssh.sh b/docker/prepare_ssh.sh index 4b3f97c..0703a58 100644 --- a/docker/prepare_ssh.sh +++ b/docker/prepare_ssh.sh @@ -13,5 +13,14 @@ cat < /etc/ssh/ssh_config Host storagebox Hostname $TARGET_DOMAIN User $TARGET_DOMAIN_USER + Port ${TARGET_DOMAIN_PORT:-22} IdentityFile /private_key EOL + + +echo "DEBUG: prepare_ssh.sh" +echo "cat known hosts" +cat /etc/ssh/ssh_known_hosts + +echo "cat ssh_config" +cat /etc/ssh/ssh_config \ No newline at end of file diff --git a/run_backup.sh b/run_backup.sh index 8b9fa9e..0f21d5e 100755 --- a/run_backup.sh +++ b/run_backup.sh @@ -18,7 +18,8 @@ if [[ "$#" -gt 0 ]]; then esac fi ### -source .env +### TODO: find a way to change env file for testing and prod usage +source test.env ### ### create restore folder at the same level as the script @@ -65,6 +66,7 @@ docker run ${RESTART_OPTION} --hostname backup \ $SOURCE_MOUNT \ -e "DEBUG"="${_DEBUG}" \ -e "TARGET_DOMAIN"="${ENV_TARGET_DOMAIN}" \ + -e "TARGET_DOMAIN_PORT"="${ENV_TARGET_DOMAIN_PORT}" \ -e "TARGET_DOMAIN_USER"="${ENV_TARGET_DOMAIN_USER}" \ -e "SSH_PRIVATE_KEY_BASE64"="${ENV_SSH_PRIVATE_KEY_BASE64}" \ -e "NC_URL"="${ENV_NC_URL}" \ @@ -80,4 +82,4 @@ docker run ${RESTART_OPTION} --hostname backup \ -e POSTGRES_HOST="${ENV_POSTGRES_HOST}" \ -e PROVISION_MODE="${ENV_PROVISION_MODE}" \ -e "CRON"="${ENV_CRON}" \ - devp1337/backup:latest "${@}" + devp1337/backup:test "${@}" diff --git a/test.env b/test.env new file mode 100644 index 0000000..95e0a4e --- /dev/null +++ b/test.env @@ -0,0 +1,23 @@ +SCRIPT_DATA_TO_BACKUP='none' +#SCRIPT_RESTORE_DATA_TO=/path/to/restore/data +#SCRIPT_LOG_PATH=/path/to/logfiles + +### restic (needed) +ENV_RESTIC_REPOSITORY_NAME=test +ENV_RESTIC_PASSWORD='test123' + +### telegram (needed) +ENV_TELEGRAM_TOKEN=123 +ENV_TELEGRAM_CHAT_ID=123 + +### CRON, defaults to 1am (needed) +ENV_CRON='0 1 * * *' + +### provision mode (needed) +### possible values: "postgres", "nextcloud", "none" +ENV_PROVISION_MODE="none" + +ENV_TARGET_DOMAIN_PORT=2222 +### storagebox settings (needed) +ENV_TARGET_DOMAIN=localhost +ENV_TARGET_DOMAIN_USER=test diff --git a/testing/preset_data.sql b/test/postgres/preset_data.sql similarity index 100% rename from testing/preset_data.sql rename to test/postgres/preset_data.sql diff --git a/testing/test.compose.yaml b/test/postgres/test.compose.yaml similarity index 100% rename from testing/test.compose.yaml rename to test/postgres/test.compose.yaml