161 lines
5.3 KiB
Bash
Executable File
161 lines
5.3 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
# Setup script for Asustor backup configuration
|
|
# This script helps configure SSH keys and test connectivity
|
|
|
|
set -e
|
|
|
|
ASUSTOR_HOST="root@asustor-lan1"
|
|
SSH_KEY_PATH="/root/.ssh/id_rsa"
|
|
CONTAINER_NAME="side-agent"
|
|
|
|
echo "=== Asustor Backup Setup Script ==="
|
|
echo "This script will help you configure SSH access to $ASUSTOR_HOST"
|
|
echo
|
|
|
|
# Function to test SSH connectivity
|
|
test_ssh_connection() {
|
|
echo "Testing SSH connection to $ASUSTOR_HOST..."
|
|
if docker exec $CONTAINER_NAME ssh -o ConnectTimeout=10 -o BatchMode=yes $ASUSTOR_HOST "echo 'SSH connection successful'" 2>/dev/null; then
|
|
echo "✓ SSH connection to $ASUSTOR_HOST is working"
|
|
return 0
|
|
else
|
|
echo "✗ SSH connection to $ASUSTOR_HOST failed"
|
|
return 1
|
|
fi
|
|
}
|
|
|
|
# Function to test rsync connectivity
|
|
test_rsync_connection() {
|
|
echo "Testing rsync connectivity to $ASUSTOR_HOST..."
|
|
if docker exec $CONTAINER_NAME rsync --dry-run -v $ASUSTOR_HOST:/volume1/data/ /tmp/test_rsync 2>/dev/null >/dev/null; then
|
|
echo "✓ Rsync connection to $ASUSTOR_HOST is working"
|
|
return 0
|
|
else
|
|
echo "✗ Rsync connection to $ASUSTOR_HOST failed"
|
|
return 1
|
|
fi
|
|
}
|
|
|
|
# Function to create SSH key inside container
|
|
create_ssh_key() {
|
|
echo "Creating SSH key inside the container..."
|
|
docker exec -it $CONTAINER_NAME bash -c "
|
|
mkdir -p /root/.ssh
|
|
chmod 700 /root/.ssh
|
|
if [ ! -f $SSH_KEY_PATH ]; then
|
|
ssh-keygen -t rsa -b 4096 -f $SSH_KEY_PATH -N '' -C 'side-agent-backup@$(hostname)'
|
|
echo 'SSH key created successfully'
|
|
else
|
|
echo 'SSH key already exists'
|
|
fi
|
|
echo
|
|
echo 'Public key content:'
|
|
echo '==================='
|
|
cat ${SSH_KEY_PATH}.pub
|
|
echo '==================='
|
|
echo
|
|
echo 'Please copy the above public key and add it to the authorized_keys file on your Asustor NAS'
|
|
echo 'You can do this by:'
|
|
echo '1. SSH to your Asustor: ssh root@asustor-lan1'
|
|
echo '2. Edit authorized_keys: nano ~/.ssh/authorized_keys'
|
|
echo '3. Paste the public key on a new line'
|
|
echo '4. Save and exit'
|
|
echo
|
|
"
|
|
}
|
|
|
|
# Function to test backup directories
|
|
test_backup_directories() {
|
|
echo "Testing backup directory structure on $ASUSTOR_HOST..."
|
|
docker exec $CONTAINER_NAME ssh $ASUSTOR_HOST "
|
|
mkdir -p /volume1/data/rsyncs/myansible
|
|
mkdir -p /volume1/data/rsyncs/hosts
|
|
mkdir -p /volume1/data/rsyncs/valuable_scripts
|
|
mkdir -p /volume1/data/google-drive-sync/backups_n_configs/rsyncs
|
|
echo 'Backup directories created/verified on Asustor'
|
|
" 2>/dev/null || {
|
|
echo "✗ Failed to create backup directories on Asustor"
|
|
return 1
|
|
}
|
|
echo "✓ Backup directories are ready on Asustor"
|
|
}
|
|
|
|
# Function to run backup tests
|
|
run_backup_tests() {
|
|
echo "Running backup scheduler tests..."
|
|
echo "Testing individual backup functions..."
|
|
|
|
echo "1. Testing hosts file sync..."
|
|
docker exec $CONTAINER_NAME python3 /app/backup_scheduler.py --test-hosts
|
|
|
|
echo "2. Testing myansible sync..."
|
|
docker exec $CONTAINER_NAME python3 /app/backup_scheduler.py --test-myansible
|
|
|
|
echo "3. Testing valuable scripts sync..."
|
|
docker exec $CONTAINER_NAME python3 /app/backup_scheduler.py --test-scripts
|
|
|
|
echo "4. Testing mydocker backup (this may take a while)..."
|
|
docker exec $CONTAINER_NAME python3 /app/backup_scheduler.py --test-mydocker
|
|
|
|
echo "All backup tests completed!"
|
|
}
|
|
|
|
# Main setup flow
|
|
main() {
|
|
echo "Checking if $CONTAINER_NAME container is running..."
|
|
if ! docker ps | grep -q $CONTAINER_NAME; then
|
|
echo "✗ Container $CONTAINER_NAME is not running"
|
|
echo "Please start the container first: docker-compose up -d side-agent"
|
|
exit 1
|
|
fi
|
|
echo "✓ Container $CONTAINER_NAME is running"
|
|
echo
|
|
|
|
# Test initial connectivity
|
|
if test_ssh_connection; then
|
|
echo "SSH is already configured!"
|
|
else
|
|
echo "SSH needs to be configured..."
|
|
create_ssh_key
|
|
echo
|
|
echo "After adding the public key to your Asustor, press Enter to continue..."
|
|
read -r
|
|
|
|
if test_ssh_connection; then
|
|
echo "✓ SSH configuration successful!"
|
|
else
|
|
echo "✗ SSH configuration failed. Please check your setup."
|
|
exit 1
|
|
fi
|
|
fi
|
|
|
|
echo
|
|
test_rsync_connection
|
|
echo
|
|
test_backup_directories
|
|
echo
|
|
|
|
echo "Would you like to run backup tests? (y/n)"
|
|
read -r response
|
|
if [[ "$response" =~ ^[Yy]$ ]]; then
|
|
run_backup_tests
|
|
fi
|
|
|
|
echo
|
|
echo "=== Setup Complete ==="
|
|
echo "Your backup scheduler is now configured and ready!"
|
|
echo "The backup scheduler will run automatically with the following schedule:"
|
|
echo "- Mydocker backup with rotation: Daily at 7:33 AM"
|
|
echo "- Myansible sync: Daily at 1:10 AM"
|
|
echo "- Myansible to Google Drive: Weekly on Sunday at 1:15 AM"
|
|
echo "- Hosts file sync: Daily at 1:15 AM"
|
|
echo "- Valuable scripts sync: Daily at 1:15 AM"
|
|
echo
|
|
echo "You can check logs with: docker logs side-agent"
|
|
echo "You can test individual backups with: docker exec side-agent python3 /app/backup_scheduler.py --test-all"
|
|
}
|
|
|
|
# Run main function
|
|
main "$@"
|