Backup & Restore Commands
Environment Variables
Container environment variables are available to use in backup commands. If
you're using environment variables, wrap the command in ""
so that the
substitution happens properly.
The command used to create backups should write to stdout
.
For example, tar cf - -C /dir/change directory/to/backup
uses the -
flag to signify the intention to write to stdout
.
Another example is the popular command line tool mysqldump
, which is included in most official SQL database images. Given the command: mysqldump --all-databases -uuser -ppassword
, mysqldump
will write all the data to stdout
.
Similar to creating backups, the command used to restore a backup should read from stdin
. For example the counterpart to the mysqldump command would be: mysql -uuser -ppassword < /dev/stdin
This pattern allows for maximum flexibility when crafting backup commands. Users should rely on the tools provided for the software they are running, such as mysqldump
. These tools offer the most reliable way to snapshot or restore the necessary files, minimizing the risk of corruption, as they are designed specifically to facilitate backups.
It is also perfectly valid to invoke a script via the command. The script must write to stdout
for the create function and read from stdin
for the restore function. If the script is not reachable by traversing the container's path, ensure the absolute path to the script is included in the command.
Double Quotes and Variables
Double quotes allow the usage of environment variables when the script executes). See below for examples.
MySQL Example
Operation | Command |
---|---|
Backup | "mysqldump --all-databases -u root -p$MYSQL_ROOT_PASSWORD" |
Restore | "mysql -u root -p $MYSQL_ROOT_PASSWORD < /dev/stdin" |
PostgreSQL Example
Operation | Command |
---|---|
Backup | "PGPASSWORD=$POSTGRES_PASSWORD pg_dump -U $POSTGRES_USER --clean -Ft $POSTGRES_DB" |
Restore | "PASSWORD=$POSTGRES_PASSWORD pg_restore -U $POSTGRES_USER -Ft -d $POSTGRES_DB" |
MongoDB Example
Operation | Command |
---|---|
Backup | "mongodump --archive -u $USERNAME -p $PASSWORD" |
Restore | "mongorestore --archive -u $USERNAME -p $PASSWORD" |
Tar Example
Operation | Command |
---|---|
Backup | "tar czf /dev/stdout -C /var/lib/test backup" |
Restore | "tar xzf /dev/stdin -C /var/lib/test" |