Automate Backup database SQL Server in docker container.

Hi, everyone. This is first my post in dev.to and my first time for write article in second language. today I want to share some trick for backup your database from docker container. let’s begin… 🙂
Note I assume your already running container.


This content originally appeared on DEV Community and was authored by Pobx

Hi, everyone. This is first my post in dev.to and my first time for write article in second language. today I want to share some trick for backup your database from docker container. let's begin... :)
Note I assume your already running container.

1. Write T-SQL for backup your database.

For example I use TutorialDB database for demonstrate purpose. Your can create same database click Create a database

PRINT "Start Backup process...";
DECLARE @MyFileName varchar(200)
SELECT @MyFileName=N'/tmp/Backup_' + convert(nvarchar(20),GetDate(),112)+'_'+convert(nvarchar(20),GetDate(),108)+ '.bak'
BACKUP DATABASE [TutorialDB] TO DISK=@MyFileName
PRINT "Finished backup process...";

Then save above T-SQL to backup-database.sql

2. Copy backup-database.sql file to your container.

For example I just copy it to tmp directory. You can copy it to any your directory.
image
Then go inside container and see in tmp directory as below
image

3. Update packages list to new version.

Go inside your container by docker exec cli as below
image
Note You need to use --user root (option) for update package list.
Now, update package list by apt-get update -y as below
image
Then you will installing 2 package such as vim and cron as below
image
image

4. Add sqlcmd cli to crontab by cron -e cli as below

image
For example above configuration is running backup-database.sql every 1 minute. Keep in mind it's just example for show how it work. Next you want to restart crontab for start your schedule job like this
image

5. see log by tail -f /tmp/daily-backup.log & .bak files.

image
image

Until we meet again. Thank you


This content originally appeared on DEV Community and was authored by Pobx


Print Share Comment Cite Upload Translate Updates
APA

Pobx | Sciencx (2021-05-17T13:17:41+00:00) Automate Backup database SQL Server in docker container.. Retrieved from https://www.scien.cx/2021/05/17/automate-backup-database-sql-server-in-docker-container/

MLA
" » Automate Backup database SQL Server in docker container.." Pobx | Sciencx - Monday May 17, 2021, https://www.scien.cx/2021/05/17/automate-backup-database-sql-server-in-docker-container/
HARVARD
Pobx | Sciencx Monday May 17, 2021 » Automate Backup database SQL Server in docker container.., viewed ,<https://www.scien.cx/2021/05/17/automate-backup-database-sql-server-in-docker-container/>
VANCOUVER
Pobx | Sciencx - » Automate Backup database SQL Server in docker container.. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/05/17/automate-backup-database-sql-server-in-docker-container/
CHICAGO
" » Automate Backup database SQL Server in docker container.." Pobx | Sciencx - Accessed . https://www.scien.cx/2021/05/17/automate-backup-database-sql-server-in-docker-container/
IEEE
" » Automate Backup database SQL Server in docker container.." Pobx | Sciencx [Online]. Available: https://www.scien.cx/2021/05/17/automate-backup-database-sql-server-in-docker-container/. [Accessed: ]
rf:citation
» Automate Backup database SQL Server in docker container. | Pobx | Sciencx | https://www.scien.cx/2021/05/17/automate-backup-database-sql-server-in-docker-container/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.