Advertisement
Guest User

backup

a guest
Nov 28th, 2016
2,065
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.73 KB | None | 0 0
  1. #!/bin/bash
  2. #@author @Todi Adiyatmo Wijoyo based on
  3. #@fork from @Julius Zaromskis(http://nicaw.wordpress.com/2013/04/18/bash-backup-rotation-script/)
  4. #@description Backup script for your website
  5.  
  6. # --------------------------
  7. # Edit this configuration !!
  8. # THESE WILL BE THE SETTINGS
  9. # IF YOU DO NOT SPECIFY THEM
  10. # WITH SWITCHES.
  11. # --------------------------
  12.  
  13. # -------------------.ftp_cache
  14. # Backup Destination option, please read the README to find the possible value
  15. # --------------------
  16.  
  17. # A Temporary Location to work with files , DO NOT END THE DIRECTORY WITH BACKSLASH !
  18. TMP_DIR=/tmp
  19.  
  20. # The directory to be backed up , DO NOT END THE DIRECTORY WITH BACKSLASH !
  21. SOURCE_DIR=/home/nginx/domains/xxxx.com/public
  22.  
  23. # The directory where the backups are sent , DO NOT END THE DIRECTORY WITH BACKSLASH !
  24. TARGET_DIR=/home/backup
  25.  
  26. # Hostname
  27. HOST="xxx.Com"
  28.  
  29. # Admin email
  30. MAIL="admin@xxxx.com"
  31.  
  32. # Email Tag
  33. EMAIL_SUBJECT_TAG="[BACK of $SOURCE_DIR@$HOST]"
  34.  
  35. # Number of day the daily backup keep ( 2 day = 2 daily backup retention)
  36. RETENTION_DAY=5
  37.  
  38. # Number of day the weekly backup keep (14 day = 2 weekly backup retention )
  39. RETENTION_WEEK=14
  40.  
  41. # Number of day the monthly backup keep (30 day = 2 montly backup retention)
  42. RETENTION_MONTH=60
  43.  
  44. # Monthly date backup option (day of month)
  45. MONTHLY_BACKUP_DATE=1
  46.  
  47. # Weekly day to backup option (day of week - 1 is monday )
  48. WEEKLY_BACKUP_DAY=6
  49.  
  50. # -----------------
  51. # FTP Configuration
  52. # Enter all data within the ' ' single quote !
  53. # -----------------
  54.  
  55. #This is the FTP servers host or IP address.
  56. FTP_HOST='xxxx.hopto.org'
  57.  
  58. #FTP PORT
  59. FTP_PORT=21
  60.  
  61. #This is the FTP user that has access to the server.
  62. FTP_USER='xxxx_data'
  63.  
  64. #This is the password for the FTP user.
  65. FTP_PASSWORD='xxx'
  66.  
  67. #The backup directory on remote machine, DO NOT END THE DIRECTORY WITH BACKSLASH !
  68. FTP_TARGET_DIR=/xxx
  69.  
  70. # --------------------------------------------------
  71. # MYSQL Configuration
  72. # Enter all data within the ' ' single quote !
  73. # --------------------------------------------------
  74.  
  75. DB_USER='xxxx_com'
  76. DB_PASSWORD='$xxx#'
  77. DB_DATABASE='xxxx'
  78. DB_HOST='127.0.0.1'
  79.  
  80. # -------------------------------------------------
  81. # Extra mysqldump option
  82. # -------------------------------------------------
  83. EXTRA_MYSQLDUMP_OPTIONS=''
  84.  
  85. # --------------------------------------------------
  86. # Default Backup Options
  87. # Here you can set what you want to be backed up by
  88. # Default. 0 = no ; 1 = yes
  89. # If you use the switches and not this config file
  90. # Thes settings will be toggled automatically.
  91. # --------------------------------------------------
  92.  
  93. # 0 or 1
  94. LOCAL_BACKUP_OPTION=0
  95. FTP_BACKUP_OPTION=1
  96.  
  97. #--------------------------------------
  98. # You may set when you want to schedule
  99. # a backup of the SQL databases or the
  100. # physical files by setting the number
  101. # between 1-7. The chart below will
  102. # help you know which to pick.
  103. #----------------------------------#
  104. # Daily | Weekly | Monthly | Value #
  105. #-------|--------|---------|-------#
  106. # Yes | No | No | 1 #
  107. # No | Yes | No | 2 #
  108. # Yes | Yes | No | 3 #
  109. # No | No | Yes | 4 #
  110. # Yes | No | Yes | 5 #
  111. # No | Yes | Yes | 6 #
  112. # Yes | Yes | Yes | 7 #
  113. #----------------------------------#
  114.  
  115. #Between 1-7
  116. SQL_BACKUP_OPTION=7
  117. FILES_BACKUP_OPTION=7
  118.  
  119. # -----------------
  120. # End configuration
  121. # -----------------
  122.  
  123. RUN_NOW=0
  124. #------------------
  125. #Begin Switches
  126. #------------------
  127.  
  128. while [ "$#" -gt 0 ];
  129. do
  130. case "$1" in
  131. -h|--help)
  132. echo "-h|--help was triggered"
  133. exit 1
  134. ;;
  135.  
  136. --both)
  137. LOCAL_BACKUP_OPTION=1
  138. FTP_BACKUP_OPTION=1
  139. ;;
  140.  
  141. --ftp)
  142. FTP_BACKUP_OPTION=1
  143. if [ "$#" -gt 1 ]; then
  144. if [ ! "$2" = \-* ]; then
  145. if [[ ! "$3" = \-* && ! "$4" = \-* && ! "$5" = \-* && ! "$6" = \-* && ! "$3" == "" && ! "$4" == "" && ! "$5" = "" && ! "$6" = "" ]]; then
  146. FTP_HOST=$2
  147. FTP_PORT=$3
  148. FTP_USER=$4
  149. FTP_PASSWORD=$5
  150. FTP_TARGET_DIR=$6
  151. shift 5
  152. else
  153. echo "Error in --ftp syntax. Script failed."
  154. exit 1
  155. fi
  156. fi
  157. fi
  158. ;;
  159.  
  160. --sql)
  161. if [[ "$#" -gt 1 && ! "$2" = \-* ]]; then
  162. SQL_BACKUP_OPTION=$2
  163. if [[ ! "$3" = \-* && ! "$4" = \-* && ! "$5" = \-* && ! "$6" = \-* && ! "$3" == "" && ! "$4" == "" && ! "$5" = "" && ! "$6" = "" ]]; then
  164. DB_HOST=$3
  165. DB_USER=$4
  166. DB_PASSWORD=$5
  167. DB_DATABASE=$6
  168. shift 4
  169. else
  170. echo "Error in --sql syntax. Script failed."
  171. exit 1
  172. fi
  173. shift
  174. fi
  175. ;;
  176.  
  177. --now)
  178. RUN_NOW=1
  179. ;;
  180.  
  181. -bd|--backupdir)
  182. if [[ "$#" -gt 1 && ! "$2" = \-* ]]; then
  183. FILES_BACKUP_OPTION=$2
  184. if [[ ! "$3" = \-* && ! "$3" == "" ]]; then
  185. SOURCE_DIR=${3%/}
  186. shift 2
  187. else
  188. echo "Error in -bd|--backupdir syntax. Script failed."
  189. exit 1
  190. fi
  191. fi
  192. ;;
  193.  
  194.  
  195. -td|--targetdir)
  196. if [[ "$#" -gt 1 && ! "$2" = \-* ]]; then
  197. TARGET_DIR=${2%/}
  198. LOCAL_BACKUP_OPTION=1
  199. shift
  200. else
  201. echo "Error in -td|--targetdir syntax. Script failed."
  202. exit 1
  203. fi
  204. ;;
  205.  
  206. -e|--email)
  207. if [[ "$#" -gt 1 && ! "$2" = \-* ]]; then
  208. MAIL=$2
  209. shift
  210. else
  211. echo "Error in -e|--email syntax. Script failed."
  212. exit 1
  213. fi
  214. ;;
  215.  
  216. -r|--retention)
  217. if [[ "$#" -gt 1 && ! "$2" = \-* && ! "$3" = \-* && ! "$4" = \-* && ! "$3" == "" && ! "$4" == "" ]]; then
  218. RETENTION_DAY=$2
  219. RETENTION_WEEK=$3
  220. RETENTION_MONTH=$4
  221. shift 3
  222. else
  223. echo "Error in -r|--retention syntax. Script failed."
  224. exit 1
  225. fi
  226. ;;
  227.  
  228. -d|--dates)
  229. if [[ "$#" -gt 1 && ! "$2" = \-* && ! "$3" = \-* && ! "$3" == "" ]]; then
  230. MONTHLY_BACKUP_DATE=$2
  231. WEEKLY_BACKUP_DAY=$3
  232. shift 2
  233. else
  234. echo "Error in -d|--dates syntax. Script failed."
  235. exit 1
  236. fi
  237. ;;
  238.  
  239. --) # End of all options.
  240. shift
  241. break
  242. ;;
  243.  
  244. -?*)
  245. printf 'WARN: Unknown option (ignored): %s\n' "$1" >&2
  246. ;;
  247.  
  248. '') # Empty case: If no more options then break out of the loop.
  249. break
  250. ;;
  251.  
  252. *) # Anything unrecognized
  253. echo "The value "$1" was not expected. Script failed."
  254. exit 1
  255. ;;
  256. esac
  257.  
  258. shift
  259. done
  260.  
  261. # STARTING BACKUP SCRIPT
  262.  
  263. #Check date
  264.  
  265. # Get current month and week day number
  266. month_day=`date +"%d"`
  267. week_day=`date +"%u"`
  268.  
  269. # On first month day do
  270. if [ "$month_day" -eq $MONTHLY_BACKUP_DATE ] ; then
  271. BACKUP_TYPE='-monthly'
  272. RETENTION_DAY_LOOKUP=$RETENTION_MONTH
  273.  
  274. COMPARATOR=4
  275. else
  276. # On saturdays do
  277. if [ "$week_day" -eq $WEEKLY_BACKUP_DAY ] ; then
  278. # weekly - keep for RETENTION_WEEK
  279. BACKUP_TYPE='-weekly'
  280. RETENTION_DAY_LOOKUP=$RETENTION_WEEK
  281.  
  282. COMPARATOR=2
  283. else
  284. # On any regular day do
  285. BACKUP_TYPE='-daily'
  286. RETENTION_DAY_LOOKUP=$RETENTION_DAY
  287.  
  288. COMPARATOR=1
  289. fi
  290. fi
  291.  
  292. CURRENT_DIR=${PWD}
  293.  
  294. if [ ! $FTP_BACKUP_OPTION -eq 0 ]; then
  295. # Create list of expired backups
  296. mkdir -p $TMP_DIR/.ftp_cache/
  297. cd $TMP_DIR/.ftp_cache/
  298. find -maxdepth 1 -name "*$BACKUP_TYPE*" -mtime +$RETENTION_DAY_LOOKUP >> $TMP_DIR/.ftp_cache/search_file.tmp
  299. cd $CURRENT_DIR
  300. # List has been created, now lets get rid of them locally.
  301. # Delete expired backups
  302. find $TMP_DIR/.ftp_cache/ -maxdepth 1 -mtime +$RETENTION_DAY_LOOKUP -name "*$BACKUP_TYPE*" -exec rm -rv {} \;
  303. fi
  304.  
  305. # Cleanup expired backups
  306. echo "Removing expired backups..."
  307. find $TARGET_DIR/ -maxdepth 1 -mtime +$RETENTION_DAY_LOOKUP -name "*$BACKUP_TYPE*" -exec rm -rv {} \;
  308.  
  309. PERFORM_SQL_BACKUP=0
  310. PERFORM_FILES_BACKUP=0
  311.  
  312. # Check wheter to do backup
  313. # This no longer is FTP or LOCAL
  314. # but rather if the backup of the
  315. # files should be SQL or FILES.
  316. if [[ $(( $COMPARATOR & $SQL_BACKUP_OPTION )) == $COMPARATOR ]]; then
  317. PERFORM_SQL_BACKUP=1
  318. fi
  319.  
  320. if [[ $(( $COMPARATOR & $FILES_BACKUP_OPTION )) == $COMPARATOR ]]; then
  321. PERFORM_FILES_BACKUP=1
  322. fi
  323.  
  324. #This will force the backup to run immediately.
  325. if [ $RUN_NOW -eq 1 ]; then
  326. PERFORM_LOCAL_BACKUP=$LOCAL_BACKUP_OPTION
  327. PERFORM_FTP_BACKUP=$FTP_BACKUP_OPTION
  328. PERFORM_SQL_BACKUP=$SQL_BACKUP_OPTION
  329. PERFORM_FILES_BACKUP=$FILES_BACKUP_OPTION
  330. fi
  331.  
  332. echo "Creating backup dir.."
  333.  
  334. #Remove previous tmp dir
  335. rm -rf $TMP_DIR/backup.incoming
  336. mkdir -p $TMP_DIR/backup.incoming
  337.  
  338. # + I don't think we need to cd into there since it's specified via the command
  339. #cd $TMP_DIR/backup.incoming
  340.  
  341. # Destination file names
  342. base_backup_filename=`date +"%d-%m-%Y"`$BACKUP_TYPE
  343. backup_filename=$base_backup_filename'.tar.xz'
  344.  
  345. # SQL section
  346. if [ ! $PERFORM_SQL_BACKUP -eq 0 ]; then
  347.  
  348. echo "Perform sql backup..."
  349.  
  350. # Destination file names
  351. backup_filename=$base_backup_filename'.sql.tar.xz'
  352.  
  353. backup_filename_sql=$backup_filename
  354.  
  355. # Dump MySQL tables
  356. mysqldump -h $DB_HOST -u $DB_USER -p$DB_PASSWORD $DB_DATABASE $EXTRA_MYSQLDUMP_OPTIONS > $TMP_DIR/backup.incoming/mysql_dump.sql
  357.  
  358. echo "Compress sql backup.."
  359.  
  360. cd $TMP_DIR/backup.incoming
  361. tar -cJf $backup_filename mysql_dump.sql
  362.  
  363.  
  364. #clean sql file
  365. rm $TMP_DIR/backup.incoming/mysql_dump.sql
  366. fi
  367.  
  368. cd $CURRENT_DIR
  369.  
  370. # + This doesn't seem to work right. Maybe it's in the wrong place or looking for the wrong thing.
  371. # Even if backup works properly, this still generates an email saying that it failed.
  372. #
  373. # Optional check if source files exist. Email if failed.
  374. #if [ ! -f $TMP_DIR/backup.incoming/$backup_filename ]; then
  375. # echo "Daily backup failed! Please check for missing files." | mail -s "$EMAIL_SUBJECT_TAG Backup Failed" $MAIL
  376. #fi
  377.  
  378. # Perform Files Backup
  379. if [ ! $PERFORM_FILES_BACKUP -eq 0 ]; then
  380. backup_filename=$base_backup_filename'.data.tar.xz'
  381. echo "Perform file backup"
  382. # Compress files
  383. cd $TARGET_DIR
  384. tar -chJf $TMP_DIR/backup.incoming/$backup_filename $SOURCE_DIR
  385. fi
  386.  
  387. # FTP
  388. if [ ! $FTP_BACKUP_OPTION -eq 0 ]; then
  389. echo "Copy backup to FTP.."
  390. #create cache copy to detect the remote file
  391. #remove previous backup
  392. mkdir -p $TMP_DIR/.ftp_cache
  393. touch $TMP_DIR/.ftp_cache/$backup_filename
  394. touch $TMP_DIR/.ftp_cache/$backup_filename_sql
  395.  
  396. echo "user $FTP_USER $FTP_PASSWORD" >> $TMP_DIR/backup.incoming/ftp_command.tmp
  397. echo "mkdir $FTP_TARGET_DIR" >> $TMP_DIR/backup.incoming/ftp_command.tmp
  398. echo "cd $FTP_TARGET_DIR" >> $TMP_DIR/backup.incoming/ftp_command.tmp
  399. echo "binary" >> $TMP_DIR/backup.incoming/ftp_command.tmp
  400. echo "put $TMP_DIR/backup.incoming/$backup_filename $FTP_TARGET_DIR/$backup_filename" >> $TMP_DIR/backup.incoming/ftp_command.tmp
  401.  
  402. if [ ! $PERFORM_SQL_BACKUP -eq 0 ]; then
  403. echo "put $TMP_DIR/backup.incoming/$backup_filename_sql $FTP_TARGET_DIR/$backup_filename_sql" >> $TMP_DIR/backup.incoming/ftp_command.tmp
  404. fi
  405.  
  406.  
  407. for f in $(<$TMP_DIR/.ftp_cache/search_file.tmp)
  408. do
  409. echo "delete ${f/.\//}" >> $TMP_DIR/backup.incoming/ftp_command.tmp
  410. done
  411. echo "bye" >> $TMP_DIR/backup.incoming/ftp_command.tmp
  412.  
  413. ftp -n -v $FTP_HOST $FTP_PORT < $TMP_DIR/backup.incoming/ftp_command.tmp
  414.  
  415. echo "FTP Backup finish" | mail -s "$EMAIL_SUBJECT_TAG FTP backup finished !" $MAIL
  416. fi
  417.  
  418.  
  419. #Perform local backup
  420. if [ ! $LOCAL_BACKUP_OPTION -eq 0 ]; then
  421.  
  422. if [ ! -d $TARGET_DIR ]; then
  423. echo "Target directory : '$TARGET_DIR/' doesn't exist.."
  424. echo "Target directory : '$TARGET_DIR/' doesn't exist.." | mail -s "$EMAIL_SUBJECT_TAG Failed !" $MAIL
  425. echo "Exiting..."
  426. exit
  427. fi
  428.  
  429. echo "Copy backup to local dir.."
  430. # Move the files
  431. mv -v $TMP_DIR/backup.incoming/* $TARGET_DIR
  432. fi
  433.  
  434. # Optional check if source files exist. Email if failed.
  435. if [ -f $TARGET_DIR/$backup_filename ]; then
  436. # +Randomly generate a number to reduse the chances of overwriting an existing file. Helps ensure we get a current list and not something possibly stale.
  437. RANDOM=$(( ( RANDOM % 100 ) + 1 ))
  438. # +Temp file to allow easy emailing of current list of backups.
  439. BACKUP_LIST=$TMP_DIR/backup.list.$RANDOM.txt
  440. touch $BACKUP_LIST
  441. echo "Sending mail"
  442. echo "Local backup finished. Here's the current list of backups." > $BACKUP_LIST
  443. echo " " >> $BACKUP_LIST
  444. # +Sleep here to give the system a chance to catch up. If it goes to fast, the total size count could sometimes be incorrect.
  445. sleep 2
  446. ls -lah $TARGET_DIR >> $BACKUP_LIST
  447. cat $BACKUP_LIST | mail -s "$EMAIL_SUBJECT_TAG Finished !" $MAIL
  448. rm $TMP_DIR/backup.list.*
  449. else
  450. echo "$TARGET_DIR/$backup_filename does not seem to exist. Something failed." | mail -s "$EMAIL_SUBJECT_TAG Finished, but failed." $MAIL
  451. fi
  452.  
  453. echo "Finish.."
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement