利用tar點樣把日期變成檔案名?

打算每星期backup一次某個/幾個directory, 點樣可以利用tar 把當天日期變成檔案名?

例如 :
當天是 2012年3月1日

打算backup /var/www/html 及 /lib/mysql

輸出的檔案要係 = "backup20120301.tar.gz"

要響tar 加d乜option?

麻煩哂!

回復 1# soccerism

tar cvf backup`date +"%Y%m%d"`.tar *

TOP

let me try ha sin, thanks ar!!

TOP

本帖最後由 soccerism 於 2013-9-20 17:55 編輯

隔左起過年半更新下先, 可能有師兄會覺得有用
以今日(20/09/2013)進行backup為例, 想用以下格式儲成檔名有日期的tar.gz
最終檔名: file-20130920.tar.gz
指令如下:

tar zcvf "file-$(date +%Y%m%d).tar.gz" /directory_name

TOP

本帖最後由 toylet 於 2013-9-21 01:08 編輯

小人的 backup script....
  1. #!/bin/bash
  2. #
  3. # for Fedora
  4. #
  5. # http://www.linuxjournal.com/content/use-bash-trap-statement-cleanup-temporary-files
  6. #
  7. #
  8. #
  9. declare -a on_exit_items

  10. function on_exit()
  11. {
  12.     for i in "${on_exit_items[@]}"
  13.     do
  14.         echo "on_exit: $i"
  15.         eval $i
  16.     done
  17. }

  18. function add_on_exit()
  19. {
  20.     local n=${#on_exit_items[*]}
  21.     on_exit_items[$n]="$*"
  22.     if [[ $n -eq 0 ]]; then
  23.         echo "Setting trap"
  24.         trap on_exit EXIT
  25.     fi
  26. }

  27. function maketempdir {
  28.         tmp=/var/tmp/backup.$RANDOM.$RANDOM.$RANDOM.$
  29.         (umask 077 && mkdir $tmp) || {
  30.                 echo "Could not create temporary directory! Exiting." 1>&2
  31.                 return 1
  32.         }
  33.         return 0
  34. }
  35. FLIST=`mktemp /var/tmp/filelist.XXXXXXXX`
  36. if [ ! -e $FLIST ]
  37. then
  38.   echo "Unable to create temp file!"
  39.   exit 1
  40. fi
  41. XCLUDE=`mktemp /var/tmp/xclude.XXXXXXXX`
  42. if [ ! -e $XCLUDE ]
  43. then
  44.   rm $FLIST
  45.   echo "Unable to create temp file!"
  46.   exit 2
  47. fi

  48. trap "rm -f $FLIST $XCLUDE" EXIT

  49. TGT=/home/target
  50. SRC=/source

  51. THISBACK=$TGT/fedora-17.7z
  52. PREVBACK=$THISBACK.old
  53. TEMPBACK=`mktemp $SRC/tempback.XXXXXXXX`
  54. LOGFILE=$SRC/backup.log
  55. SEFILE=$SRC/selinux/selinux.context.txt

  56. # backup partition table
  57. fdisk -l /dev/sda > $SRC/partition.txt

  58. # backup SELinux contexts
  59. ls -Zd /var/www/ > $SEFILE
  60. ls -Zr /var/www/ >> $SEFILE
  61. echo >> $SEFILE
  62. ls -Zd /var/ftp/ >> $SEFILE
  63. ls -Zr /var/ftp/ >> $SEFILE
  64. echo >> $SEFILE
  65. getsebool -a | grep samba >> $SEFILE
  66. ls -Zd /home/samba/ >> $SEFILE
  67. ls -Zr /home/samba/ >> $SEFILE

  68. #
  69. # individual file, multiple files, folders
  70. #
  71. cat > $FLIST << EOF
  72. /etc/sysctl.conf
  73. /etc/virc
  74. /etc/proftpd.conf
  75. /etc/samba/
  76. /etc/sysconfig/samba
  77. /etc/sysconfig/network-scripts/ifcfg-p2p1
  78. /etc/sysconfig/iptables
  79. /etc/sysconfig/iptables.fc17
  80. /etc/resolv.conf
  81. /etc/httpd/
  82. /etc/bashrc
  83. /etc/crontab
  84. /etc/init.d/rc.signature
  85. /etc/fstab
  86. /etc/grub.conf
  87. /etc/default/grub
  88. /etc/yum.repos.d/rpm-sphere.repo
  89. /etc/named.conf
  90. /etc/named/
  91. /etc/tmux.conf
  92. /etc/freshclam.conf
  93. /root/partition.txt
  94. /root/selinux/
  95. /root/backup
  96. /root/fedup.txt
  97. /var/www/html/
  98. /home/someone/public_html/
  99. EOF

  100. #
  101. # files to be excluded
  102. #
  103. cat > $XCLUDE << EOF
  104. errors
  105. phorum-3.4.8a
  106. home/www/easy
  107. home/someone/public_html/songs
  108. seti/work_unit\.sah
  109. lcdmc/*.jpg
  110. bf2*
  111. video
  112. *nrg
  113. *exe
  114. mods-enabled
  115. sites-enabled
  116. chang_canton*exe
  117. chang_canton.7z
  118. ssh_host_*
  119. fiona/*.jpg
  120. EOF

  121. #
  122. echo "Starting tar ..."
  123. # if tar cpzfv $TEMPBACK -T ${FLIST} -X $XCLUDE > $LOGFILE
  124. rm $TEMPBACK
  125. if tar cpf - -T ${FLIST} -X $XCLUDE | 7za a -si -pPASSWORD $TEMPBACK > $LOGFILE
  126. then
  127.   echo "BACKUP COMPLETED SUCCESSFULLY!"
  128.   mv -f $THISBACK $PREVBACK
  129.   mv -f $TEMPBACK $THISBACK
  130.   chown nobody:nobody $THISBACK
  131.   chmod 644 $THISBACK
  132.   chcon -t samba_share_t $THISBACK
  133. else
  134.   rm -f $TEMPBACK
  135. fi
  136. #
  137. # if using bzip2 format:
  138. # tar cvp - -T ${FLIST} -X $XCLUDE | bzip2 -zv > config.bz2
  139. # bunzip2 -ckd config.bz2 | tar xvf -
  140. #
  141. # cleanup
  142. #
  143. rm $FLIST
  144. rm $XCL
複製代碼

TOP

小人的 backup script....
toylet 發表於 2013-9-21 01:05



    睇唔明啊!! 放在crontab ? backup 邊d files?

TOP

睇唔明啊!! 放在crontab ? backup 邊d files?
soccerism 發表於 21/9/2013 14:06

手動! 個 script 用左一些 tar options  和 7-zip....

TOP