Advertisement
Guest User

Untitled

a guest
Jan 27th, 2019
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.76 KB | None | 0 0
  1. #!/bin/bash
  2. #
  3. # bash-backup V1.1
  4. #################################################################
  5. # You need megatools in order to upload your backup file to MEGA
  6. # Download megatools from http://megatools.megous.com/
  7. #################################################################
  8. # Simple backup script for GNU/Linux servers
  9. # Main features:
  10. # - Backup custom files and directories
  11. # - Backup MySQL/PostgreSQL/MongoDB databases
  12. # - Copy/SCP/FTP to another server or mounted media
  13. # - Upload to MEGA.nz cloud
  14. # - Send a notification to your email
  15. # - Logging all the activities
  16. # - Encrypts backup file using GPG
  17. # - Backup multiple MariaDB/MySQL docker containers
  18. #
  19. # Edit the configuration and run:
  20. # $ sudo bash backup.sh
  21. #
  22. # Please help to simplify and develop new features
  23. # Narbeh - http://narbeh.org - narbeh.aj@gmail.com
  24. #################################################################
  25.  
  26. ################
  27. # Configuration
  28. ################
  29.  
  30. # Server Name
  31. server_name="hostname"
  32.  
  33. # Backup path
  34. backup_path="/tmp"
  35.  
  36. # Script log file
  37. log_file="/var/log/backup.log"
  38.  
  39. # Files to backup (Multi value)
  40. backup_files_enable="no"
  41. backup_files="/root/.bash_history /etc/passwd"
  42.  
  43. # Directories to backup (Multi value)
  44. backup_dir_enable="no"
  45. backup_directories="/etc /var/log /usr/local"
  46.  
  47. # Copy to other media (Multi value)
  48. external_copy="no"
  49. external_storage="/mnt"
  50.  
  51. # SCP to other server (Trusted servers for now)
  52. scp_enable="no"
  53. scp_server="1.2.3.4"
  54. scp_port="22"
  55. scp_username="root"
  56. scp_path="/media/backups"
  57.  
  58. # Enable iptables backup
  59. iptables_backup="no"
  60.  
  61. # Upload to FTP server (Using curl command)
  62. ftp_enable="no"
  63. ftp_server="1.2.3.4"
  64. ftp_path="/backups"
  65. ftp_username=""
  66. ftp_password=""
  67.  
  68. # Send an email the result of the backup process
  69. # You should have sendmail or postfix installed
  70. send_email="no"
  71. email_to=""
  72.  
  73. # Encrypt archive file using GPG
  74. gpg_enable="no"
  75. gpg_public_recipient=""
  76.  
  77. # Upload to MEGA.nz if you have installed the client.
  78. # /Root/ is the main directory in MEGA.nz
  79. mega_enable="no"
  80. mega_email=""
  81. mega_pass=""
  82. mega_path="/Root/backups" # /Root/ should always be here.
  83.  
  84. # Full MySQL dump (All Databases)
  85. mysql_backup="no"
  86. mysql_user=""
  87. mysql_pass=""
  88.  
  89. # Full PostgreSQL dump (All Databases)
  90. postgres_backup="no"
  91. postgres_user=""
  92. postgres_pass=""
  93. postgres_database=""
  94. postgres_host="localhost"
  95. postgres_port="5432"
  96.  
  97. # MongoDB collection dump (MongoDB Version +3)
  98. mongo_backup="no"
  99. mongo_host="localhost"
  100. mongo_port="27017"
  101. mongo_database=""
  102. mongo_collection=""
  103.  
  104. # Docker Mariadb/Mysql dump config
  105. # pattern of backup most be like containerID:::user:::password:::database
  106. # This script can backup multiple container with this pattern
  107.  
  108. docker_mysql_backup="no"
  109. docker_mysql_containers=""
  110.  
  111. #################################################################
  112. #################################################################
  113. #################################################################
  114.  
  115. ################
  116. # Do the backup
  117. ################
  118.  
  119. case $1 in
  120. "--fresh" )
  121. rm /var/backup_lock 2> /dev/null;;
  122. *)
  123. :;;
  124. esac
  125.  
  126. # Main variables
  127. color='\033[0;36m'
  128. color_fail='\033[0;31m'
  129. nc='\033[0m'
  130. hostname=$(hostname -s)
  131. date_now=$(date +"%Y-%m-%d %H:%M:%S")
  132.  
  133. # Checking lock file
  134. test -r /var/backup_lock
  135. if [ $? -eq 0 ];then
  136. echo -e "\n ${color}--- $date_now There is another backup process. \n${nc}"
  137. echo "$date_now There is another backup process." >> $log_file
  138. echo -e "\n ${color}--- $date_now If not, run the script with --fresh argument. \n${nc}"
  139. exit
  140. fi
  141.  
  142. touch /var/backup_lock 2> /dev/null
  143. path_date=$(hostname -s)_$(date +"%Y-%m-%d-%H-%M-%S")
  144. mkdir -p $backup_path/Backup/$path_date 2>> $log_file
  145. echo -e "\n ${color}--- $date_now Backup started. \n${nc}"
  146. echo "$date_now Backup started." >> $log_file
  147.  
  148. sleep 1
  149.  
  150. # Backing up the files
  151. if [ $backup_files_enable = "yes" ]
  152. then
  153. echo -e "\n ${color}--- $date_now Backing up files \n${nc}"
  154. echo "$date_now Backing up files" >> $log_file
  155. mkdir $backup_path/Backup/$path_date/custom_files | tee -a $log_file
  156. for backup_custom_files in $backup_files
  157. do
  158. echo "--> $backup_custom_files" | tee -a $log_file
  159. cp $backup_files $backup_path/Backup/$path_date/custom_files/ 2>> $log_file
  160. done
  161. echo
  162. fi
  163.  
  164. if [ $iptables_backup = "yes" ]
  165. then
  166. echo -e "\n ${color}--- $date_now Backing up iptables rules \n${nc}"
  167. echo "$date_now Backing up iptables rules" >> $log_file
  168. iptables-save >> $backup_path/Backup/$path_date/custom_files/iptables-save
  169. echo
  170. fi
  171.  
  172.  
  173. sleep 1
  174.  
  175. # Backing up the directories
  176. if [ $backup_dir_enable = "yes" ]
  177. then
  178. echo -e "\n ${color}--- $date_now Backing up directories \n${nc}"
  179. echo "$date_now Backing up directories" >> $log_file
  180. for backup_dirs in $backup_directories
  181. do
  182. echo "--> $backup_dirs" | tee -a $log_file
  183. dir_name=`echo $backup_dirs | cut -d / -f2- | sed 's/\//-/g'`
  184. if [[ -d ${backup_dirs}/.git ]]; then
  185. tar -cjf $backup_path/Backup/$path_date/$dir_name.tar.bz2 -X ${backup_dirs}/.gitignore $backup_dirs/ > /dev/null 2> /dev/null
  186. else
  187. tar -cjf $backup_path/Backup/$path_date/$dir_name.tar.bz2 $backup_dirs/ > /dev/null 2> /dev/null
  188. fi
  189. done
  190. echo
  191. fi
  192.  
  193. sleep 1
  194.  
  195. # MySQL backup
  196. if [ $mysql_backup = "yes" ]
  197. then
  198. echo -e "\n ${color}--- $date_now MySQL backup enabled, backing up: \n${nc}"
  199. echo "$date_now MySQL backup enabled, backing up" >> $log_file
  200. # Using ionice for MySQL dump
  201. ionice -c 3 mysqldump -u $mysql_user -p$mysql_pass --events --all-databases | gzip -9 > $backup_path/Backup/$path_date/MySQL_Full_Dump_$path_date.sql.gz | tee -a $log_file
  202. if [ $? -eq 0 ]
  203. then
  204. echo -e "\n ${color}--- $date_now MySQL backup completed. \n${nc}"
  205. echo "$date_now Backing up files" >> $log_file
  206. fi
  207. fi
  208.  
  209. sleep 1
  210.  
  211. # PostgreSQL backup
  212. if [ $postgres_backup = "yes" ]
  213. then
  214. # Creating ~/.pgpass for PostgreSQL password
  215. # PostgreSQL does not support inline password
  216. # Know better solution? Let me know.
  217. USERNAME=`whoami`
  218. CUR_DATE=$(date +"%Y-%m-%d-%H-%M-%S")
  219. if [ $USERNAME = "root" ]
  220. then
  221. echo "$postgres_host:$postgres_port:$postgres_database:$postgres_user:$postgres_pass" > /root/.pgpass
  222. chmod 600 /root/.pgpass
  223. else
  224. echo "$postgres_host:$postgres_port:$postgres_database:$postgres_user:$postgres_pass" > /home/$USERNAME/.pgpass
  225. chmod 600 /home/$USERNAME/.pgpass
  226. fi
  227.  
  228. echo -e "\n ${color}--- $date_now PostgreSQL backup enabled, backing up: \n${nc}"
  229. echo "$date_now PostgreSQL backup enabled, backing up" >> $log_file
  230. # Using ionice for PostgreSQL dump
  231. ionice -c 3 pg_dump -p $postgres_port -h $postgres_host -Fc -U $postgres_user $postgres_database > ${backup_path}/Backup/${path_date}/Postgres_Full_Dump_${path_date}.dump | tee -a $log_file
  232. if [ $? -eq 0 ]
  233. then
  234. echo -e "\n ${color}--- $date_now PostgreSQL backup completed. \n${nc}"
  235. echo "$date_now PostgreSQL backup completed" >> $log_file
  236. fi
  237. fi
  238.  
  239. sleep 1
  240.  
  241. # MongoDB backup
  242. if [ $mongo_backup = "yes" ]
  243. then
  244. echo -e "\n ${color}--- $date_now MongoDB backup enabled, backing up: \n${nc}"
  245. echo "$date_now MongoDB backup enabled, backing up" >> $log_file
  246. # Using ionice for MongoDB dump
  247. ionice -c 3 mongodump --host $mongo_host --collection $mongo_collection --db $mongo_database --gzip --archive=${backup_path}/Backup/${path_date}/MongoDB_${mongo_collection}_${path_date}.dump | tee -a $log_file
  248. if [ $? -eq 0 ]
  249. then
  250. echo -e "\n ${color}--- $date_now MongoDB backup completed. \n${nc}"
  251. echo "$date_now MongoDB backup completed" >> $log_file
  252. fi
  253. fi
  254.  
  255. sleep 1
  256.  
  257. # Docker Backup
  258. # Mariadb or Mysql backup
  259.  
  260. if [ $docker_mysql_backup = "yes" ]
  261. then
  262. echo -e "\n ${color}--- $date_now Docker Mariadb/MySQL backup enabled, backing up: \n${nc}"
  263. echo "$date_now Docker MySQL backup enabled, backing up" >> $log_file
  264. for docker_mysql_container in $docker_mysql_containers
  265. do
  266. docker_mysql_container_id=`echo $ocker_mysql_container | awk -F":::" '{print $1}'`
  267. docker_mysql_container_name=`docker ps --filter "id=$docker_mysql_container_id" | awk '{print $11}'`
  268. docker_mysql_user=`echo $ocker_mysql_container | awk -F":::" '{print $2}'`
  269. docker_mysql_pass=`echo $ocker_mysql_container | awk -F":::" '{print $3}'`
  270. docker_mysql_database=`echo $ocker_mysql_container | awk -F":::" '{print $4}'`
  271. docker exec $docker_mysql_container_id /usr/bin/mysqldump -u $docker_mysql_user --password=$docker_mysql_pass $docker_mysql_database | gzip -9 > $backup_path/Backup/$path_date/Docker_MySQL_${docker_mysql_container_name}_Dump_$path_date.sql.gz | tee -a $log_file
  272. if [ $? -eq 0 ]
  273. then
  274. echo -e "\n ${color}--- $date_now Docker Mariadb/MySQL backup completed. \n${nc}"
  275. echo "$date_now Docker Mariadb/MySQL backup completed" >> $log_file
  276. fi
  277. done
  278. fi
  279.  
  280.  
  281. ############################################################################################
  282.  
  283. # Create TAR file
  284. echo -e "\n ${color}--- $date_now Creating TAR file located in $backup_path/Full_Backup_$path_date.tar.bz2 \n${nc}"
  285. echo "$date_now Creating TAR file located in $backup_path/Full_Backup_$path_date.tar.bz2" >> $log_file
  286. tar -cjf $backup_path/Full_Backup_${path_date}.tar.bz2 $backup_path/Backup/$path_date 2> /dev/null
  287. rm -rf $backup_path/Backup/
  288. final_archive="Full_Backup_${path_date}.tar.bz2"
  289.  
  290. sleep 1
  291.  
  292. ############################################################################################
  293.  
  294. # Encrypt using GPG
  295. if [ $gpg_enable = "yes" ]
  296. then
  297. echo -e "\n ${color}--- $date_now Encrypting archive file using $gpg_public_recipient key\n${nc}"
  298. echo "$date_now Encrypting archive file using $gpg_public_recipient key" >> $log_file
  299. gpg --yes --always-trust -e -r $gpg_public_recipient $backup_path/Full_Backup_${path_date}.tar.bz2
  300. # Removing the unencrypted archive file
  301. rm $backup_path/Full_Backup_${path_date}.tar.bz2
  302. final_archive="Full_Backup_${path_date}.tar.bz2.gpg"
  303. fi
  304.  
  305. sleep 1
  306.  
  307. # Copy to other storage
  308. if [ $external_copy = "yes" ]
  309. then
  310. for cp_paths in $external_storage
  311. do
  312. echo -e "\n ${color}--- $date_now Copy backup archive to $cp_paths: \n${nc}"
  313. echo "$date_now Copy backup archive to $cp_paths" >> $log_file
  314. cp $backup_path/$final_archive $cp_paths/
  315. if [ $? -eq 0 ]
  316. then
  317. echo -e "Copied to $cp_paths. \n"
  318. echo "$date_now Copied to $cp_paths" >> $log_file
  319. else
  320. echo -e " ${color_fail} Copy to $cp_paths failed. ${nc} \n"
  321. echo "$date_now Copy to $cp_paths failed. Please investigate." >> $log_file
  322. fi
  323. done
  324. fi
  325.  
  326. sleep 1
  327.  
  328. # SCP to other server
  329. if [ $scp_enable = "yes" ]
  330. then
  331. echo -e "\n ${color}--- $date_now SCP backup archive to $scp_server: \n${nc}"
  332. echo "$date_now SCP backup archive to $scp_server" >> $log_file
  333. scp -P $scp_port $backup_path/$final_archive '$scp_username'@'$scp_server':$scp_path
  334. echo "$date_now SCP done" | tee -a $log_file
  335. fi
  336.  
  337. sleep 1
  338.  
  339. # Upload to FTP server
  340. if [ $ftp_enable = "yes" ]
  341. then
  342. if [ `which curl` ]
  343. then
  344. echo -e "\n ${color}--- $date_now Uploading backup archive to FTP server $ftp_server \n${nc}"
  345. echo "$date_now Uploading backup archive to FTP server $ftp_server" >> $log_file
  346. curl --connect-timeout 30 -S -T $backup_path/$final_archive ftp://$ftp_server/$ftp_path --user $ftp_username:$ftp_password | tee -a $log_file
  347. if [ $? -eq 0 ]
  348. then
  349. echo "$date_now FTP Upload Done" | tee -a $log_file
  350. else
  351. echo -e "\n ${color_fail}--- $date_now FTP upload failed. \n${nc}"
  352. echo "$date_now FTP upload failed. Please investigate." >> $log_file
  353. fi
  354. else
  355. echo -e " ${color_fail}--- $date_now You have been enabled FTP upload. ${nc}"
  356. echo -e " ${color_fail}--- $date_now You need to install curl package. ${nc}"
  357. echo -e " ${color_fail}--- $date_now FTP upload failed. ${nc}"
  358. echo "$date_now FTP upload failed. Install 'curl' package." >> $log_file
  359. fi
  360. fi
  361.  
  362. # Upload archive file to MEGA.nz
  363. if [ $mega_enable = "yes" ]
  364. then
  365. if [ `which megaput` ]
  366. then
  367. echo -e "\n ${color}--- $date_now Uploading backup archive to MEGA.nz \n${nc}"
  368. echo "$date_now Uploading backup archive to MEGA.nz" >> $log_file
  369. megaput --reload --path $mega_path -u $mega_email -p $mega_pass $backup_path/$final_archive
  370. echo "$date_now MEGA Upload Done. Path: $mega_path" | tee -a $log_file
  371. else
  372. echo -e " ${color_fail}--- $date_now You have been enabled MEGA upload. ${nc}"
  373. echo -e " ${color_fail}--- $date_now You need to install megatools from http://megatools.megous.com ${nc}"
  374. echo -e " ${color_fail}--- $date_now MEGA upload failed. ${nc}"
  375. echo "$date_now Uploading to MEGA.nz failed. Install 'megatools' from http://megatools.megous.com" >> $log_file
  376. fi
  377. fi
  378.  
  379. # Send a simple email notification
  380. if [ $send_email = "yes" ]
  381. then
  382. echo -e "Backup completed $date_now\nBackup path: $backup_path/$final_archive" | mail -s "Backup Result" $email_to >> $log_file 2>&1
  383. fi
  384.  
  385. echo -e "\n"
  386. echo -e "###########################################################"
  387. echo -e "$date_now Backup finished"
  388. echo -e "Backup path: $backup_path/$final_archive"
  389. echo -e "###########################################################"
  390. echo -e "\n"
  391. echo "$date_now Backup finished. Backup path: $backup_path/$final_archive" >> $log_file
  392. echo "#######################" >> $log_file
  393.  
  394. # Removing lock after successful backup
  395. rm /var/backup_lock
  396.  
  397. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement