docker-backup.sh 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #!/bin/bash
  2. OPTIND=1
  3. VOLUMES=''
  4. ALLFLAG=''
  5. OUTDIR='./'
  6. NOAUTORESTART=''
  7. # get arguments
  8. while getopts "h?v:o:na" opt; do
  9. case "$opt" in
  10. h|\?)
  11. echo "
  12. docker-backup.sh stops all running containers, exports them and tars them to the
  13. current directory, then restarts the previously running containers in the reverse
  14. order of which they were stopped.
  15. This script assumes it is being run as root, or the current user is in the docker group.
  16. Usage: "$0 "[args]
  17. -a Backup all containers, not just running containers. Only running containers will be
  18. restarted after backup (unless otherwise specified by -n)
  19. -h Print this help message.
  20. -n Don't restart containers after backup
  21. -o OUTDIR Backups are sent to OUTDIR, rather than the current directory
  22. -v VOLUME (-v SECONDVOLUME -v THIRDVOLUME...)]
  23. Include volume directories with your backup. These are made once the container is
  24. stopped to avoid corruption.
  25. "
  26. exit 0
  27. ;;
  28. v) VOLUMES=$VOLUMES\;$OPTARG
  29. ;;
  30. o) OUTDIR=$OPTARG
  31. ;;
  32. n) NOAUTORESTART='1'
  33. ;;
  34. a) ALLFLAG='-a'
  35. esac
  36. done
  37. # Cleanup from getopts
  38. shift $((OPTIND-1))
  39. [ "$1" = "--" ] && shift
  40. # Check if outdir exists, else (try to) create it
  41. if [ ! -d $OUTDIR ]
  42. then
  43. mkdir $OUTDIR
  44. fi
  45. # Check or write access
  46. if [ ! -w $OUTDIR ]
  47. then
  48. echo $OUTDIR is not writable. Exiting...
  49. exit 1
  50. fi
  51. # Remove first semicolon from volumes list
  52. VOLUMES=$(echo $VOLUMES | sed 's/^.//')
  53. # Get requested list of containers
  54. CONTAINERS=$(docker ps $ALLFLAG --format '{{.Names}}' | sed ':a;N;$!ba;s/\n/ /g')
  55. RUNNINGCONTAINERS=''
  56. if [ ! $NOAUTORESTART ]
  57. then
  58. RUNNINGCONTAINERS=$(docker ps --format '{{.Names}}' | sed ':a;N;$!ba;s/\n/ /g' | awk '{for(i=NF;i>0;--i)printf "%s%s",$i,(i>1?OFS:ORS)}')
  59. fi
  60. # Stop all containers
  61. echo Stopping containers...
  62. for i in $CONTAINERS
  63. do
  64. docker stop $i
  65. done
  66. # Wait for all to be stopped
  67. wait
  68. # Export each container
  69. echo Exporting containers...
  70. for i in $CONTAINERS
  71. do
  72. docker export $i > $OUTDIR/$i.tar && echo "Exported "$i &
  73. done
  74. wait
  75. # Get specified volumes delimited by semicolons
  76. if [ $VOLUMES ]
  77. then
  78. echo Backing up volumes...
  79. OLDIFS=$IFS
  80. IFS=';'
  81. for i in $VOLUMES
  82. do
  83. tar cf $OUTDIR/$(basename $i).tar $i && echo "Backed up "$i &
  84. done
  85. IFS=$OLDIFS
  86. fi
  87. wait
  88. # Restart previously running containers (if any were given or it was requested)
  89. if [ ! $NOAUTORESTART ]
  90. then
  91. echo Restarting previously running containers...
  92. for i in $RUNNINGCONTAINERS
  93. do
  94. docker start $i &
  95. done
  96. wait
  97. fi