backup-server 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171
  1. #!/bin/bash
  2. #/###################################################################\
  3. #| Make backup of: |
  4. #| -Web Pages (/var/www) |
  5. #| -SQL Databases (MySQL or MariaDB) |
  6. #| -HTTP Server Configuration (nginx) |
  7. #| -Let's Encrypt Certificates |
  8. #| -Home Directory (/home) |
  9. #| -GOGS Git Repository |
  10. #| -Mail Mailboxes (/var/mail) |
  11. #| -PostFix MTA Configuration (SMTP mail server) |
  12. #| -Dovecot MDA Configuration (POP3/IMAP server) |
  13. #| -OpenDKIM Configuration (Anti email Spoofing) |
  14. #| -SPF Configuration (Anti email Spoofing) |
  15. #| -OpenDMARC Configuration (Anti email Spoofing) |
  16. #| -Amavis Configuration (Antivirus) |
  17. #| -SPAM Assassin (AntiSPAM) |
  18. #| Compressed with: |
  19. #| -Gzip |
  20. #| -Bzip2 |
  21. #| -XZ |
  22. #| -7zip |
  23. #| and encrypted (or not). |
  24. #| |
  25. #| USAGE: backup-server [options] -z|-j|-J|-7 backupdir |
  26. #| HELP: backup-server -h |
  27. #| |
  28. #| Guzmán Castanedo (guzman@castanedo.es) |
  29. #| Version 2.1 (May 2018) |
  30. #| Licence: GPL v3.0 -> https://www.gnu.org/licenses/gpl-3.0.en.html |
  31. #\###################################################################/
  32. function usage {
  33. printf "%s Version 2.1\n" $(basename $0)
  34. printf "Usage: %s [options]... -z|-j|-J|-7 [backupdir]\n" $(basename $0)
  35. printf "Make a encrypted backup of a server compressed with: \n"
  36. printf "\t·Gzip (tar.gz or tar.gz.gpg)\n"
  37. printf "\t·Bzip2 (tar.bz2 or tar.gz.gpg)\n"
  38. printf "\t·XZ (tar.xz or tar.xz.gpg)\n"
  39. printf "\t·7Z (7z)\n"
  40. printf "\n"
  41. printf "Backup Directory:\n"
  42. printf "\tbackupdir: argument to set to directory to save the backup\n"
  43. printf "\t\tDefault: %s\n" $backupDir
  44. printf "Backup Options:\n"
  45. printf "\t-p, --prefix name: prefix for the name of the backup\n"
  46. printf "\t\tDefault: backup-'hostname'(%s)\n" $backupPrefix
  47. printf "\t--postfix name: postfix for the name of the backup.\n"
  48. printf "\t\tTo unset default postfix: --postfix \"\"\n"
  49. printf "\t\tDefault: prefix-yyyy-mm-dd(date)-XXXXXX(random)\n"
  50. printf "\t--no-remove: no remove old files\n"
  51. printf "\t\tDefault: false\n"
  52. printf "\t--remove-days number: set number of days to considere a backup old\n"
  53. printf "\t\tDefault: %s\n" $deleteDays
  54. printf "\t-u, --user uid: name of the user owner of the backup\n"
  55. printf "\t\tDefault: %s\n" $user
  56. printf "\t-g, --group gid: name of the group owner of the backup\n"
  57. printf "\t\tDefault: %s\n" $group
  58. printf "\t--permision-mask number: octal mask to set accesss permision of the backup\n"
  59. printf "\t\tDefault: %s\n" $permisionMask
  60. printf "\n"
  61. printf "Compression Options:\n"
  62. printf "\t-z, --gzip: compress using gzip (tar.gz or tar.gz.gpg)\n"
  63. printf "\t-j, --bzip2: compress using bzip2 (tar.bz2 or tar.bz2.gpg)\n"
  64. printf "\t-J, --xz: compress using xz (tar.xz or tar.xz.gpg)\n"
  65. printf "\t-7, --7zip: compress using 7z (7z)\n"
  66. printf "\n"
  67. printf "Encryption Options:\n"
  68. printf "\t-k, --key-id ID: set gpg2 public key-id used for encryption\n"
  69. printf "\t\tUse with Gzip, Bzip2 or XZ compression\n"
  70. printf "\t\tDefault: %s\n" $keyID
  71. printf "\t--no-encryption: disable the encryption of the backup\n"
  72. printf "\t\tDefault: %s\n" $noEncryption
  73. printf "\t--7z-password password: set password (ONLY for 7z) (INSECURE)\n"
  74. printf "\t\tDefault: %s\n" $pass7z
  75. printf "\n"
  76. printf "Web Options:\n"
  77. printf "\t--web-dir dir: set web pages directory to backup\n"
  78. printf "\t\tDefault: %s\n" $webDir
  79. printf "\t--no-web: disable backup of web pages\n"
  80. printf "\t\tDefault: %s\n" $noWeb
  81. printf "\n"
  82. printf "SQL Options:\n"
  83. printf "\t--no-sql: disable MySQL/MariaDB backup\n"
  84. printf "\t\tDefault: %s\n" $noSql
  85. printf "\t--sql-user username: set MySQL/MariaDB username\n"
  86. printf "\t\tDefault: %s\n" $mysqluser
  87. printf "\t--sql-password password: set MySQL/MariaDB password (INSECURE)\n"
  88. printf "\t\tDefault: %s\n" $mysqlpass
  89. printf "\n"
  90. printf "Nginx Options:\n"
  91. printf "\t--nginx-dir dir: set nginx configuration directory to backup\n"
  92. printf "\t\tDefault: %s\n" $nginxDir
  93. printf "\t--no-nginx: disable Nginx backup\n"
  94. printf "\t\tDefault: %s\n" $noNginx
  95. printf "\n"
  96. printf "Let's Encrypt Options:\n"
  97. printf "\t--letsencrypt-dir dir: set Let's Encrypt configuration directory to backup\n"
  98. printf "\t\tDefault: %s\n" $letsencryptDir
  99. printf "\t--no-letsencrypt: disable Let's Encrypt backup\n"
  100. printf "\t\tDefault: %s\n" $noLetsencrypt
  101. printf "\n"
  102. printf "Mail Options:\n"
  103. printf "\t--mail-dir dir: set Mailboxes directory to backup\n"
  104. printf "\t\tDefault: %s\n" $mailDir
  105. printf "\t--no-mail: disable mailboxes backup\n"
  106. printf "\t\tDefault: %s\n" $noMail
  107. printf "\n"
  108. printf "Home Options:\n"
  109. printf "\t--home-dir dir: set home directories to backup\n"
  110. printf "\t\tDefault: %s\n" $homeDir
  111. printf "\t--no-home: disable home directory backup\n"
  112. printf "\t\tDefault: %s\n" $noHome
  113. printf "\n"
  114. printf "GOGS Options:\n"
  115. printf "\t--gogs-dir dir: set GOGS Git Repository to backup\n"
  116. printf "\t\tDefault: %s\n" $gogsDir
  117. printf "\t--no-gogs: disable gogs backup\n"
  118. printf "\t\tDefault: %s\n" $noGogs
  119. printf "\n"
  120. printf "Postfix Options:\n"
  121. printf "\t--postfix-dir dir: set Postfix configuration directory to backup\n"
  122. printf "\t\tDefault: %s\n" $postfixDir
  123. printf "\t--no-postfix: disable Postfix backup\n"
  124. printf "\t\tDefault: %s\n" $noPostfix
  125. printf "\n"
  126. printf "Dovecot Options:\n"
  127. printf "\t--dovecot-dir dir: set Dovecot configuration directory to backup\n"
  128. printf "\t\tDefault: %s\n" $dovecotDir
  129. printf "\t--no-dovecot: disable Dovecot backup\n"
  130. printf "\t\tDefault: %s\n" $noDovecot
  131. printf "\n"
  132. printf "OpenDKIM Options:\n"
  133. printf "\t--opendkim-conf file: set OpenDKIM configuration file to backup\n"
  134. printf "\t\tDefault: %s\n" $openDKIMConf
  135. printf "\t--opendkim-default file: set OpenDKIM socket configuration file to backup\n"
  136. printf "\t\tDefault: %s\n" $openDKIMDefault
  137. printf "\t--opendkim-keys dir: set OpenDKIM keys dir to backup\n"
  138. printf "\t\tDefault: %s\n" $openDKIMKeys
  139. printf "\t--no-opendkim: disable OpenDKIM backup\n"
  140. printf "\t\tDefault: %s\n" $noOpenDKIM
  141. printf "\n"
  142. printf "SPF Options:\n"
  143. printf "\t--spf-dir dir: set SPF configuration directory to backup\n"
  144. printf "\t\tDefault: %s\n" $spfDir
  145. printf "\t--no-spf: disable SPF backup\n"
  146. printf "\t\tDefault: %s\n" $noSPF
  147. printf "\n"
  148. printf "OpenDMARC Options:\n"
  149. printf "\t--opendmarc-conf file: set OpenDMARC configuration file to backup\n"
  150. printf "\t\tDefault: %s\n" $openDMARCConf
  151. printf "\t--opendmarc-default file: set OpenDMARC socket configuration file to backup\n"
  152. printf "\t\tDefault: %s\n" $openDMARCDefault
  153. printf "\t--no-opendmarc: disable OpenDMARC backup\n"
  154. printf "\t\tDefault: %s\n" $noOpenDMARC
  155. printf "\n"
  156. printf "Amavis Options:\n"
  157. printf "\t--amavis-dir dir: set Amavis configuration directory to backup\n"
  158. printf "\t\tDefault: %s\n" $amavisDir
  159. printf "\t--no-amavis: disable Amavis backup\n"
  160. printf "\t\tDefault: %s\n" $noAmavis
  161. printf "\n"
  162. printf "SPAM Assassin Options:\n"
  163. printf "\t--spamassassin-dir dir: set SPAM Assassin configuration directory to backup\n"
  164. printf "\t\tDefault: %s\n" $spamAssassinDir
  165. printf "\t--no-spamassassin: disable SPAM Assassin backup\n"
  166. printf "\t\tDefault: %s\n" $noSpamAssassin
  167. printf "\n"
  168. printf "Other Options:\n"
  169. printf "\t--no-sha512: not calculate SHA512 hash (to check integrity)\n"
  170. printf "\t-h, --help: shows this message and exit\n"
  171. printf "\n"
  172. printf "Examples:\n"
  173. printf "\t·%s --xz -k ABCDEFG -p backup-example.com --no-sql /var/backup\n" $(basename $0)
  174. printf "\t\tMake a backup in \"/var/backup\" compressed with XZ and encrypted with GPG2\n"
  175. printf "\t\tPublic Key (\"backup-example.com-2018-03-19-a1b2c3.tar.xz.gpg\")\n"
  176. printf "\t\tDisable SQL backup\n"
  177. printf "\t·%s -7 --7z-password 7zpass --sql-user root --sql-password toor /var/www/backup\n" $(basename $0)
  178. printf "\t\tMake a backup in \"/var/www/backup\" compressed with 7Zip and encrypted\n"
  179. printf "\t\t(\"backup-'hostname'-2018-03-19-abcdef.7z\")\n"
  180. printf "\t·%s -z --no-encryption --no-sql /var/backup\n" $(basename $0)
  181. printf "\t\tMake a backup in \"/var/backup\" compressed with GZip and not encrypted\n"
  182. printf "\t\t(\"backup-'hostname'-2018-03-19-qwerty.tar.gz\")\n"
  183. }
  184. function makep7zip {
  185. #Destination file
  186. encrypt7z=""
  187. if [ ! -z $pass7z ];then
  188. encrypt7z="-p$pass7z -mhe"
  189. fi
  190. if [ $noEncryption = true ];then
  191. encrypt7z=""
  192. fi
  193. printf "Backup File:\t%s\n" $backupOutput
  194. #Copy webpages code (except backup and main/public)
  195. if [ $noWeb = false ];then
  196. printf "Compressing:\t%s\n" $webDir
  197. tempfile=$(mktemp -t exclude-XXX)
  198. echo "www/backup" > $tempfile
  199. echo "www/main/public" >> $tempfile
  200. 7z a -t7z -mx=9 $encrypt7z $backupOutput $webDir -x@$tempfile > /dev/null
  201. if [ $? != 0 ];then
  202. printf "WARNING:\tError copying web pages (Continue).\n"
  203. fi
  204. rm $tempfile
  205. fi
  206. #Copy MySQL databases (mysqldump)
  207. if [ $noSql = false ]; then
  208. list=$(mysql -u $mysqluser -p$mysqlpass -e "show DATABASES;")
  209. #Parse databases expect information_schema & performance_schema
  210. for database in $list; do
  211. valid=true
  212. for excep in Database information_schema performance_schema; do
  213. if [ $database = $excep ]; then
  214. valid=false
  215. break
  216. fi
  217. done
  218. if [ $valid = true ]; then
  219. printf "Compressing MySQL database:\t%s\n" $database.sql
  220. mysqldump -u $mysqluser -p$mysqlpass $database | 7z a -t7z -mx=9 $encrypt7z $backupOutput -simysql/$database.sql > /dev/null 2>&1
  221. if [ $? != 0 ];then
  222. printf "WARNING:\tError compressing database (%s) (Continue).\n" $database
  223. fi
  224. fi
  225. done
  226. fi
  227. #Copy nginx configuration (sites-available)
  228. if [ $noNginx = false ];then
  229. printf "Compressing:\t%s\n" $nginxDir
  230. 7z a -t7z -mx=9 $encrypt7z $backupOutput $nginxDir > /dev/null
  231. if [ $? != 0 ];then
  232. printf "WARNING:\tError copying nginx configuration (Continue)\n"
  233. fi
  234. fi
  235. #Copy Email (this could be heavy in the future)
  236. if [ $noMail = false ];then
  237. printf "Compressing:\t%s\n" $mailDir
  238. 7z a -t7z -mx=9 $encrypt7z $backupOutput $mailDir > /dev/null
  239. #tar -c -zf - $mailDir | 7z a -t7z -mx=9 -p$pass7z -mhe $backupOutput -simail.tar.gz > /dev/null
  240. if [ $? != 0 ];then
  241. printf "WARNING:\tError copying mailboxes (Continue)\n"
  242. fi
  243. fi
  244. #Copy Certificates (LetsEncrypt)
  245. if [ $noLetsencrypt = false ];then
  246. printf "Compressing:\t%s\n" $letsencryptDir
  247. 7z a -t7z -mx=9 $encrypt7z $backupOutput $letsencryptDir > /dev/null
  248. if [ $? != 0 ];then
  249. printf "WARNING:\tError copying Let's Encrypt certificates (Continue)\n"
  250. fi
  251. fi
  252. #Copy /home
  253. if [ $noHome = false ];then
  254. printf "Compressing:\t%s\n" $homeDir
  255. 7z a -t7z -mx=9 $encrypt7z $backupOutput $homeDir > /dev/null
  256. if [ $? != 0 ];then
  257. printf "WARNING:\tError copying home dir (Continue)\n"
  258. fi
  259. fi
  260. #Copy GOGS
  261. if [ $noGogs = false ];then
  262. printf "Compressing:\t%s\n" $gogsDir
  263. 7z a -t7z -mx=9 $encrypt7z $backupOutput $gogsDir > /dev/null
  264. if [ $? != 0 ];then
  265. printf "WARNING:\tError copying GOGS Repository (Continue)\n"
  266. fi
  267. fi
  268. #Copy Postfix
  269. if [ $noPostfix = false ] && [ -d $postfixDir ];then
  270. printf "Compressing:\t%s\n" $postfixDir
  271. 7z a -t7z -mx=9 $encrypt7z $backupOutput $postfixDir > /dev/null
  272. if [ $? != 0 ];then
  273. printf "WARNING:\tError copying Postfix configuration (Continue)\n"
  274. fi
  275. fi
  276. #Copy Dovecot
  277. if [ $noDovecot = false ];then
  278. printf "Compressing:\t%s\n" $dovecotDir
  279. 7z a -t7z -mx=9 $encrypt7z $backupOutput $dovecotDir > /dev/null
  280. if [ $? != 0 ];then
  281. printf "WARNING:\tError copying Dovecot configuration (Continue)\n"
  282. fi
  283. fi
  284. #Copy OpenDKIM
  285. if [ $noOpenDKIM = false ];then
  286. if [ -f $openDKIMConf ];then
  287. printf "Compressing:\t%s\n" $openDKIMConf
  288. 7z a -t7z -mx=9 $encrypt7z $backupOutput $openDKIMConf > /dev/null
  289. if [ $? != 0 ];then
  290. printf "WARNING:\tError copying OpenDKIM configuration (Continue)\n"
  291. fi
  292. else
  293. printf "WARNING:\tFile %s not exist\n" $openDKIMConf
  294. fi
  295. if [ -f $openDKIMDefault ];then
  296. printf "Compressing:\t%s\n" $openDKIMDefault
  297. 7z a -t7z -mx=9 $encrypt7z $backupOutput $openDKIMDefault > /dev/null
  298. if [ $? != 0 ];then
  299. printf "WARNING:\tError copying OpenDKIM sockets configuration (Continue)\n"
  300. fi
  301. else
  302. printf "WARNING:\tFile %s not exist\n" $openDKIMDefault
  303. fi
  304. if [ -d $openDKIMKeys ];then
  305. printf "Compressing:\t%s\n" $openDKIMKeys
  306. 7z a -t7z -mx=9 $encrypt7z $backupOutput $openDKIMKeys > /dev/null
  307. if [ $? != 0 ];then
  308. printf "WARNING:\tError copying OpenDKIM keys (Continue)\n"
  309. fi
  310. else
  311. printf "WARNING:\tDir %s not exist\n" $openDKIMKeys
  312. fi
  313. fi
  314. #Copy SPF
  315. if [ $noSPF = false ];then
  316. printf "Compressing:\t%s\n" $spfDir
  317. 7z a -t7z -mx=9 $encrypt7z $backupOutput $spfDir > /dev/null
  318. if [ $? != 0 ];then
  319. printf "WARNING:\tError copying Postfix configuration (Continue)\n"
  320. fi
  321. fi
  322. #Copy OpenDMARC
  323. if [ $noOpenDMARC = false ];then
  324. if [ -f $openDMARCConf ];then
  325. printf "Compressing:\t%s\n" $openDMARCConf
  326. 7z a -t7z -mx=9 $encrypt7z $backupOutput $openDMARCConf > /dev/null
  327. if [ $? != 0 ];then
  328. printf "WARNING:\tError copying OpenDMARC configuration (Continue)\n"
  329. fi
  330. else
  331. printf "WARNING:\tFile %s not exist\n" $openDMARCConf
  332. fi
  333. if [ -f $openDMARCDefault ];then
  334. printf "Compressing:\t%s\n" $openDMARCDefault
  335. 7z a -t7z -mx=9 $encrypt7z $backupOutput $openDMARCDefault > /dev/null
  336. if [ $? != 0 ];then
  337. printf "WARNING:\tError copying OpenDMARC sockets configuration (Continue)\n"
  338. fi
  339. else
  340. printf "WARNING:\tFile %s not exist\n" $openDMARCDefault
  341. fi
  342. fi
  343. #Copy Amavis
  344. if [ $noAmavis = false ];then
  345. printf "Compressing:\t%s\n" $amavisDir
  346. 7z a -t7z -mx=9 $encrypt7z $backupOutput $amavisDir > /dev/null
  347. if [ $? != 0 ];then
  348. printf "WARNING:\tError copying Amavis configuration (Continue)\n"
  349. fi
  350. fi
  351. #Copy SPAMAssassin
  352. if [ $noSpamAssassin = false ];then
  353. printf "Compressing:\t%s\n" $spamAssassinDir
  354. 7z a -t7z -mx=9 $encrypt7z $backupOutput $spamAssassinDir > /dev/null
  355. if [ $? != 0 ];then
  356. printf "WARNING:\tError copying SPAM Assasin configuration (Continue)\n"
  357. fi
  358. fi
  359. }
  360. function makeTar {
  361. #Destination file
  362. printf "Backup File:\t%s\n" $backupOutput
  363. #Copy webpages code (except backup and main/public)
  364. if [ $noWeb = false ];then
  365. printf "Adding:\t%s\n" $webDir
  366. tar -rf $tempOutput --exclude=var/www/backup --exclude=var/www/main/public $webDir > /dev/null 2>&1
  367. if [ $? != 0 ]; then
  368. printf "WARNING:\tError copying web pages (Continue).\n"
  369. fi
  370. fi
  371. #Copy MySQL databases (mysqldump)
  372. if [ $noSql = false ]; then
  373. list=$(mysql -u $mysqluser -p$mysqlpass -e "show DATABASES;" 2> /dev/null)
  374. mkdir /tmp/mysql
  375. #Parse databases expect information_schema & performance_schema
  376. for database in $list; do
  377. valid=true
  378. for excep in Database information_schema performance_schema; do
  379. if [ $database = $excep ]; then
  380. valid=false
  381. break
  382. fi
  383. done
  384. if [ $valid = true ]; then
  385. printf "Adding MySQL database:\t%s\n" $database.sql
  386. mysqldump -u $mysqluser -p$mysqlpass $database > /tmp/mysql/$database.sql 2> /dev/null
  387. if [ $? != 0 ];then
  388. printf "WARNING:\tError extracting database (%s) (Continue).\n" $database
  389. continue
  390. fi
  391. tar -rf $tempOutput -C /tmp mysql/$database.sql > /dev/null 2>&1
  392. if [ $? != 0 ];then
  393. printf "WARNING:\tError adding to tar (%s) (Continue)\n"
  394. fi
  395. rm /tmp/mysql/$database.sql
  396. fi
  397. done
  398. rm -R /tmp/mysql
  399. fi
  400. #Copy nginx configuration (sites-available)
  401. if [ $noNginx = false ];then
  402. printf "Adding:\t%s\n" $nginxDir
  403. tar -rf $tempOutput $nginxDir > /dev/null 2>&1
  404. if [ $? != 0 ];then
  405. printf "WARNING:\tError copying nginx configuration (Continue)\n"
  406. fi
  407. fi
  408. #Copy Email (this could be heavy in the future)
  409. if [ $noMail = false ];then
  410. printf "Adding:\t%s\n" $mailDir
  411. tar -rf $tempOutput $mailDir > /dev/null 2>&1
  412. if [ $? != 0 ];then
  413. printf "WARNING:\tError copying mailboxes (Continue)\n"
  414. fi
  415. fi
  416. #Copy Certificates (LetsEncrypt)
  417. if [ $noLetsencrypt = false ];then
  418. printf "Adding:\t%s\n" $letsencryptDir
  419. tar -rf $tempOutput $letsencryptDir > /dev/null 2>&1
  420. if [ $? != 0 ];then
  421. printf "WARNING:\tError copying Let's Encrypt certificates (Continue)\n"
  422. fi
  423. fi
  424. #Copy /home
  425. if [ $noHome = false ];then
  426. printf "Adding:\t%s\n" $homeDir
  427. tar -rf $tempOutput $homeDir > /dev/null 2>&1
  428. if [ $? != 0 ];then
  429. printf "WARNING:\tError copying home dir (Continue)\n"
  430. fi
  431. fi
  432. #Copy GOGS
  433. if [ $noGogs = false ];then
  434. printf "Adding:\t%s\n" $gogsDir
  435. tar -rf $tempOutput $gogsDir > /dev/null 2>&1
  436. if [ $? != 0 ];then
  437. printf "WARNING:\tError copying GOGS Repository (Continue)\n"
  438. fi
  439. fi
  440. #Copy Postfix
  441. if [ $noPostfix = false ] && [ -d $postfixDir ];then
  442. printf "Adding:\t%s\n" $postfixDir
  443. tar -rf $tempOutput $postfixDir > /dev/null 2>&1
  444. if [ $? != 0 ];then
  445. printf "WARNING:\tError copying Postfix configuration (Continue)\n"
  446. fi
  447. fi
  448. #Copy Dovecot
  449. if [ $noDovecot = false ];then
  450. printf "Adding:\t%s\n" $dovecotDir
  451. tar -rf $tempOutput $dovecotDir > /dev/null 2>&1
  452. if [ $? != 0 ];then
  453. printf "WARNING:\tError copying Dovecot configuration (Continue)\n"
  454. fi
  455. fi
  456. #Copy OpenDKIM
  457. if [ $noOpenDKIM = false ];then
  458. if [ -f $openDKIMConf ];then
  459. printf "Adding:\t%s\n" $openDKIMConf
  460. tar -rf $tempOutput $openDKIMConf > /dev/null 2>&1
  461. if [ $? != 0 ];then
  462. printf "WARNING:\tError copying OpenDKIM configuration (Continue)\n"
  463. fi
  464. else
  465. printf "WARNING:\tFile %s not exist\n" $openDKIMConf
  466. fi
  467. if [ -f $openDKIMDefault ];then
  468. printf "Adding:\t%s\n" $openDKIMDefault
  469. tar -rf $tempOutput $openDKIMDefault > /dev/null 2>&1
  470. if [ $? != 0 ];then
  471. printf "WARNING:\tError copying OpenDKIM sockets configuration (Continue)\n"
  472. fi
  473. else
  474. printf "WARNING:\tFile %s not exist\n" $openDKIMDefault
  475. fi
  476. if [ -d $openDKIMKeys ];then
  477. printf "Adding:\t%s\n" $openDKIMKeys
  478. tar -rf $tempOutput $openDKIMKeys > /dev/null 2>&1
  479. if [ $? != 0 ];then
  480. printf "WARNING:\tError copying OpenDKIM keys (Continue)\n"
  481. fi
  482. else
  483. printf "WARNING:\tDir %s not exist\n" $openDKIMKeys
  484. fi
  485. fi
  486. #Copy SPF
  487. if [ $noSPF = false ];then
  488. printf "Adding:\t%s\n" $spfDir
  489. tar -rf $tempOutput $spfDir > /dev/null 2>&1
  490. if [ $? != 0 ];then
  491. printf "WARNING:\tError copying Postfix configuration (Continue)\n"
  492. fi
  493. fi
  494. #Copy OpenDMARC
  495. if [ $noOpenDMARC = false ];then
  496. if [ -f $openDMARCConf ];then
  497. printf "Adding:\t%s\n" $openDMARCConf
  498. tar -rf $tempOutput $openDMARCConf > /dev/null 2>&1
  499. if [ $? != 0 ];then
  500. printf "WARNING:\tError copying OpenDMARC configuration (Continue)\n"
  501. fi
  502. else
  503. printf "WARNING:\tFile %s not exist\n" $openDMARCConf
  504. fi
  505. if [ -f $openDMARCDefault ];then
  506. printf "Adding:\t%s\n" $openDMARCDefault
  507. tar -rf $tempOutput $openDMARCDefault > /dev/null 2>&1
  508. if [ $? != 0 ];then
  509. printf "WARNING:\tError copying OpenDMARC sockets configuration (Continue)\n"
  510. fi
  511. else
  512. printf "WARNING:\tFile %s not exist\n" $openDMARCDefault
  513. fi
  514. fi
  515. #Copy Amavis
  516. if [ $noAmavis = false ];then
  517. printf "Adding:\t%s\n" $amavisDir
  518. tar -rf $tempOutput $amavisDir > /dev/null 2>&1
  519. if [ $? != 0 ];then
  520. printf "WARNING:\tError copying Amavis configuration (Continue)\n"
  521. fi
  522. fi
  523. #Copy SPAMAssassin
  524. if [ $noSpamAssassin = false ];then
  525. printf "Adding:\t%s\n" $spamAssassinDir
  526. tar -rf $tempOutput $spamAssassinDir > /dev/null 2>&1
  527. if [ $? != 0 ];then
  528. printf "WARNING:\tError copying SPAM Assasin configuration (Continue)\n"
  529. fi
  530. fi
  531. }
  532. function gzUnencrypted {
  533. printf "Compressing (GZIP)..."
  534. gzip -9 --stdout $tempOutput > $backupOutput
  535. if [ $? != 0 ]; then
  536. printf "\nERROR:\tImpossible to compress (%s)\n" $backupOutput$ext
  537. exit 1
  538. fi
  539. rm $tempOutput
  540. printf " done\n"
  541. }
  542. function gzEncrypted {
  543. printf "Compressing (GZIP) and encrypting..."
  544. gzip -9 --stdout $tempOutput | gpg2 --no-batch --output $backupOutput --encrypt -r $keyID -
  545. if [ $? != 0 ]; then
  546. printf "\nERROR:\tImpossible to compress (%s)\n" $backupOutput$ext
  547. exit 1
  548. fi
  549. rm $tempOutput
  550. printf " done\n"
  551. }
  552. function bz2Unencrypted {
  553. printf "Compressing (BZIP2)..."
  554. bzip2 -9 --stdout $tempOutput > $backupOutput
  555. if [ $? != 0 ]; then
  556. printf "\nERROR:\tImpossible to compress (%s)\n" $backupOutput$ext
  557. exit 1
  558. fi
  559. rm $tempOutput
  560. printf " done\n"
  561. }
  562. function bz2Encrypted {
  563. printf "Compressing (BZIP2) and encrypting..."
  564. bzip2 -9 --stdout $tempOutput | gpg2 --no-batch --output $backupOutput --encrypt -r $keyID -
  565. if [ $? != 0 ]; then
  566. printf "\nERROR:\tImpossible to compress (%s)\n" $backupOutput$ext
  567. exit 1
  568. fi
  569. rm $tempOutput
  570. printf " done\n"
  571. }
  572. function xzUnencrypted {
  573. printf "Compressing (XZ)..."
  574. xz -9 --stdout $tempOutput > $backupOutput
  575. if [ $? != 0 ]; then
  576. printf "\nERROR:\tImpossible to compress (%s)\n" $backupOutput$ext
  577. exit 1
  578. fi
  579. rm $tempOutput
  580. printf " done\n"
  581. }
  582. function xzEncrypted {
  583. printf "Compressing (XZ) and encrypting..."
  584. xz -9 --stdout $tempOutput | gpg2 --no-batch --output $backupOutput --encrypt -r $keyID -
  585. if [ $? != 0 ]; then
  586. printf "\nERROR:\tImpossible to compress (%s)\n" $backupOutput$ext
  587. exit 1
  588. fi
  589. rm $tempOutput
  590. printf " done\n"
  591. }
  592. function makeSHA512 {
  593. if [ ! -x $(which sha512sum) ]; then
  594. printf "WARNING:\tsha512sum not installed\n"
  595. sha512=false
  596. fi
  597. if [ $sha512 = true ]; then
  598. printf "Calculating checksum (SHA512)..."
  599. cd $backupDir > /dev/null 2>&1
  600. sha512sum -b $(basename $backupOutput) > $backupOutput.sha512
  601. #Permissions
  602. chown $user:$group $backupOutput.sha512
  603. chmod $permisionMask $backupOutput.sha512
  604. cd - > /dev/null 2>&1
  605. printf " done\n"
  606. fi
  607. }
  608. function checkRoutes {
  609. if [ ! -d $backupDir ];then
  610. printf "ERROR:\tBackup directory don't exist\n"
  611. usage
  612. exit 1
  613. fi
  614. if [ $noWeb = false ] && [ ! -d $webDir ];then
  615. printf "WARNING:\t%s don't exist (no backup)\n" $webDir
  616. noWeb=true
  617. fi
  618. if [ $noNginx = false ] && [ ! -d $nginxDir ];then
  619. printf "WARNING:\t%s don't exist (no backup)\n" $nginxDir
  620. noNginx=true
  621. fi
  622. if [ $noLetsencrypt = false ] && [ ! -d $letsencryptDir ];then
  623. printf "WARNING:\t%s don't exist (no backup)\n" $letsencryptDir
  624. noLetsencrypt=true
  625. fi
  626. if [ $noMail = false ] && [ ! -d $mailDir ];then
  627. printf "WARNING:\t%s don't exist (no backup)\n" $mailDir
  628. noMail=true
  629. fi
  630. if [ $noHome = false ] && [ ! -d $homeDir ];then
  631. printf "WARNING:\t%s don't exist (no backup)\n" $homeDir
  632. noHome=true
  633. fi
  634. if [ $noGogs = false ] && [ ! -d $gogsDir ];then
  635. printf "WARNING:\t%s don't exist (no backup)\n" $gogsDir
  636. noGogs=true
  637. fi
  638. if [ $noPostfix = false ] && [ ! -d $postfixDir ];then
  639. printf "WARNING:\t%s don't exist (no backup)\n" $postfixDir
  640. noPostfix=true
  641. fi
  642. if [ $noDovecot = false ] && [ ! -d $dovecotDir ];then
  643. printf "WARNING:\t%s don't exist (no backup)\n" $dovecotDir
  644. noDovecot=true
  645. fi
  646. # No noOpenDKIM=true, to make a parcial copy
  647. if [ $noOpenDKIM = false ] && [ ! -f $openDKIMConf ];then
  648. printf "WARNING:\t%s don't exist (no backup)\n" $openDKIMConf
  649. fi
  650. if [ $noOpenDKIM = false ] && [ ! -f $openDKIMDefault ];then
  651. printf "WARNING:\t%s don't exist (no backup)\n" $openDKIMDefault
  652. fi
  653. if [ $noOpenDKIM = false ] && [ ! -d $openDKIMKeys ];then
  654. printf "WARNING:\t%s don't exist (no backup)\n" $openDKIMKeys
  655. fi
  656. if [ $noSPF = false ] && [ ! -d $spfDir ];then
  657. printf "WARNING:\t%s don't exist (no backup)\n" $spfDir
  658. noSpf=true
  659. fi
  660. # No noOpenDMARC=true, to make a parcial copy
  661. if [ $noOpenDMARC = false ] && [ ! -f $openDMARCConf ];then
  662. printf "WARNING:\t%s don't exist (no backup)\n" $openDMARCConf
  663. fi
  664. if [ $noOpenDMARC = false ] && [ ! -f $openDMARCDefault ];then
  665. printf "WARNING:\t%s don't exist (no backup)\n" $openDMARCDefault
  666. fi
  667. if [ $noAmavis = false ] && [ ! -d $amavisDir ];then
  668. printf "WARNING:\t%s don't exist (no backup)\n" $amavisDir
  669. noAmavis=true
  670. fi
  671. if [ $noSpamAssassin = false ] && [ ! -d $spamAssassinDir ];then
  672. printf "WARNING:\t%s don't exist (no backup)\n" $spamAssassinDir
  673. noSpamAssassin=true
  674. fi
  675. }
  676. function checkMethod {
  677. if [ $gzipOn = false ] && [ $bzip2On = false ] && [ $xzOn = false ] && [ $p7zipOn = false ];then
  678. printf "ERROR:\tCompressing method not set\n"
  679. exit 1
  680. fi
  681. if [ $noEncryption = false ];then
  682. if [ $gzipOn = true ] || [ $bzip2On = true ] || [ $xzOn = true ];then
  683. if [ ! -x $(which gpg2) ];then
  684. printf "WARNING:\tGPG2 Not Installed: continue without encryption (type \"%s -h\" for help)\n" $(basename $0)
  685. noEncryption=true
  686. else
  687. if [ ! -z $keyID ];then
  688. ext=$ext.gpg
  689. gpg2 --list-keys $keyID > /dev/null 2>&1
  690. if [ $? != 0 ];then
  691. printf "ERROR:\tKeyID %s don't exist in keyring (\"gpg2 --list-keys\" to see all keys)\n" $keyID
  692. exit 1
  693. fi
  694. else
  695. printf "WARNING:\tKeyID (-k keyID) not set: continue without encryption (type \"%s -h\" for help)\n" $(basename $0)
  696. noEncryption=true
  697. fi
  698. fi
  699. else
  700. if [ -z $pass7z ];then
  701. printf "WARNING:\t7Z password (--7z-password) not set: continue without encryption (type \"%s -h\" for help)\n" $(basename $0)
  702. noEncryption=true
  703. fi
  704. fi
  705. fi
  706. }
  707. function checkSqlAuth {
  708. if [ $noSql = false ];then
  709. if [ ! -x "$(which mysql)" ] || [ ! -x "$(which mysqldump)" ];then
  710. printf "WARNING:\tmysql or/and mysqldump Not installed\n"
  711. noSql=true
  712. fi
  713. if [ -z $mysqluser ] || [ -z $mysqlpass ]; then
  714. printf "WARNING:\tSQL User/Password not set: continue without SQL backup (type \"%s -h\" for help)\n" $(basename $0)
  715. noSql=true
  716. fi
  717. fi
  718. }
  719. #Check root
  720. startTime=$(date +"%s")
  721. if [ $(id -u) -ne 0 ]; then
  722. printf "ERROR:\tNeed to be root :O\n"
  723. exit 1
  724. fi
  725. #Default Data
  726. mysqluser="root"
  727. mysqlpass=""
  728. keyID=""
  729. pass7z=""
  730. backupDir=/var/www/backup
  731. backupPrefix="backup-"$(hostname)
  732. backupPostfix=-$(date +"%Y-%m-%d")-$(cat /dev/urandom | tr -dc 'a-z0-9' | fold -w 6 | head -n 1)
  733. deleteDays="15"
  734. user="www-data"
  735. group="www-data"
  736. #Default Routes
  737. webDir="/var/www"
  738. nginxDir="/etc/nginx"
  739. mailDir="/var/mail"
  740. letsencryptDir="/etc/letsencrypt"
  741. homeDir="/home"
  742. gogsDir="/opt/gogs"
  743. postfixDir="/etc/postfix"
  744. dovecotDir="/etc/dovecot"
  745. openDKIMConf="/etc/opendkim.conf"
  746. openDKIMDefault="/etc/default/opendkim"
  747. openDKIMKeys="/etc/dkimkeys"
  748. spfDir="/etc/postfix-policyd-spf-python"
  749. openDMARCConf="/etc/opendmarc.conf"
  750. openDMARCDefault="/etc/default/opendmarc"
  751. amavisDir="/etc/amavis"
  752. spamAssassinDir="/etc/spamassassin"
  753. #Control Variables
  754. noEncryption=false
  755. gzipOn=false
  756. bzip2On=false
  757. xzOn=false
  758. p7zipOn=false
  759. ext=""
  760. permisionMask=640
  761. removeOld=true
  762. noWeb=false
  763. noSql=false
  764. noNginx=false
  765. noMail=false
  766. noLetsencrypt=false
  767. noHome=false
  768. noGogs=false
  769. noPostfix=false
  770. noDovecot=false
  771. noOpenDKIM=false
  772. noSPF=false
  773. noOpenDMARC=false
  774. noAmavis=false
  775. noSpamAssassin=false
  776. sha512=true
  777. #Parse args
  778. TEMP=$(getopt -q -o zjJ7p:u:g:k:h --longoptions gzip,bzip2,xz,7zip,prefix:,postfix:,no-remove,remove-days:,user:,group:,permision-mask:,key-id:,no-encryption,7z-password:,web-dir:,no-web,no-sql,sql-user:,sql-password:,nginx-dir:,no-nginx,letsencrypt-dir:,no-letsencrypt,mail-dir:,no-mail,home-dir:,no-home,gogs-dir:,no-gogs,postfix-dir:,no-postfix,dovecot-dir:,no-dovecot,opendkim-conf:,opendkim-default:,opendkim-keys:,no-opendkim,spf-dir:,no-spf,opendmarc-conf:,opendmarc-default:,no-opendmarc,amavis-dir:,no-amavis,spamassassin-dir:,no-spamassassin,no-sha512,help --name $(basename $0) -- $@)
  779. eval set -- $TEMP
  780. unset TEMP
  781. while true; do
  782. case $1 in
  783. -z|--gzip)
  784. gzipOn=true
  785. ext=".tar.gz"
  786. if [ $bzip2On = true ] || [ $xzOn = true ] || [ $p7zipOn = true ];then
  787. printf "ERROR:\tOnly choose one compressing method\n"
  788. exit 1
  789. fi
  790. if [ ! -x $(which gzip) ];then
  791. printf "ERROR:\tGzip Not Installed\n"
  792. exit 1
  793. fi
  794. shift
  795. ;;
  796. -j|--bzip2)
  797. bzip2On=true
  798. ext=".tar.bz2"
  799. if [ $gzipOn = true ] || [ $xzOn = true ] || [ $p7zipOn = true ];then
  800. printf "ERROR:\tOnly choose one compressing method\n"
  801. exit 1
  802. fi
  803. if [ ! -x $(which bzip2) ];then
  804. printf "ERROR:\tBzip2 Not Installed\n"
  805. exit 1
  806. fi
  807. shift
  808. ;;
  809. -J|--xz)
  810. xzOn=true
  811. ext=".tar.xz"
  812. if [ $gzipOn = true ] || [ $bzip2On = true ] || [ $p7zipOn = true ];then
  813. printf "ERROR:\tOnly choose one compressing method\n"
  814. exit 1
  815. fi
  816. if [ ! -x $(which xz) ];then
  817. printf "ERROR:\tXZ Not Installed\n"
  818. exit 1
  819. fi
  820. shift
  821. ;;
  822. -7|--7zip)
  823. p7zipOn=true
  824. ext=".7z"
  825. if [ $gzipOn = true ] || [ $bzip2On = true ] || [ $xzOn = true ];then
  826. printf "ERROR:\tOnly choose one compressing method\n"
  827. exit 1
  828. fi
  829. if [ ! -x $(which 7z) ];then
  830. printf "ERROR:\t7Z Not Installed\n"
  831. exit 1
  832. fi
  833. shift
  834. ;;
  835. -p|--prefix)
  836. backupPrefix=$2
  837. shift 2
  838. ;;
  839. --postfix)
  840. backupPostfix=$2
  841. shift 2
  842. ;;
  843. --no-remove)
  844. removeOld=false
  845. shift
  846. ;;
  847. --remove-days)
  848. deleteDays=$2
  849. shift 2
  850. ;;
  851. -u|--user)
  852. grep $2 /etc/passwd > /dev/null 2>&1
  853. if [ $? = 0 ]; then
  854. user=$2
  855. else
  856. printf "ERROR:\tUser %s don't exist\n" $2
  857. exit 1
  858. fi
  859. shift 2
  860. ;;
  861. -g|--group)
  862. grep $2 /etc/group > /dev/null 2>&1
  863. if [ $? = 0 ];then
  864. group=$2
  865. else
  866. printf "ERROR:\tGroup %s don't exist\n" $2
  867. exit 1
  868. fi
  869. shift 2
  870. ;;
  871. --permision-mask)
  872. if [ $2 -eq $2 ] 2> /dev/null;then
  873. permisionMask=$2
  874. else
  875. printf "ERROR:\tPermission has to be a number (%s)\n" $2
  876. exit 1
  877. fi
  878. shift 2
  879. ;;
  880. -k|--key-id)
  881. keyID=$2
  882. shift 2
  883. ;;
  884. --no-encryption)
  885. noEncryption=true
  886. shift
  887. ;;
  888. --7z-password)
  889. pass7z=$2
  890. shift 2
  891. ;;
  892. --web-dir)
  893. webDir=$2
  894. shift 2
  895. ;;
  896. --no-web)
  897. noWeb=true
  898. shift
  899. ;;
  900. --no-sql)
  901. noSql=true
  902. shift
  903. ;;
  904. --sql-user)
  905. mysqluser=$2
  906. shift 2
  907. ;;
  908. --sql-password)
  909. mysqlpass=$2
  910. shift 2
  911. ;;
  912. --nginx-dir)
  913. nginxDir=$2
  914. shift 2
  915. ;;
  916. --no-nginx)
  917. noNginx=true
  918. shift
  919. ;;
  920. --letsencrypt-dir)
  921. letsencryptDir=$2
  922. shift 2
  923. ;;
  924. --no-letsencrypt)
  925. noLetsencrypt=true
  926. shift
  927. ;;
  928. --mail-dir)
  929. mailDir=$2
  930. shift 2
  931. ;;
  932. --no-mail)
  933. noMail=true
  934. shift
  935. ;;
  936. --home-dir)
  937. homeDir=$2
  938. shift 2
  939. ;;
  940. --no-home)
  941. noHome=true
  942. shift
  943. ;;
  944. --gogs-dir)
  945. gogsDir=$2
  946. shift 2
  947. ;;
  948. --no-gogs)
  949. noGogs=true
  950. shift
  951. ;;
  952. --postfix-dir)
  953. postfixDir=$2
  954. shift 2
  955. ;;
  956. --no-postfix)
  957. noPostfix=true
  958. shift
  959. ;;
  960. --dovecot-dir)
  961. dovecotDir=$2
  962. shift 2
  963. ;;
  964. --no-dovecot)
  965. noDovecot=true
  966. shift
  967. ;;
  968. --opendkim-conf)
  969. openDKIMConf=$2
  970. shift 2
  971. ;;
  972. --opendkim-default)
  973. openDKIMDefault=$2
  974. shift 2
  975. ;;
  976. --opendkim-keys)
  977. openDKIMKeys=$2
  978. shift 2
  979. ;;
  980. --no-opendkim)
  981. noOpenDKIM=true
  982. shift
  983. ;;
  984. --spf-dir)
  985. spfDir=$2
  986. shift 2
  987. ;;
  988. --no-spf)
  989. noSPF=true
  990. shift
  991. ;;
  992. --opendmarc-conf)
  993. openDMARCConf=$2
  994. shift 2
  995. ;;
  996. --opendmarc-default)
  997. openDMARCDefault=$2
  998. shift 2
  999. ;;
  1000. --no-opendmarc)
  1001. noOpenDMARC=true
  1002. shift
  1003. ;;
  1004. --amavis-dir)
  1005. amavisDir=$2
  1006. shift 2
  1007. ;;
  1008. --no-amavis)
  1009. noAmavis=true
  1010. shift
  1011. ;;
  1012. --spamassassin-dir)
  1013. spamAssassinDir=$2
  1014. shift 2
  1015. ;;
  1016. --no-spamassassin)
  1017. noSpamAssassin=true
  1018. shift
  1019. ;;
  1020. --no-sha512)
  1021. sha512=false
  1022. shift
  1023. ;;
  1024. -h|--help)
  1025. usage
  1026. exit 0
  1027. shift
  1028. ;;
  1029. --)
  1030. #Last One
  1031. shift
  1032. break
  1033. ;;
  1034. *)
  1035. #Unspected
  1036. usage
  1037. printf "\nERROR:\tInvalid Option (%s)\n" $1
  1038. exit 1
  1039. ;;
  1040. esac
  1041. done
  1042. if [ ! $# -eq 1 ];then
  1043. printf "ERROR:\tBackup directory not set\n"
  1044. usage
  1045. exit 1
  1046. else
  1047. backupDir=$1
  1048. backupName=$backupPrefix$backupPostfix
  1049. backupOutput=$backupDir/$backupName
  1050. tempOutput=/tmp/$backupName.tar
  1051. fi
  1052. #Check (routes, compression, encryption and SQL auth)
  1053. checkRoutes
  1054. checkMethod
  1055. checkSqlAuth
  1056. #make tar file or 7z
  1057. backupOutput=$backupOutput$ext
  1058. if [ $p7zipOn = true ];then
  1059. if [ ! -x $(which 7z) ];then
  1060. printf "ERROR:\tp7zip Not Installed\n"
  1061. exit 1
  1062. fi
  1063. makep7zip
  1064. else
  1065. if [ ! -x $(which tar) ];then
  1066. printf "ERROR:\tTAR Not Installed\n"
  1067. exit 1
  1068. fi
  1069. makeTar
  1070. fi
  1071. #Compression and encryption (Except 7Zip -> Already done)
  1072. if [ $gzipOn = true ];then
  1073. if [ $noEncryption = true ];then
  1074. gzUnencrypted
  1075. else
  1076. gzEncrypted
  1077. fi
  1078. elif [ $bzip2On = true ];then
  1079. if [ $noEncryption = true ];then
  1080. bz2Unencrypted
  1081. else
  1082. bz2Encrypted
  1083. fi
  1084. elif [ $xzOn = true ];then
  1085. if [ $noEncryption = true ];then
  1086. xzUnencrypted
  1087. else
  1088. xzEncrypted
  1089. fi
  1090. else
  1091. if [ $p7zipOn = false ];then
  1092. printf "ERROR:\tCompression method not set\n"
  1093. rm $tempOutput
  1094. exit 1
  1095. fi
  1096. fi
  1097. #Make SHA512
  1098. if [ $sha512 = true ]; then
  1099. makeSHA512
  1100. fi
  1101. #Permissions
  1102. chown $user:$group $backupOutput
  1103. chmod $permisionMask $backupOutput
  1104. #Remove files older than 15 days
  1105. if [ $removeOld = true ];then
  1106. printf "Deletting old backups (+15 days)..."
  1107. find $backupDir -mindepth 1 -maxdepth 1 -mtime +$deleteDays -type f -iname "$backupPrefix*$ext" -delete
  1108. if [ $? != 0 ];then
  1109. printf "\nWARNING:\tImposible to delete old backups (+%s days)\n" $deleteDays
  1110. fi
  1111. printf " done\n"
  1112. fi
  1113. #End
  1114. finalTime=$(date +"%s")
  1115. printf "\n"
  1116. echo "------------------------------------------------"
  1117. printf "Backup completed successfully in %s seconds :)\n" $((finalTime-startTime))
  1118. echo "------------------------------------------------"