How to Backup the Password Pusher Database
Postgres
To back up a PostgreSQL database running inside the Password Pusher Docker Compose setup, follow these steps:
Identify the Database Service Name
Open the docker-compose.yml
file in your Password Pusher setup and look for the PostgreSQL service name (commonly db or postgres).
Run the Backup Command
Use docker exec to run the pg_dump command inside the PostgreSQL container. Replace your_database_name and backup.sql with your actual database name and desired backup file name:
docker exec <container_name> pg_dump -U <db_user> your_database_name > backup.sql
<container_name>
: The name of your PostgreSQL container (you can get this from docker ps).<db_user>
: The database user (often postgres or as defined in your environment variables).your_database_name
: The name of the database (check your application’s configuration).
Example
If your Docker container is named password-pusher-db
and your database is named password_pusher
, the command might look like this:
docker exec password-pusher-db pg_dump -U postgres password_pusher > backup.sql
Store the Backup File
The backup.sql
file will be saved on your host system in the current working directory. You can move it to a secure location for long-term storage.
Verify the Backup
Test the backup file to ensure it works by restoring it to a test database:
docker exec -i <container_name> psql -U <db_user> -d test_database < backup.sql
Automate the Backup (Optional)
To automate backups, you can:
- Add a cron job or scheduled task to run the command regularly.
- Use a dedicated backup script.
MariaDB or MySQL
To back up a MySQL or MariaDB database running inside the Password Pusher Docker Compose setup, the process is similar to PostgreSQL but uses the mysqldump
tool instead of pg_dump
. Here’s how you can do it:
Identify the Database Service Name
- Open the
docker-compose.yml
file in your Password Pusher setup and look for the MySQL/MariaDB service name (commonly db or mysql).
Run the Backup Command
Use docker exec
to run the mysqldump
command inside the MySQL/MariaDB container. Replace placeholders as needed:
docker exec <container_name> mysqldump -u<db_user> -p<db_password> <database_name> > backup.sql
<container_name>
: The name of your MySQL/MariaDB container (you can get this with docker ps).<db_user>
: The database user (commonly root or as defined in your environment).<db_password>
: The password for the database user (often in .env or docker-compose.yml).<database_name>
: The name of your database (check your application configuration).
Example
If your container is named password-pusher-db
, the database user is root
, the password is password
, and the database name is password_pusher
, your command might look like this:
docker exec password-pusher-db mysqldump -uroot -ppassword password_pusher > backup.sql
Note: There is no space between -u and the username or -p and the password.
Store the Backup File
The backup.sql
file will be saved on your host system in the current working directory. Move it to a secure location for storage.
Verify the Backup
Test the backup file to ensure it works by restoring it to a test database. Run:
docker exec -i <container_name> mysql -u<db_user> -p<db_password> <test_database_name> < backup.sql
Automate the Backup (Optional)
To automate backups:
- Add a cron job or scheduled task to run the backup command regularly.
- Use a script to timestamp and store backups in a structured way.
Script Example
Here’s a simple backup script you can use:
#!/bin/bash
CONTAINER_NAME="password-pusher-db"
DB_USER="root"
DB_PASSWORD="password"
DATABASE_NAME="password_pusher"
BACKUP_FILE="backup_$(date +%Y%m%d%H%M%S).sql"
docker exec $CONTAINER_NAME mysqldump -u$DB_USER -p$DB_PASSWORD $DATABASE_NAME > $BACKUP_FILE
echo "Backup completed: $BACKUP_FILE"
Save it as backup.sh
, make it executable (chmod +x backup.sh
), and run it whenever you need to create a backup.