This file regroups some useful combinations of different commands to efficiently manage your Scaleway resources.
Do not hesitate to open a PR and share your favorite recipes.
General
Retrieve specific field
Code
## Retrieve all available instance type names# Using jqscw -o json instance server-type list | jq -r '.[].name'# Using CLI templatesscw instance server-type list -o template="{{ .Name }}"
Filter output using jq
Code
# Retrieve all available instance type with GPUsscw -o json instance server-type list | jq '.[] | select (.gpu > 0)'```
Parallelize actions using xargs
Code
## Reboot all listed servers at the same time, 8 max concurrent actions# Using jqscw -o json instance server list | jq -r '.[].id' | xargs -L1 -P8 scw instance server reboot# Using CLI templatesscw instance server list -o template="{{ .ID }}" | xargs -L1 -P8 scw instance server reboot
Create arguments for a cli command and use it in xargs
Code
## List private-nics of all listed servers# Using jqscw -o json instance server list | jq -r '.[] | "server-id=\(.id)"' | xargs -L1 scw instance private-nic list# Using CLI templatesscw instance server list -o template="server-id={{ .ID }}" | xargs -L1 scw instance private-nic list
Instance
Start/Stop a group of servers based on a tag
Code
# Start all servers with tag stagingscw -o json instance server list tags.0=staging | jq -r '.[].id' | xargs scw instance server start -w# Stop all servers with tag stagingscw -o json instance server list tags.0=staging | jq -r '.[].id' | xargs scw instance server stop -w
Servers and private networks
Code
# Add all listed servers to a given private networkscw -o json instance server list tags.0=staging | jq '.[].id' | xargs -t -I{} scw instance private-nic create private-network-id=<pn-id> server-id={}# List all servers in a specific private networkscw instance server list -ojson | jq 'map(select (.private_nics | map(select (.private_network_id == "<pn-id>")) | length == 1))'# List all servers not in a specific private networkscw instance server list -ojson | jq 'map(select (.private_nics | map(select (.private_network_id == "<pn-id>")) | length == 0))'
Action on servers across multiple zones
Code
## Reboot all servers across all zones, 8 server at a time# Using jqscw -o json instance server list zone=all | jq -r '.[] | "\(.id) zone=\(.zone)"' | xargs -P8 -L1 scw instance server reboot# Using CLI templatesscw instance server list zone=all -o template="{{.ID}} zone={{.Zone}}" | xargs -P8 -L1 scw instance server reboot
Database
Filter backups by date
Code
# Get all backup older than 7 daysscw rdb backup list -ojson | jq --arg d "$(date -d "7 days ago" --utc --iso-8601=ns)" '.[] | select (.created_at < $d)'
Restore a backup from another Database Instance (even with different version)
You can restore a backup from one Database Instance to another, even if they have different PostgreSQL/MySQL versions (e.g., PostgreSQL-15 to PostgreSQL-16). The restore operation works within the same region.
Code
# Step 1: Create a backup from the source instancescw rdb backup create instance-id=<source-instance-id> database-name=<db-name> name=cross-instance-backup region=<region> -w# Step 2: Get the backup IDBACKUP_ID=$(scw rdb backup list instance-id=<source-instance-id> region=<region> -ojson | jq -r '.[0].id')# Step 3: Create the target database on the destination instance (if it doesn't exist)scw rdb database create instance-id=<target-instance-id> name=<db-name> region=<region># Step 4: Restore the backup to the target instancescw rdb backup restore $BACKUP_ID instance-id=<target-instance-id> region=<region> -w# Example: Restore from PostgreSQL-15 to PostgreSQL-16SOURCE_ID="325fd68a-a286-4f5c-b56b-3b8d66fcd13d" # PG-15 instanceTARGET_ID="70644724-60c9-411c-a3e2-5276f1cefff1" # PG-16 instancescw rdb backup create instance-id=$SOURCE_ID database-name=mydb name=upgrade-backup region=fr-par -wBACKUP_ID=$(scw rdb backup list instance-id=$SOURCE_ID region=fr-par -ojson | jq -r '.[0].id')scw rdb database create instance-id=$TARGET_ID name=mydb region=fr-parscw rdb backup restore $BACKUP_ID instance-id=$TARGET_ID region=fr-par -w
Note: This method only works within the same region. For cross-region migrations, see the "Migrate a managed database to another region" section.
Migrate a managed database to another region
Important:scw rdb backup restore cannot restore backups across different regions (API limitation). Logical backups and snapshots can only be restored within the same region. For cross-region migration, you must export the backup, download it, and manually restore it using database client tools (psql/mysql).
Code
# Method 1: Manual migration via backup export (for cross-region migration)# Step 1: Create a backup of the source databasescw rdb backup create instance-id=<source-instance-id> database-name=<db-name> name=migration-backup region=<source-region> -w# Step 2: Export and download the backupBACKUP_ID=$(scw rdb backup list instance-id=<source-instance-id> region=<source-region> -ojson | jq -r '.[0].id')scw rdb backup export $BACKUP_ID region=<source-region> -wscw rdb backup download $BACKUP_ID region=<source-region> output=./backup.sql# Step 3: Create a new Database Instance in the target regionscw rdb instance create name=<new-instance-name> engine=<engine-version> user-name=<username> password=<password> node-type=<node-type> region=<target-region> -w# Step 4: Get connection details for the new instanceNEW_INSTANCE_ID=$(scw rdb instance list name=<new-instance-name> region=<target-region> -ojson | jq -r '.[0].id')ENDPOINT=$(scw rdb instance get $NEW_INSTANCE_ID region=<target-region> -ojson | jq -r '.endpoints[0].ip + ":" + (.endpoints[0].port | tostring)')# Step 5: Manually restore the backup using database client# For PostgreSQL:psql -h <endpoint-host> -p <endpoint-port> -U <username> -d postgres -f backup.sql# For MySQL:mysql -h <endpoint-host> -P <endpoint-port> -u <username> -p < backup.sql# Method 2: Same-region backup restore (for migration within the same region)# Step 1: Create a backupscw rdb backup create instance-id=<source-instance-id> database-name=<db-name> name=same-region-backup region=<region> -w# Step 2: Create a database in the target instancescw rdb database create instance-id=<target-instance-id> name=<db-name> region=<region># Step 3: Restore the backupBACKUP_ID=$(scw rdb backup list instance-id=<source-instance-id> region=<region> -ojson | jq -r '.[0].id')scw rdb backup restore $BACKUP_ID instance-id=<target-instance-id> region=<region> -w
Configure password storage for rdb connect
The scw rdb instance connect command can automatically use stored credentials to avoid typing passwords manually. Here's how to configure it for PostgreSQL and MySQL.
PostgreSQL - Using .pgpass file
Create a password file to store your connection credentials securely:
Code
# Linux/macOS: Create ~/.pgpasscat > ~/.pgpass << 'EOF'# Format: hostname:port:database:username:password51.159.25.206:13917:rdb:myuser:mypassword# You can use * as wildcard*:*:*:myuser:mypasswordEOFchmod 600 ~/.pgpass# Windows: Create %APPDATA%\postgresql\pgpass.conf# Same format as above
MySQL provides mysql_config_editor for secure, obfuscated password storage:
Code
# Configure credentials for a login pathmysql_config_editor set --login-path=scw \ --host=195.154.69.163 \ --port=12210 \ --user=myuser \ --password# You'll be prompted to enter the password securely# Verify configuration (password will be masked)mysql_config_editor print --login-path=scw# Connect using the login pathmysql --login-path=scw --database=rdb
The credentials are stored in ~/.mylogin.cnf (Linux/macOS) or %APPDATA%\MySQL\.mylogin.cnf (Windows).
Alternative: You can also use ~/.my.cnf for plain-text storage (less secure):