syncToServer.sh 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. #!/bin/bash
  2. ###################################################################
  3. # Written By:
  4. # Christian Kissinger
  5. #
  6. # use as serversync
  7. #
  8. ###################################################################
  9. ###################################################################
  10. # Destination(s)
  11. #
  12. # If unsure of NAS IP, use the following command:
  13. # nmap -sP 192.168.7.0/24 | awk '/^Nmap/{ip=$NF}/98:90:96/{print ip}'
  14. #
  15. # but replace 98:90:96 with what your NAS actually has
  16. #
  17. ###################################################################
  18. #NAS=`id -un 1000`@192.168.7.2
  19. NAS=192.168.7.2
  20. NFS1=/mnt/hdd1
  21. NFS2=/mnt/hdd2
  22. NASDIR1=$NAS:$NFS1
  23. NASDIR2=$NAS:$NFS2
  24. # change depending if desktop or laptop
  25. # This distinction mainly exists as
  26. # the author's desktops tend to have more
  27. # than one screen, thus need slightly different
  28. # configs for each.
  29. # mostly to check if exists or not
  30. LAPTOPDIR=$NFS1/config/laptop
  31. DESKTOPDIR=$NFS1/config/desktop
  32. BACKUPDIR=$NFS2/other_backups/`hostname`
  33. LAPTOP=/config/laptop
  34. DESKTOP=/config/desktop
  35. # Customise if Desktop or Laptop!!!
  36. MACHINE=$LAPTOP
  37. STO_CONF=$NASDIR1$MACHINE
  38. BACKUPS=$NASDIR2/other_backups/`hostname`
  39. ###################################################################
  40. # Sources
  41. ###################################################################
  42. # For everything!
  43. USERHOME=/home/`id -un 1000`
  44. # For use with primarily homeconf, and on client end
  45. CONFDIR=$USERHOME/.config
  46. FONTS=$USERHOME/.fonts
  47. CONFS=$USERHOME/.*rc
  48. OVPN=$USERHOME/.ovpn
  49. # Programs
  50. ARDUINO=$USERHOME/arduino-*
  51. ECLIPSE=$USERHOME/eclipse
  52. # Server end
  53. SERVER_CONFDIR=$STO_CONF/.config
  54. SERVER_FONTS=$STO_CONF/.fonts
  55. SERVER_CONFS=$STO_CONF/.*rc
  56. SERVER_OVPN=$STO_CONF/.ovpn
  57. SERVER_ARDUINO=$STO_CONF/arduino-*
  58. SERVER_ECLIPSE=$STO_CONF/eclipse
  59. ###################################################################
  60. # shortcuts
  61. ###################################################################
  62. USER=`id -un 1000`
  63. DOC=Documents
  64. PIX=Pictures
  65. MUS=Music
  66. VID=Videos
  67. DWN=Downloads
  68. ARD=Arduino
  69. ###################################################################
  70. # rsync flags
  71. ###################################################################
  72. RSF_TO=-chavzP
  73. RSF_FROM=-havuzP
  74. ###################################################################
  75. # misc
  76. ###################################################################
  77. EDITOR=vim
  78. ###################################################################
  79. # Functions
  80. ###################################################################
  81. usage()
  82. {
  83. echo "usage"
  84. echo -e "For backing up:
  85. config files only:
  86. '$(basename "$0") backup config'
  87. entire /home/user directory
  88. '$(basename "$0") backup home'
  89. both:
  90. '$(basename "$0") backup all'
  91. For syncing (copying files from server):
  92. config files only:
  93. '$(basename "$0") sync config'
  94. entire backed-up /home/user directory:
  95. '$(basename "$0") sync home'
  96. both:
  97. '$(basename "$0") sync all'
  98. Current rsync flags:
  99. to NAS: $RSF_TO
  100. from NAS: $RSF_FROM
  101. To change source and destinations:
  102. '$(basename "$0") edit'
  103. For help:
  104. '$(basename "$0") help'
  105. "
  106. exit 1
  107. }
  108. sync_home_to()
  109. {
  110. # See if directory does not exist first
  111. if ssh $USER@$NAS "! [ -d $BACKUPDIR ]"; then
  112. # Create the directory
  113. echo "Creating directory"
  114. ssh $USER@$NAS "mkdir -p $BACKUPDIR"
  115. fi
  116. # Sync Documents
  117. rsync $RSF_TO $USERHOME/$DOC $BACKUPS
  118. # Sync Pictures
  119. rsync $RSF_TO $USERHOME/$PIX $BACKUPS
  120. # Sync Music
  121. rsync $RSF_TO $USERHOME/$MUS $BACKUPS
  122. # Sync Videos
  123. rsync $RSF_TO $USERHOME/$VID $BACKUPS
  124. # Sync Downloads
  125. rsync $RSF_TO $USERHOME/$DWN $BACKUPS
  126. # Sync Arduino code
  127. rsync $RSF_TO $USERHOME/$ARD $BACKUPS
  128. }
  129. sync_config_to()
  130. {
  131. # See if directory does not exist first
  132. if [[ $MACHINE == $LAPTOP ]]; then
  133. if ssh $USER@$NAS "! [ -d $LAPTOPDIR ]"; then
  134. # Create the directory
  135. echo "Creating directory"
  136. ssh $USER@$NAS "mkdir -p $LAPTOPDIR"
  137. fi
  138. elif [[ $MACHINE == $DESKTOP ]]; then
  139. if ssh $USER@$NAS "! [ -d $DESKTOPDIR ]"; then
  140. # Create the directory
  141. echo "Creating directory"
  142. ssh $USER@$NAS "mkdir -p $DESKTOPDIR"
  143. fi
  144. else
  145. echo "Don't know what to do with $MACHINE, exiting..."
  146. exit 1
  147. fi
  148. # Sync .config dir
  149. rsync $RSF_TO $CONFDIR $STO_CONF
  150. # Sync .fonts dir
  151. rsync $RSF_TO $FONTS $STO_CONF
  152. # Sync dotfiles
  153. rsync $RSF_TO $CONFS $STO_CONF
  154. # Sync ovpn files
  155. rsync $RSF_TO $OVPN $STO_CONF
  156. # Sync Arduino and Eclipse program directories
  157. rsync $RSF_TO $ARDUINO $STO_CONF
  158. rsync $RSF_TO $ECLIPSE $STO_CONF
  159. # retrieve list of installed files for eventual re-setup
  160. TMP=$(mktemp)
  161. TMP2=$(mktemp)
  162. apt list | grep -i 'installed' | grep -v 'code/stable' > $TMP
  163. cut -d/ -f1 $TMP > $TMP2
  164. scp $TMP2 $STO_CONF/packages
  165. rm $TMP $TMP2
  166. }
  167. sync_home_from()
  168. {
  169. # If directory does not exist, just exit
  170. if ssh $USER@$NAS "! [ -d $BACKUPDIR ]"; then
  171. echo "No such configs stored, exiting..."
  172. exit 1
  173. fi
  174. # Sync Documents
  175. rsync $RSF_FROM $BACKUPS/$DOC $USERHOME
  176. # Sync Pictures
  177. rsync $RSF_TO $BACKUPS/$PIX $USERHOME
  178. # Sync Music
  179. rsync $RSF_TO $BACKUPS/$MUS $USERHOME
  180. # Sync Videos
  181. rsync $RSF_TO $BACKUPS/$VID $USERHOME
  182. # Sync Downloads
  183. rsync $RSF_TO $BACKUPS/$DWN $USERHOME
  184. # Sync Arduino code
  185. rsync $RSF_TO $BACKUPS/$ARD $USERHOME
  186. }
  187. sync_config_from()
  188. {
  189. # See if directory does not exist first
  190. if [[ $MACHINE == $LAPTOP ]]; then
  191. if ssh $USER@$NAS "! [ -d $LAPTOPDIR ]"; then
  192. echo "No such configs stored, exiting..."
  193. exit 1
  194. fi
  195. elif [[ $MACHINE == $DESKTOP ]]; then
  196. if ssh $USER@$NAS "! [ -d $DESKTOPDIR ]"; then
  197. echo "No such configs stored, exiting..."
  198. exit 1
  199. fi
  200. else
  201. echo "Don't know what to do with $MACHINE, exiting..."
  202. exit 1
  203. fi
  204. # Sync .config dir
  205. rsync $RSF_FROM $SERVER_CONFDIR $USERHOME
  206. # Sync .fonts dir
  207. rsync $RSF_FROM $SERVER_FONTS $USERHOME
  208. # Sync dotfiles
  209. rsync $RSF_FROM $SERVER_CONFS $USERHOME
  210. # Sync ovpn files
  211. rsync $RSF_FROM $SERVER_OVPN $USERHOME
  212. # Sync Arduino and Eclipse program directories
  213. rsync $RSF_FROM $SERVER_ARDUINO $USERHOME
  214. rsync $RSF_FROM $SERVER_ECLIPSE $USERHOME
  215. # Install Arduino and eclipse
  216. # Or at least have it executable via dmenu or rofi or the likes
  217. sudo cp $USERHOME/$ARD/arduino /usr/bin/arduino
  218. sudo cp $USERHOME/$ECLIPSE/eclipse /usr/bin/eclipse
  219. # Install packages listed
  220. scp $STO_CONF/packages $USERHOME
  221. sudo apt install `cat $USERHOME/packages`
  222. rm $USERHOME/packages
  223. }
  224. case $1 in
  225. -[Hh]|--[Hh][Ee][Ll][Pp])
  226. echo -e "$(basename "$0") is a simple script
  227. which uses rsync to copy data to or from a storage
  228. machine, like a NAS of sorts.
  229. "
  230. usage
  231. ;;
  232. [Bb][Aa][Cc][Kk][Uu][Pp])
  233. case $2 in
  234. [Aa][Ll][Ll])
  235. echo "backing up everything"
  236. sync_home_to
  237. sync_config_to
  238. ;;
  239. [Hh][Oo][Mm][Ee])
  240. echo "backing up home only"
  241. sync_home_to
  242. ;;
  243. [Cc][Oo][Nn][Ff][Ii][Gg])
  244. echo "backing up configs only"
  245. sync_config_to
  246. ;;
  247. *)
  248. usage
  249. ;;
  250. esac
  251. ;;
  252. [Ss][Yy][Nn][Cc])
  253. case $2 in
  254. [Aa][Ll][Ll])
  255. echo "Syncing everything"
  256. sync_home_to
  257. sync_config_from
  258. ;;
  259. [Hh][Oo][Mm][Ee])
  260. echo "Syncing home only"
  261. sync_home_from
  262. ;;
  263. [Cc][Oo][Nn][Ff][Ii][Gg])
  264. echo "Syncing configs only"
  265. sync_config_from
  266. ;;
  267. *)
  268. usage
  269. ;;
  270. esac
  271. ;;
  272. [Ee]|[Ee][Dd][Ii][Tt]|-[Ee]|--[Ee][Dd][Ii][Tt])
  273. $EDITOR /usr/bin/$(basename "$0")
  274. ;;
  275. *)
  276. usage
  277. ;;
  278. esac
  279. exit 0