Cleanup¶
Get the namespaces and compare the namespaces with database users and
Get namespaces:
kubectl get namespaces -o=jsonpath='{.items[*].metadata.name}'
# With new lines
kubectl get namespaces -o=jsonpath='{range .items[*]}{.metadata.name}{"\n"}{end}'
# Assuming the lists are defined as follows:
list1=("Alice" "Bob" "Charlie")
list2=("Bob" "Daniel" "Eve")
# Unique to list1
grep --invert-match --fixed-strings --file=<(printf '%s\n' "${list2[@]}" | sort) <(printf '%s\n' "${list1[@]}" | sort)
# Unique to list2
grep --invert-match --fixed-strings --file=<(printf '%s\n' "${list1[@]}" | sort) <(printf '%s\n' "${list2[@]}" | sort)
In this command:
- --invert-match (or -v) inverts the match, so grep selects lines that do not match.
- --fixed-strings (or -F) treats the patterns as fixed strings, not regular expressions. This is important if your data might contain characters that are special in regex (like . or *).
- --file= (or -f) obtains patterns from the specified file, in this case a process substitution that sorts the other list.
This will output the names that are unique to each list, respectively.
Instances¶
Get al list of database instances
doctl databases list
Database users¶
Postgres: "b468657c-85f7-4df2-9dce-a85a0d9842c9" Mysql: "51ecba51-6594-43a8-a29a-47376be22245"
doctl databases user list "51ecba51-6594-43a8-a29a-47376be22245"
NAMESPACES=`kubectl get namespaces -o=jsonpath='{.items[*].metadata.name}'`
USERS=`doctl databases user list $DATABASE --format Name --no-header`
USERS_DELETE=`grep --invert-match --fixed-strings --file=<(printf '%s\n' $NAMESPACES | sort) <(printf '%s\n' $USERS | sort) | grep -v 'doadmin' | grep -v 'docontrol' | grep -v 'stellarhosted' | grep -v 'backup'`
echo $USERS_DELETE
This will give a list of users to be deleted from the database
echo $USERS_DELETE | xargs --max-args 1 --open-tty sh -c 'echo "Deleting database user: $0" && doctl databases user delete <database> $0'
Databases¶
Get all the databases not starting with a namespace name
doctl databases db list "b468657c-85f7-4df2-9dce-a85a0d9842c9"
NAMESPACES=`kubectl get namespaces -o=jsonpath='{.items[*].metadata.name}'`
DATABASES=`doctl databases db list b468657c-85f7-4df2-9dce-a85a0d9842c9 --no-header`
DATABASES_DELETE=`grep --invert-match --extended-regexp "^($(printf "%s\n" $NAMESPACES | paste --serial --delimiters '|'))" <<< $DATABASES`
echo $DATABASES_DELETE
This will give a list of databases to be deleted from the database instance
echo $DATABASES_DELETE | xargs --max-args 1 --open-tty sh -c 'echo "Deleting database: $0" && doctl databases db delete b468657c-85f7-4df2-9dce-a85a0d9842c9 $0'