1.先在容器内写个脚本导出数据库sql文件
root@b412c9bd111b:~# vi dockersqlbak.sh
#!/bin/bash
Time=`date +%Y-%m-%d`
db_user=root
db_passwd=123456
/usr/bin/mysqldump -u${db_user} -p${db_passwd} --opt ccnet_db > /root/sql/ccnet_db-${Time}.sql
/usr/bin/mysqldump -u${db_user} -p${db_passwd} --opt seahub_db > /root/sql/seahub_db-${Time}.sql
/usr/bin/mysqldump -u${db_user} -p${db_passwd} --opt seafile_db > /root/sql/seafile_db-${Time}.sql
2.授权容器内的脚本
root@b412c9bd111b:~# chmod +x dockersqlbak.sh
3.测试脚本成功
root@b412c9bd111b:~# ./dockersqlbak.sh
root@b412c9bd111b:/opt/seafile# ll /root/sql/
total 240
drwxr-xr-x. 2 root root 102 Dec 16 09:59 ./
drwx------. 4 root root 4096 Dec 16 09:54 ../
-rw-r--r--. 1 root root 16714 Dec 16 10:10 ccnet_db-2020-12-16.sql
-rw-r--r--. 1 root root 44673 Dec 16 10:10 seafile_db-2020-12-16.sql
-rw-r--r--. 1 root root 173508 Dec 16 10:10 seahub_db-2020-12-16.sql
4.宿主机写个导出docker容器内sql文件脚本
[root@lixian sql]# cat /Seafile/backup_sql.sh
#/bin/bash
Time=`date +%Y-%m-%d`
docker exec -d b412c9bd111b /bin/bash -c 'find /root/sql -type f -name "*.sql" -mtime +5 |xargs rm -f && /root/dockersqlbak.sh'
sleep 3
docker cp seafile:/root/sql/ccnet_db-${Time}.sql /Seafile/sql
docker cp seafile:/root/sql/seafile_db-${Time}.sql /Seafile/sql
docker cp seafile:/root/sql/seahub_db-${Time}.sql /Seafile/sql
5.开启crontab定时任务
[root@lixian sql]# crontab -l
0 3 * * * /usr/bin/bash /Seafile/backup_sql.sh > /dev/null 2>&1