Skip to content

Instantly share code, notes, and snippets.

@excla1mmm
Last active February 11, 2025 10:28
Show Gist options
  • Select an option

  • Save excla1mmm/c9891306b459cac0c7ea3c785ab0936e to your computer and use it in GitHub Desktop.

Select an option

Save excla1mmm/c9891306b459cac0c7ea3c785ab0936e to your computer and use it in GitHub Desktop.
Script for transfer data to the new MikoPBX station using Rsync
# Asking for connection details
echo -n "Enter OLD MikoPBX ip address or host name: "
read PBX_IP
echo -n "Enter ssh root username [root]: "
read PBX_USER
PBX_USER=${PBX_USER:-root}
echo -n "Enter ssh port [22]: "
read PBX_PORT
PBX_PORT=${PBX_PORT:-22}
# Configuration
SSH_PORT="$PBX_PORT"
PBX_HOST="$PBX_USER@$PBX_IP"
CONF_DB_FILE='/cf/conf/mikopbx.db'
STORAGE_PBX_DIR='/storage/usbdisk1/mikopbx'
SSH_KEY_PATH="$HOME/.ssh/id_rsa"
SYNC_APP='rsync'
# Check if rsync is available
type "$SYNC_APP" > /dev/null 2> /dev/null
if [ "$?" = '1' ];then
SYNC_APP='scp'
echo "rsync not found, using scp instead"
fi
# SSH key setup
setup_ssh_environment() {
mkdir -p "$HOME/.ssh"
chmod 700 "$HOME/.ssh"
}
generate_ssh_key() {
echo "Generating new RSA SSH key..."
setup_ssh_environment
# Remove existing keys if they exist
rm -f "$SSH_KEY_PATH" "${SSH_KEY_PATH}.pub"
# Generate temporary keys using dropbearkey
local temp_key="/tmp/dropbear_temp"
rm -f "$temp_key" "${temp_key}.pub"
# Generate key using dropbearkey
dropbearkey -t rsa -f "$temp_key" 2>/dev/null
if [ $? -ne 0 ]; then
echo "Failed to generate private key"
exit 1
fi
# Generate public key without comment
dropbearkey -y -f "$temp_key" | \
grep "^ssh-rsa" | \
cut -d' ' -f1,2 > "${SSH_KEY_PATH}.pub"
if [ ! -s "${SSH_KEY_PATH}.pub" ]; then
echo "Failed to extract public key"
rm -f "$temp_key" "${temp_key}.pub" "$SSH_KEY_PATH" "${SSH_KEY_PATH}.pub"
exit 1
fi
# Convert private key to OpenSSH format
dropbearconvert dropbear openssh "$temp_key" "$SSH_KEY_PATH"
if [ $? -ne 0 ]; then
echo "Failed to convert private key format"
rm -f "$temp_key" "${temp_key}.pub" "$SSH_KEY_PATH" "${SSH_KEY_PATH}.pub"
exit 1
fi
# Clean up temporary files
rm -f "$temp_key" "${temp_key}.pub"
# Set correct permissions
chmod 600 "$SSH_KEY_PATH"
chmod 644 "${SSH_KEY_PATH}.pub"
echo -e "\nHere's your public SSH key. Please copy it and add it to /root/.ssh/authorized_keys on the old PBX:\n"
cat "${SSH_KEY_PATH}.pub"
echo -e "\nPress any key after you've added the key to continue..."
read -n 1 -s
}
check_existing_keys() {
if [ -f "$SSH_KEY_PATH" ]; then
echo "Found existing RSA key at $SSH_KEY_PATH"
echo "Would you like to use this key? (Y/n)"
read -r use_existing
if [[ ! "$use_existing" =~ ^([nN][oO]|[nN])+$ ]]; then
echo -e "\nHere's your public SSH key. If you haven't added it yet to the old PBX, please copy it:"
cat "${SSH_KEY_PATH}.pub"
echo -e "\nPress any key after you've verified the key is added..."
read -n 1 -s
return 0
fi
fi
return 1
}
test_ssh_connection() {
echo "Testing SSH connection..."
ssh -o StrictHostKeyChecking=accept-new \
-o PasswordAuthentication=no \
-o BatchMode=yes \
-p "$SSH_PORT" \
"$PBX_HOST" exit 2>/dev/null
return $?
}
# Main SSH key setup
echo "Checking SSH key configuration..."
if ! check_existing_keys; then
echo "No suitable SSH key found. Generating new key..."
generate_ssh_key
fi
# Test SSH connection
test_ssh_connection
if [ $? -ne 0 ]; then
echo "SSH connection failed. Please check your configuration and try again."
exit 1
fi
echo "SSH connection successful. Starting data transfer..."
# Data transfer process
echo "Creating necessary directories..."
mkdir -p "$STORAGE_PBX_DIR/tmp"
mkdir -p "$STORAGE_PBX_DIR/astlogs/asterisk"
mkdir -p "$STORAGE_PBX_DIR/media"
mkdir -p "$STORAGE_PBX_DIR/custom_modules"
mkdir -p "$STORAGE_PBX_DIR/astspool/monitor"
# Main database dump and transfer
echo "Transferring main database..."
ssh "$PBX_HOST" -p "$SSH_PORT" "sqlite3 $CONF_DB_FILE .dump > $STORAGE_PBX_DIR/tmp/mikopbx.db.dmp"
"$SYNC_APP" "$PBX_HOST":"$STORAGE_PBX_DIR/tmp/mikopbx.db.dmp" "$STORAGE_PBX_DIR/tmp/mikopbx.db.dmp"
sqlite3 "$STORAGE_PBX_DIR/tmp/mikopbx.db" < "$STORAGE_PBX_DIR/tmp/mikopbx.db.dmp"
mv "$STORAGE_PBX_DIR/tmp/mikopbx.db" "$CONF_DB_FILE"
rm -rf "$STORAGE_PBX_DIR/tmp/mikopbx.db"
ssh "$PBX_HOST" -p "$SSH_PORT" "rm -rf $STORAGE_PBX_DIR/tmp/mikopbx.db"
# Disable providers
echo "Disabling providers..."
sqlite3 "$CONF_DB_FILE" "UPDATE m_Sip SET disabled='1' WHERE type='friend'"
# Copy media files
echo "Copying media files..."
"$SYNC_APP" -r "$PBX_HOST":"$STORAGE_PBX_DIR"/media/* "$STORAGE_PBX_DIR"/media
# Copy custom modules
echo "Copying custom modules..."
"$SYNC_APP" -r "$PBX_HOST":"$STORAGE_PBX_DIR"/custom_modules/* "$STORAGE_PBX_DIR"/custom_modules
# CDR database transfer
echo "Transferring call history..."
ssh "$PBX_HOST" -p "$SSH_PORT" \
"sqlite3 $STORAGE_PBX_DIR/astlogs/asterisk/cdr.db .dump > \
$STORAGE_PBX_DIR/astlogs/asterisk/cdr.db.dmp"
"$SYNC_APP" -r \
"$PBX_HOST":"$STORAGE_PBX_DIR"/astlogs/asterisk/cdr.db.dmp \
"$STORAGE_PBX_DIR"/astlogs/asterisk/cdr.db.dmp
sqlite3 "$STORAGE_PBX_DIR"/astlogs/asterisk/cdrdb.tmp < \
"$STORAGE_PBX_DIR"/astlogs/asterisk/cdr.db.dmp
rm -rf "$STORAGE_PBX_DIR"/astlogs/asterisk/cdr.db*
mv "$STORAGE_PBX_DIR"/astlogs/asterisk/cdrdb.tmp "$STORAGE_PBX_DIR"/astlogs/asterisk/cdr.db
rm -rf "$STORAGE_PBX_DIR"/astlogs/asterisk/cdr.db.dmp
ssh "$PBX_HOST" -p "$SSH_PORT" "rm -rf $STORAGE_PBX_DIR/astlogs/asterisk/cdr.db.dmp"
# Update database structure
echo "Updating database structure..."
php -r 'require_once "Globals.php"; \
use MikoPBX\Core\System\Upgrade\UpdateDatabase; \
$dbUpdater = new UpdateDatabase(); \
$dbUpdater->updateDatabaseStructure();'
# Copy call recordings with correct path
echo "Copying call recordings..."
"$SYNC_APP" -avzh --perms --chmod=u+rwx,g+rwx,o+rx \
"$PBX_HOST":"$STORAGE_PBX_DIR/astspool/monitor/" \
"$STORAGE_PBX_DIR/astspool/monitor/"
# Verify recordings transfer
echo "Verifying recordings transfer..."
if [ -n "$(ls -A $STORAGE_PBX_DIR/astspool/monitor/)" ]; then
echo "Recordings transferred successfully"
else
echo "Warning: Monitor directory is empty after transfer! Please check the source path on the old PBX."
fi
echo "Data transfer completed!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment